如何传递后期绑定参数
在 VB6 中,我试图将后期绑定对象传递给另一个窗体。
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
y 的内容是一个字符串,其中包含后期绑定对象的类型名称“MyOwn.Object”。 ByVal 和 ByRef 没有区别。 有什么线索吗? 记忆有困难。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我手边没有 VB6 的副本,但我记得或多或少经常做同样的事情,并且我相信我们在方法签名中使用了 Object 而不是 Variant。 Variant 通常很难预测它可以在变量上运行什么类型的转换,而对于 Object,我相当确定 VB 不会尝试任何类型的转换。
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.
这似乎证实了我的怀疑。
MyOwn.Object
类必须具有返回字符串的默认属性或方法。因此,当您尝试
Debug.Print
它时,它将返回默认属性/方法的值。 当您将鼠标悬停在 IDE 中的变量上时,VB6 将显示默认属性/方法的值。 当您对y
执行VarType
调用时,它将返回默认属性或方法的变量类型。原因是,当您有一个存储
Object
的Variant
类型的变量,并且该对象的类定义了默认方法或属性时,该变量将计算为在大多数情况下,返回默认方法或属性的值。您可以通过打开
MyOwn.Object
类的对象浏览器并查看来快速检查MyOwn.Object
类是否具有默认成员在其属性和方法列表中。 如果您看到某个方法或属性的角上有一个带有小蓝色圆圈的图标,则表明该方法或属性是该类的默认成员。 如果你找到一个,我愿意打赌它被声明为返回一个字符串。请注意,即使您将所有
Variant
S 更改为Object
S,您仍然会在很多地方遇到此行为。 例如,即使y
被声明为As Object
,执行Debug.Print y
仍然会打印出默认属性或方法的值,执行VarType(y)
仍将返回 8(字符串)。确切地知道 VB6 何时使用默认成员以及何时不使用可能会造成混乱。 例如,如果您将
y
声明为Object
,则执行TypeName(y)
将返回MyOwn.Class
,但VarType(y)
仍将返回 8(字符串)。 但是,如果将y
声明为Variant
,则TypeName(y)
返回String
。如果您使用后期绑定,则很难避免这种副作用,因为您只能将对象变量声明为
Object
或Variant
。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 aVarType
call ony
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 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.Object
class has a default member by opening the Object Browser to theMyOwn.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
Variant
S toObject
S, you would still encounter this behavior in a number of places. For example, even ify
is declaredAs Object
, doing aDebug.Print y
will 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
y
asObject
, then doingTypeName(y)
will returnMyOwn.Class
, butVarType(y)
will still return 8 (string). However, if you declarey
asVariant
, 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
Object
orVariant
.您确定没有省略 Set 关键字吗?例如,
如果是这种情况,则 y 的值将是对象的默认值。 您的 MyOwn.Object 类是否有一个返回其类型描述的属性,并且已被定义为该类的默认成员(在 VB 对象浏览器中用蓝点标记)?
Are you sure you haven't omitted the Set keyword e.g.
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
frmDialog
当您使用 CreateObject 时,您创建的是一个对象而不是变体。
当您传递对象时,通常使用 ByRef。
frmMain.vb
frmDialog
When you use CreateObject, you are creating an Object not a Variant.
When you pass an Object generally you use ByRef.