summary of text to be covered create your own class create and use objects of your own class control...

22
Summary of text to be covered • Create your own class • Create and use objects of your own class • Control access to object instance variables • Use keyword private • Create your own properties • Use panel control • Use string methods padLeft and Substrings

Post on 19-Dec-2015

218 views

Category:

Documents


0 download

TRANSCRIPT

Summary of text to be covered

• Create your own class• Create and use objects of your own class• Control access to object instance variables• Use keyword private• Create your own properties• Use panel control• Use string methods padLeft and

Substrings

Key points

• Classes: http://en.wikipedia.org/wiki/Class_%28computer_science%29

• Classes contain methods or variables and called members of the class e.g. in class random there is a method next which you have accessed using random.next()

• Each GUI control, toolbox control that you add is an instance of (an object) of that class. In other words your application is a client of that class.

• Keyword class and end class means start and end of the class

Chapter 19: Building your own classes and objects

• So far you have used classes defined in Framework class library (FCL) e.g. GUI objects, Classes String, Random, and Date

• When you add the GUI control to the form, an instance of that class is created

• Now we will create our own classes called programmer-defined classes.

• Classes can be reused

Microwave application

• A user enters the time for the microwave to cook food.

• To handle the time data we will create a class called Time. This class will store the number of minutes and seconds. (This will be used by the microwave application to keep track of remaining cooking time.

Microwave App

• Add a panel to the form. A panel is different from group box as it does not have a caption

• Rename the panel to windowPanel

• View the code. You will notice that the code already includes events for numeric buttons.

Microwave App• Add a new class by clicking project add

class and rename to time.vb• Any methods or variables created in this

class will be called members of this class• Keyword class and end class means start

and end of the class• Comments are very important to describe

the purpose of the class. Purpose of this class is to represent time data and its properties.

Microwave App

• Declare 2 instance variables:Dim minuteValue As integer

Dim secondValue as Integer

• Value of minute will be stored in minuteValue and seconds will be stored in secondValue

• Each class contains instance variables and methods.

Microwave App

• Instance variables are variables defined in a class and are copied each time an object of that class is created.

• Methods in class perform certain activities e.g. random.next or string.format. Next and format are methods defined in random and string class

• Constructor is a special type of methods which gets called first and initializes the classes instance variables

Microwave App

• A constructor is a sub procedure type of a method so it cannot return a value

' Time constructor, minute and second supplied

Public Sub New(ByVal mm As Integer, ByVal ss As Integer)

Minute=mm

Second=ss

End Sub

• So when creating an object of this class the statement will look like – timeObject= New Time (5,3)

Microwave App

• Now back in the main application declare an object of the class time under timeIs variableDim timeObject As Time

• Classes also have properties which allow manipulating the instance variables e.g. to manipulate the value of variable minuteValue and secondValue, we will create two properties: minute and second.

Microwave App

• The property consists of two accesors– One to modify and return a value (Get

accessor) e.g. minuteValue=timeObject.minute

– One to assign a value (Set accessor) e.g. timeObject.minute=35. This can also be used to validate the values e.g. 0<min<59.

Microwave App

• Declare the property functionPublic Property Minute () As Integer

• The rest of the code will be declared automatically.

• Now the get accessor returns the value stored in minute so return minuteValue

• Set validates the minute and stores the value so check if 0<min<60 is met and assign the value.

Microwave AppPublic Property minute() As Integer Get Return minuteValue End Get

'set minute value Set(ByVal value As Integer) If (value < 60) Then minuteValue = value Else minuteValue = 0 End If End Set End Property

Public Property second() As Integer Get Return secondValue End Get

Set(ByVal value As Integer) If (value < 60) Then secondValue = value Else secondValue = 0 End If End Set End Property

Microwave App

• Now that your class is ready, create the start button event.Dim second as IntegerDim minute as IntegertimeIs=timeIs.PadLeft(4, convert.ToChar(“0”))

• Padleft ensures that the string is atleast 4 characters long so 3:25 displays as 03:25

• Now the first two digits represent minutes and last two represent seconds

Microwave App.

• Now to break the string and get minutes and seconds add the following code:Second=Convert.ToInt32(timeIs.Substring(2))Substring(2) means start at the second position

and go till the end. So 0345 will start at 4 and give 45

Minute=convert.ToInt32(timeIs.Substring(0,2))Substring(0,2) means start at position 0 and get

2 characters. So 0345 will start at 0 and give 03.

Microwave App

• Create the time object

timeObject = New Time(minute, second)• New allocates the memory in which the time

object will be stored. The constructor in class time initializes the instance variables.displayLabel.Text = String.Format("{0:d2}:{1:d2}",

timeObject.minute, timeObject.second)

timeIs = ""

clockTimer.Enabled = True

windowPanel.BackColor = Color.Yellow

Microwave App.

• Define the clear button event

displayLabel.Text = " Microwave Owen"

timeIs = ""

timeObject = New Time(0, 0)

clockTimer.Enabled = False

windowPanel.BackColor = Control.DefaultBackColor

Display time sub ' method to display formatted time in timer window Sub DisplayTime() Dim second As Integer Dim minute As Integer Dim display As String

If timeIs.Length > 4 Then timeIs = timeIs.Substring(0, 4) End If

display = timeIs.PadLeft(4, Convert.ToChar("0")) second = Convert.ToInt32(display.Substring(2)) minute = Convert.ToInt32(display.Substring(0, 2)) displayLabel.Text = String.Format("{0:d2}:{1:d2}", minute, second) End Sub ' DisplayTime

Only thing left is the countdown If timeObject.second > 0 Then timeObject.second -= 1 ElseIf timeObject.minute > 0 Then timeObject.minute -= 1 timeObject.second = 59 Else clockTimer.Enabled = False Beep() displayLabel.Text = "Done" windowPanel.BackColor = Control.DefaultBackColor Return ‘ returns the control back to the application without executing

any further End If

displayLabel.Text = String.Format("{0:d2}:{1:d2}", timeObject.minute, timeObject.second)

Summary

• Declare a class

• Declare instance variables

• Declare a constructor (sub procedure that is called when class object is created). Used to initialize instance variables

• Declare properties of the class: Get/Set

• Create objects of the class and use them using dot operator

Controlling access to members

• Keywords public and private are called member-access modifiers. Public class objects are available to any Time object. Private makes it available to only methods and properties of the class.

• Instance variables are usually declared private whereas methods and properties are declared public.

Inclass group exercise

• 19:13 – The local bank wants you to create an application that will allow it to view clients information. The interface is given below. You need to implement the client class. Once the application is complete, the manager should be able to click the next or previous button to run through each clients information. The information is stored in arrays.

Download the template of the application from here.