Chapter 16 Inheriting Forms and Creating Base Classes 405
documentation often shows these Get and Set code blocks when discussing classes, so
you should learn the standard syntax now .)
- Type in the following FirstName property procedure structure that uses the Get and Set
keywords. You’ll notice that much of the structure is added automatically after you type
the first Get statement:
Get
Return Name1
End Get
Set(ByVal value As String)
Name1 = value
End Set
End Property
In this structure, the Return keyword specifies that the Name1 string variable will be
returned when the FirstName property is referenced. The Set block assigns a string
value to the Name1 variable when the property is set. Notice here especially the value
variable, which is used in property procedures to stand for the value that’s assigned
to the class when a property is set. Although this syntax might look strange, trust
me for now—this is the formal way to create property settings in controls, and more
sophisticated properties would even add additional program logic here to test values
or make computations.
- Below the End Property statement, type a second property procedure for the LastName
property in your class. Again, after you type the Get keyword, much of the structure for
the property procedure will be added automatically:
Public Property LastName() As String
Get
Return Name2
End Get
Set(ByVal value As String)
Name2 = value
End Set
End Property
This property procedure is similar to the first one except that it uses the second string
variable (Name2) that you declared at the top of the class.
You’re finished defining the two properties in your class. Now let’s move on to a
method named Age that will determine the new employee’s current age based on his
or her birth date.
Step 3: Create a method
n Below the LastName property procedure, type the following function definition:
Public Function Age(ByVal Birthday As Date) As Integer
Return Int(Now.Subtract(Birthday).Days / 365.25)
End Function