In VB6, I’m trying to pass a late bound object to another form.
frmMain.vb
Dim x
Set x = CreateObject("MyOwn.Object")
Dim f as frmDialog
Set f = New frmDialog
f.SetMyOwnObject x
frmDialog
Dim y
Public Sub SetMyOwnObject(ByVal paramX As Variant)
Set y = paramX
End Sub
The contents of y are a string containing the type name of the late bound object, “MyOwn.Object”. ByVal and ByRef don’t make a difference. Any clues? Having trouble remembering.
This seems to confirm my suspicions. The
MyOwn.Objectclass must have a default property or method that returns a string.Therefore, when you try to
Debug.Printit, it will return the value of the default property/method. When you hover over the variable in the IDE, VB6 will display the value of the default property/method. When you do aVarTypecall onyit will return the variable type of the default property or method.The reason is that when you have a variable of type
Variantthat stores anObject, and the class of the object defines a default method or property, the variable will evaluate to the return value of the default method or property in most situations.You can quickly check to see if the
MyOwn.Objectclass has a default member by opening the Object Browser to theMyOwn.Objectclass and looking at the its list of properties and methods. If you see a method or property that has an icon with small blue circle in the corner, that indicates the method or property is the default member of the class. If you find one, I’m willing to bet it’s declared to return a string.Note that even if you changed all your
VariantS toObjectS, you would still encounter this behavior in a number of places. For example, even ifyis declaredAs Object, doing aDebug.Print ywill still print out the value of the default property or method, and doing aVarType(y)will still return 8 (string).Knowing exactly when VB6 will use the default member and when it won’t can be confusing. For example, if you declare
yasObject, then doingTypeName(y)will returnMyOwn.Class, butVarType(y)will still return 8 (string). However, if you declareyasVariant, thenTypeName(y)returnsString.If you are using late-binding, it’s hard to avoid this side-effect, since you’ll only be able to declare your object variable as
ObjectorVariant.