Question

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.

Was it helpful?

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.

OTHER TIPS

I don't have a copy of VB6 handy, but I can remember doing the same thing more or less pretty often, and I believe that we used Object rather than Variant in the method signature. Variant is generally a lot less predictable in terms of what kinds of conversions it may run on a variable, whereas with Object I'm fairly sure VB won't attempt any sort of conversion.

Are you sure you haven't omitted the Set keyword e.g.

Dim y
Public Sub SetMyOwnObject(ByVal paramX As Variant)
  ' Set y = paramX  ' thought you had this...
  y = paramX        ' ...actually have this
End Sub

If this were the case then value of y would be the object's default value. Does your MyOwn.Object class have a property that returns a description of its type and has been defined as the default member for the class (marked with a blue dot in the VB Object Browser)?

frmMain.vb

Dim x As Object
Set x = CreateObject("MyOwn.Object")
Dim f as frmDialog
Set f = New frmDialog
f.SetMyOwnObject x

frmDialog

Dim y As Object
Public Sub SetMyOwnObject(ByRef paramX As Object)
  Set y = paramX
End Sub

When you use CreateObject, you are creating an Object not a Variant. When you pass an Object generally you use ByRef.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top