In addition to (enumerated) class properties, our class objects might expose properties that return object values. To give you an example, Visual Basic object such as forms and visible controls expose a Font property, which returns a Font object.
txtFirstName.Font.Name = "Tahoma" txtFirstName.Size = 10 txtFirstName.Bold = True frmStudent.Font.Bold = True |
We can also do this in our classes. Taking our Student class, we might add a Address property, but string is not enough to point accurately where the student lives, and we usually need several pieces of related information, such street, city, state or province, zip code, as well as country. Instead of adding multiple properties to the Student object, create a new Address class:
- To create an Address class
- On the Project menu, click Add Class Module.
- In the Add Class Module dialog box, select Class Module, then click Open.
- In the Properties window, set the Name property for the class module to Address.
- In the Code window, type the following:
Option Explicit ' In Address class module declaration Private m_Street As String Private m_City As String Private m_State As String Private m_Zip As String Private m_Country As String Public Property Let Street(ByVal strNewStreet As String) If Len(strNewStreet) = 0 Then Err.Raise 5 m_Street = strNewStreet End Property Public Property Get Street() As String Street = m_Street End Property Public Property Let City(ByVal strNewCity As String) If Len(strNewCity) = 0 Then Err.Raise 5 m_City = strNewCity End Property Public Property Get City() As String City = m_City End Property Public Property Let State(ByVal strNewState As String) If Len(strNewState) = 0 Then Err.Raise 5 m_State = strNewState End Property Public Property Get State() As String State = m_State End Property Public Property Let Zip(ByVal strNewZip As String) If Len(strNewZip) = 0 Then Err.Raise 5 m_Zip = strNewZip End Property Public Property Get Zip() As String Zip = m_Zip End Property Public Property Let Country(ByVal strNewCountry As String) If Len(strNewCountry) = 0 Then Err.Raise 5 m_Country = strNewCountry End Property Public Property Get Country() As String Country = m_Country End Property Public Function CompleteAddress() As String CompleteAddress = Street & vbCrLf & _ City & ", " & State & " " & Country & " " & Zip End Function
Now you can add our new Address property to our Student class in declaration section of the Student class module:
'In the declaration section of the Student class module 'Enum type declaration omitted Private m_Student_ID As String Private m_FirstName As String Private m_LastName As String Private m_MajorCode As MajorCodeEnum Private m_YearLevel As YearLevelEnum Private m_BirthDate As Date Private m_Gender As GenderEnum Private m_Address As Address ' Student address |
Property Set procedures
A Property Set procedure sets the value of a property that contains a reference to an object. When you assign a value to an object, you must use the Visual Basic Set statement. An example of a property which is an object itself would be the Font property of the TextBox control. Because you're dealing with object references, you must use the Setkeyword in both procedures. Add the following property procedure and additional method, as well as the revised version of StudentInfo method in Student class module:
' Student Address property procedures Property Get Address() As Address Set Address = m_Address End Property Property Set Address(ByVal strNewAddress As Address) Set m_Address = strNewAddress End Property ' New Student method Function StudentAddressInfo() As String If m_Address Is Nothing Then Err.Raise 5 StudentAddressInfo = m_Address.CompleteAddress End Function ' Student StudentInfo method revised Function StudentInfo(Optional ByVal IncludedAddressInfo As Boolean = True) As String ' Returns the Student information Dim info As String info = "Student # : " & StudentID & vbCrLf & _ "Name : " & FullName & vbCrLf & _ "Age : " & Age & vbCrLf & _ "Gender : " & GenderDescription & vbCrLf & _ "Major Code : " & MajorCode & vbCrLf & _ "Major Description: " & MajorCodeDescription & vbCrLf & _ "Year Level : " & YearLevelDescription If IncludedAddressInfo Then info = info & "Address : " & StudentAddressInfo() StudentInfo = info End Function |
In our new StudentAddressInfo method, it is a good programming practice that you check first the existence of an object (Address) with in an object (Student), because a call to that method will raise an error number 91.
Now you can create a Address object in client form, initialize its properties, and then assign it to the Address property of the Student object.
' In client form ' Declare Student object and Address Object Dim Student As Student Dim Address As Address ' Initiate the object Student Set Student = New Student ' Initiate the object Address Set Address = New Address' Set up Address properties With Address .Street = "Block 10 Lot 26, Molave Street, Calendola Village" .City = "San Pedro" .State = "Laguna" .Country = "Philippines" .Zip = "4023" End With ' Set up Student pproperties With Student .FullName = "Dante Salvador" ' Add the newly created Address object to Student Address property Set .Address = Address .StudentID = "102472" .BirthDate = #10/24/1972# .Gender = Male .YearLevel = Senior .Major = BSCS End With ' Show Student information MsgBox Student.StudentInfo |
Comments