Assume that you are writing a class that models a kitchen oven. You think about it for a while, and you come up with a description, as depicted in the Unified Modeling Language (UML) class diagram shown in Figure 16.1. Figure 16.1. A UML class diagram for the COven class.
The member variables shown in the UML class diagram might be interpreted in the following manner:
At this point the oven has four member variables. Although you could add other member variables to the class (for example, an oven might have a sensor you can insert into a roast to read its temperature), this is good enough for a starting point. In the COven class, you might want to alter the state of each one of these member variables through the user interface. You can do that with a property procedure. If you write property procedures for member variables, those member variables become properties that are accessible to users of the COven class. You have also added four class methods that can be used with the class. Although you might not have thought of everything, this design is a good starting point for the example. Now you need to create a new project and name it Oven . Next , you should create a new class for the project by selecting Project, Add Class, and name the new project COven (see Figure 16.2). Figure 16.2. Adding the COven class to a project.
After you press Enter, the screen changes so that the new class is shown in the Code window. (Your screen should look very similar to the screen shown in Figure 15.3 in Chapter 15.) Now you need to enter the following lines of code in the COven Code window: Private mOvenStatus As Integer ' Is the oven on or off Private mTemperature As Integer ' Oven temperature Private mDoorStatus As Integer ' Is oven door open or closed Private mOvenLight As Integer ' Is oven light on or off Public Property OvenStatus() As Integer These lines define the properties with the Private access specifier . As you know, this means that these class member variables are not directly available outside the COven class; that is, their scope is limited to the COven class itself. Property Accessor MethodsWhen you enter the last line shown in the code fragment in the preceding section and press the Enter key, some pretty neat stuff happens. Immediately, new lines of code are automatically supplied for you by Visual Basic .NET. You should see the following in your Code window: Public Property OvenStatus() As Integer Get End Get Set(ByVal Value As Integer) End Set End Property By default, every property of a class has a property Get and a property Set method. To distinguish these methods from other class methods, the Get and Set methods are formally called accessor methods. As you have probably guessed, the accessor methods are used to change a property value (the Set accessor method) or to read a property value (the Get accessor method). Visual Basic .NET automatically supplies a code skeleton for each of these accessor methods. Of course, you must flesh out the details for the accessor methods. Listing 16.1 shows the code for the OvenStatus accessor method. Listing 16.1 The Code for the OvenStatus Accessor MethodPublic Property OvenStatus() As Integer ' Purpose: To get the current status of the oven. ' 0 = oven off, non-zero = oven on Get Return mOvenStatus End Get ' Purpose: To change the status of the oven. It ' is assumed that Value is the desired ' temperature. Value = 0 means the oven ' is off. Any other value indicates a ' desired temperature value. Set(ByVal Value As Integer) ' Turn oven on or off Dim Flag As Integer If Value <> OVENOFF Then mOvenStatus = OVENON ' Oven is now on Else mOvenStatus = OVENOFF ' Oven is now off End If Flag = Bake(Value) End Set End Property The Get method in Listing 16.1 simply returns the symbolic constant ( OVENON or OVENOFF ) for the mOvenStatus class member, depending on its current state. (From now on, I drop accessor from the term accessor method. If you see the terms Get method and Set method, you should understand that I am talking about accessor methods.) By having a Get method for mOvenStatus and keeping it Private , you are forced to use the interface of the COven class to change mOvenStatus . Again, using class Get and Set methods to read or write a class property reinforces the idea of encapsulation for the class. The Set method for mOvenStatus is rather involved because you need to change the value of mOvenStatus . Note that the Set method has a parameter passed to it. The assumption is that a value of for the parameter reflects an oven-off state for the object, whereas a nonzero value says the oven is on. Again, you have defined symbolic constants for these values. (The complete code is presented in the following section, in Listing 16.2.) Programmer's Tip
The COven Class CodeListing 16.2 shows the complete code for the COven class. Most of the code should look pretty familiar to you by now. Listing 16.2 Code for the COven ClassOption Strict On Public Class COven '========== Constants ================= Private Const DOOROPEN = True ' Oven door open Private Const DOORCLOSED = 0 ' Oven door closed Private Const OVENON = True ' Oven on Private Const OVENOFF = 0 ' Oven off Private Const LIGHTON = True ' Oven light on Private Const LIGHTOFF = 0 ' Oven light off Private Const BROILTEMP = 550 ' Broiler temperature Private Const CLEANTEMP = 600 ' Self-clean temperature '========== class member data ========== Private mOvenStatus As Integer ' Is the oven on or off Private mTemperature As Integer ' Oven temperature Private mDoorStatus As Integer ' Is oven door open or closed Private mOvenLight As Integer ' Is oven light on or off '========== Constructor ================ Public Sub New() ' Purpose: To initialize the variables to their proper values. ' Even though some are zero, we set them explicitly. mOvenStatus = OVENOFF ' Oven is off mTemperature = 0 ' No temperature mDoorStatus = DOORCLOSED ' Oven door is closed mOvenLight = LIGHTOFF ' Oven light is off End Sub '================ Properties =========== Public Property OvenStatus() As Integer ' Purpose: To get the current status of the oven. ' 0 = oven off, 1 = oven on Get Return mOvenStatus End Get ' Purpose: To change the status of the oven. It ' is assumed that Value is the desired ' temperature. Value = 0 means the oven ' is off. Any other value indicates a ' desired temperature value. Set(ByVal Value As Integer) ' Turn oven on or off Dim Flag As Integer If Value <> OVENOFF Then mOvenStatus = OVENON ' Oven is now on Else mOvenStatus = OVENOFF ' Oven is now off End If Flag = Bake(Value) End Set End Property Public Property DoorStatus() As Integer ' Purpose: This property gets the status of the door Get Return mDoorStatus End Get ' Purpose: This property sets the status of the door Set(ByVal Value As Integer) If Value = 0 Then mDoorStatus = DOORCLOSED Else mDoorStatus = DOOROPEN End If End Set End Property Public Property LightStatus() As Integer ' Purpose: This property gets the status of the light Get Return mOvenLight End Get ' Purpose: This property sets the status of the light Set(ByVal Value As Integer) If Value = 0 Then mOvenLight = LIGHTOFF Else mOvenLight = LIGHTON End If End Set End Property Public Property Temperature() As Integer ' Purpose: This property gets the oven temperature Get Return mTemperature End Get ' Purpose: This property changes the oven temperature Set(ByVal Value As Integer) Dim Flag As Integer If Value < 0 Then mTemperature = 0 End If Flag = Bake(Value) ' Set the oven temperature End Set End Property '================ Methods ============== Public Function Bake(ByVal Temp As Integer) As Integer ' Purpose: This method is used to set the oven temperature ' to the temperature passed to this method. ' ' Argument list: ' Temp the desired temperature ' ' Return value: ' integer 1 if everything is OK, 0 on error ' If mOvenStatus = OVENOFF Then ' Oven is now off Return 0 ' Error End If mTemperature = Temp ' Since oven is on, set temperature Return 1 ' Everything's OK End Function Public Function Preheat(ByVal Temp As Integer) As Integer ' Purpose: This method is used to preheat the oven ' temperature to the value passed to this method. ' ' Argument list: ' temp the desired temperature ' ' Return value: ' integer 1 if everything is OK, 0 on error ' Return Bake(Temp) End Function Public Sub Broil() ' Purpose: This method is used to turn on the broiler ' in the oven . ' ' Argument list: ' N/A ' ' Return value: ' N/A ' mTemperature = BROILTEMP End Sub Public Sub SelfClean() ' Purpose: This method is used to self-clean the oven. ' ' Argument list: ' N/A ' ' Return value: ' N/A ' mTemperature = CLEANTEMP End Sub End Class The code in Listing 16.2 begins with the definition of a series of symbolic constants. Next, it defines the member variables ”that is, the properties ”for the class. The access specifier for each of the member variables is Private . This forces the user of the class to use the class interface to change the state of the object. Programmer's Tip
|