Search code examples
vb6parameterslate-binding

How to Pass a Late Bound Parameter


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.


Solution

  • I used VarType(y). The result is 8, for vbString. It should be 9 for object. – ssorrrell 1 hour ago

    Use Print y in the Immediate window to find the contents of y. – ssorrrell 55 mins ago

    This seems to confirm my suspicions. The MyOwn.Object class must have a default property or method that returns a string.

    Therefore, when you try to Debug.Print it, 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 a VarType call on y it will return the variable type of the default property or method.

    The reason is that when you have a variable of type Variant that stores an Object, 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.Object class has a default member by opening the Object Browser to the MyOwn.Object class 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 to ObjectS, you would still encounter this behavior in a number of places. For example, even if y is declared As Object, doing a Debug.Print y will still print out the value of the default property or method, and doing a VarType(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 y as Object, then doing TypeName(y) will return MyOwn.Class, but VarType(y) will still return 8 (string). However, if you declare y as Variant, then TypeName(y) returns String.

    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 Object or Variant.