devxlogo

Deal properly with Variant properties that can contain objects

Deal properly with Variant properties that can contain objects

If you want to implement a Variant property that can also be assigned an object, you must be very careful about how you build the corresponding Property procedures. To begin with, you must create three procedures, that is Property Get, Let and Set. In fact, the Property Set procedure will be invoked when the property is assigned an object value, while the Property Let procedure will be invoked in all other cases. Moreover, the Property Get procedure must account for the two types of return values. This is the code that you should end up with:

' the Tag property can be assigned either an object' or a non-object valueDim m_Tag As VariantProperty Get Tag() As Variant    If IsObject(m_Tag) Then        Set Tag = m_Tag    Else        Tag = m_Tag    End IfEnd Property' this procedure is called when an object is' assigned to the Tag propertyProperty Set Tag(newValue As Variant)    Set m_Tag = newValueEnd Property' this procedure is called when a non-object ' value is assigned to the Tag propertyProperty Let Tag(newValue As Variant)    m_Tag = newValueEnd Property

devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist