We need you!

We're working hard on the next version of Developer Fusion. Let us know what you think we should be up to!

Members

Technology Zones

Articles

Hosted By

MaximumASP

Info

Rated
Read 63,759 times

Contents

Related Categories

Introduction to Class Programming Part II - Variant Properties

tengtium

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.

Comments

  • Re: [3965] Introduction to Class Programming Part II

    Posted by testorp on 06 Apr 2007

    these tutorials are great, have shed a lot of light on a rather difficult subject.


     


    One further expalnation, how can I make the OBJECT an array???


    the example givven i...

  • Thank You for a perfekt tutorial

    Posted by oyejo on 07 Oct 2005

    Introduction to Class Programming Part I and II is the best tutorials I ever seen.
    I look forward to the next tutorial from this author. :-)

  • logon with windows profile on my login form

    Posted by bisigreat on 07 Jun 2005

    Hi All,

    I created a Login form and expect that whenever i load the form the current username on the computer should be displayed on the username textbox. The user can now put his/her password in th...

  • excellent tutorial

    Posted by bisigreat on 07 Jun 2005

    Over the years, i have been running away from classes because it was ambiguous to me but with what i have read in Class programming Part I, i know it could be fun with OOP. It's was an excellent piece...

  • Posted by James Crowley on 29 Oct 2003

    I'm not sure what you mean. If you click "Printer Friendly Version", you don't get any adverts or iframes or anything........