Better type checking in Variant properties that can accept objects

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 = newValueEnd 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 VariantProperty Set Address(newValue As Variant)    If TypeName(newValue) <> "CAddress" Then        Err.Raise 1003, , "A CAddress object is expected"    End If    Set m_Address = newValueEnd Property

However, you can let Visual Basic do the test on your behalf, and build a better routine as follows:

Dim m_Address As VariantProperty Set Address(newValue As CAddress)    Set m_Address = newValueEnd Property

Share the Post:
data observability

Data Observability Explained

Data is the lifeblood of any successful business, as it is the driving force behind critical decision-making, insight generation, and strategic development. However, due to its intricate nature, ensuring the

Heading photo, Metadata.

What is Metadata?

What is metadata? Well, It’s an odd concept to wrap your head around. Metadata is essentially the secondary layer of data that tracks details about the “regular” data. The regular

XDR solutions

The Benefits of Using XDR Solutions

Cybercriminals constantly adapt their strategies, developing newer, more powerful, and intelligent ways to attack your network. Since security professionals must innovate as well, more conventional endpoint detection solutions have evolved