Introduction to Class Programming Part II

Variant Properties

You can add flexibility to your class by including a Variant member.  Assuming that you want to implement a ProvincialAddress property, but you want to keep it more flexible and capable of storing either a Address object or a string.   Now let us add ProvincialAddress property to our Student class as shown below: 

Private m_ProvincialAddress As Variant

Property Get ProvincialAddress() As Variant
    If IsObject(m_ProvincialAddress) Then
        Set ProvincialAddress = m_CurrentAddress   ' Return a Address object.
    Else
        ProvincialAddress = m_ProvincialAddress    ' Return a string.
    End If
End Property

Property Let ProvincialAddress(ByVal strNewProvincialAddress As Variant)
    m_ProvincialAddress = strNewProvincialAddress
End Property

Property Set ProvincialAddress(ByVal strNewProvincialAddress As Variant)
    Set m_ProvincialAddress = strNewProvincialAddress
End Property

' Revised StudentInfo method
Function StudentInfo(Optional ByVal IncludeAddressInfo As Boolean = True, _
                     Optional ByVal IncludeProvincialAddressInfo As Boolean = False) 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 IncludeAddressInfo Then info = info & vbCrLf & "Address : " & StudentAddressInfo()
    If IncludeProvincialAddressInfo Then info = info & vbCrLf & "Provincial Add : " & _
                                         StudentProvincialAddInfo()
                 
    StudentInfo = info
End Function

' Newly added method for Student class
Function StudentProvincialAddInfo() As String
' Return Student Provincial address 
    If IsObject(m_ProvincialAddress) Then
       ' invoke Address CompleteAddress method
        StudentProvincialAddInfo = m_ProvincialAddress.CompleteAddress
    Else
       ' simply return a string
       StudentProvincialAddInfo = m_ProvincialAddress
    End If
End Function

But things are a bit more complex if the property can receive either a regular value or an object value. While this sort of flexibility adds a lot of power to your class, it also reduces its robustness because nothing keeps a programmer from adding a nonstring value or an object of a class other than Address:

'In the client form

With Student
   .FullName = "Dante Salvador"
   Set .Address = 12345		' an Integer value					
   .StudentID = "102472"
   'etc  
End With

Because ProvincialAddress property is declared as Variant type, meaning you can assign any value, including numeric type.   To have more control of what is actually assigned to this property, you need to arbitrate all accesses to it through Property procedures:

' In Student class module
' Revised property procedures
Property Let ProvincialAddress(ByVal strNewProvincialAddress As Variant)
    ' Check if it is a string value.If VarType(strNewProvincialAddress) <> vbString Then Err.Raise 5
    m_ProvincialAddress = strNewProvincialAddress
End Property

Property Set ProvincialAddress(ByVal strNewProvincialAddress As Variant)
    ' Check if it is a Address object.
    If TypeName(strNewProvincialAddress) <> "Address" Then Err.Raise 5
    Set m_ProvincialAddress = strNewProvincialAddress
End Property

'In the client form
With Student
   .FullName = "Dante Salvador"
   Set .Address = 12345		' this raises an error
   .StudentID = "102472"
   'etc  
End With

Another technique that you can use that give slightly improve run-time performances and you save some code is to declare the type of the object you're expecting right in the parameter list of the Property Set procedure:

Property Set ProvincialAddress(ByVal strNewProvincialAddress As Address)
    ' Check if it is a Address object.
    If TypeName(strNewProvincialAddress) <> "Address" Then Err.Raise 5
    Set m_ProvincialAddress = strNewProvincialAddress
End Property

But  you can't use it when if your class accept two or more objects of different types. One solution is use As Object parameter:

Property Set ProvincialAddress(ByVal strNewProvincialAddress As Object)
    If TypeName(strNewProvincialAddress) <> "Address" And _
       TypeName(strNewProvincialAddress) <> "OtherAddressType" Then Err.Raise 5
    Set m_CurrentAddress = newValue
End Property

Object Keyword

The TypeName function

The TypeName function returns the name of an object's class in the form of a string. This means that you can find the type of an object in a more concise form

In many situations, testing an object's type using the TypeName function is preferable to using the TypeOf...Is statement because it doesn't require that the object class be present in the current application or in the References dialog box.  For information about TypeOf...Is function, consult your Visual Basic documentation.

The VarType function

The VarType function returns the type name of an object's class in the form of a string. Variant variables can also host special values that don't correspond to any data values described so far. The Emptyvalue is the state of a Variant variable when nothing has been assigned to it yet. You can test this special value using the IsEmpty function, or you can test the VarType function for the value 0-vbEmpty. 

The Null value is useful in database programming to mark fields that don't contain a value. You can explicitly assign the Null value to a Variant using the Nullconstant, test for a Null value using the IsNull function, or compare the return value of the VarType function with the value 1-vbNull.  For more information about this function, consult your Visual Basic documentation.

You might also like...

Comments

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“I invented the term Object-Oriented, and I can tell you I did not have C++ in mind.” - Alan Kay