4

在 VB6 中,我试图将后期绑定的对象传递给另一种形式。

frmMain.vb

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

对话框

Dim y
Public Sub SetMyOwnObject(ByVal paramX As Variant)
  Set y = paramX
End Sub

y 的内容是一个字符串,其中包含后期绑定对象的类型名称“MyOwn.Object”。ByVal 和 ByRef 没有区别。有什么线索吗?难以记忆。

4

4 回答 4

2

我手头没有 VB6 的副本,但我记得或多或少经常做同样的事情,而且我相信我们在方法签名中使用了 Object 而不是 Variant。Variant 通常很难预测它可以在变量上运行什么样的转换,而对于 Object,我相当确定 VB 不会尝试任何类型的转换。

于 2009-07-22T21:36:37.703 回答
0

frmMain.vb

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

对话框

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

当您使用 CreateObject 时,您创建的是一个 Object 而不是 Variant。当你传递一个对象时,通常你使用 ByRef。

于 2009-07-23T16:22:50.483 回答
0

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.

于 2009-07-23T19:16:00.167 回答
0

你确定你没有省略 Set 关键字,例如

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

如果是这种情况,那么 y 的值将是对象的默认值。您的 MyOwn.Object 类是否有一个属性,该属性返回其类型的描述并已被定义为该类的默认成员(在 VB 对象浏览器中用蓝点标记)?

于 2009-07-23T09:22:05.367 回答