Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Language: VB4,VB5,VB6
Expertise: Intermediate
Dec 4, 1999

Better type checking in Variant properties that can accept objects

As explained in the Mistake Bank you need all three flavors of Property procedures to correctly implement a Variant property that can take either an object or a non-object value. Surprisingly enough, in this case you don't need that the argument of the Property Set procedure be declared As Variant. In fact, you can slightly optimize your code by declaring the argument of the Property Set procedure using As Object instead:


Property Set Tag(newValue As Object)
    Set m_Tag = newValue
End Property
You can build on this undocumented capability to write more concise code and further optimize the procedure by using a more specific type of object. For example, let's assume that the Address property can take either a string or a CAddress object. You might believe that you need to test the type of the object at run-time using this code:


Dim m_Address As Variant

Property Set Address(newValue As Variant)
    If TypeName(newValue) <> "CAddress" Then
        Err.Raise 1003, , "A CAddress object is expected"
    End If
    Set m_Address = newValue
End Property
However, you can let Visual Basic do the test on your behalf, and build a better routine as follows:


Dim m_Address As Variant

Property Set Address(newValue As CAddress)
    Set m_Address = newValue
End Property
Francesco Balena
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date