如何传递后期绑定参数

发布于 2024-07-27 19:27:49 字数 404 浏览 4 评论 0原文

在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

百善笑为先 2024-08-03 19:27:49

我手边没有 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.

夏雨凉 2024-08-03 19:27:49

我使用了 VarType(y)。 结果是8,
对于 vbString. 应该是9
目的。 – ssorrrell 1 小时前

在立即窗口中使用“打印 y”
查找 y 的内容。 – 索雷尔 55
分钟前

这似乎证实了我的怀疑。 MyOwn.Object 类必须具有返回字符串的默认属性或方法。

因此,当您尝试Debug.Print它时,它将返回默认属性/方法的值。 当您将鼠标悬停在 IDE 中的变量上时,VB6 将显示默认属性/方法的值。 当您对 y 执行 VarType 调用时,它将返回默认属性或方法的变量类型。

原因是,当您有一个存储 ObjectVariant 类型的变量,并且该对象的类定义了默认方法或属性时,该变量将计算为在大多数情况下,返回默认方法或属性的值。

您可以通过打开 MyOwn.Object 类的对象浏览器并查看来快速检查 MyOwn.Object 类是否具有默认成员在其属性和方法列表中。 如果您看到某个方法或属性的角上有一个带有小蓝色圆圈的图标,则表明该方法或属性是该类的默认成员。 如果你找到一个,我愿意打赌它被声明为返回一个字符串。

请注意,即使您将所有 VariantS 更改为 ObjectS,您仍然会在很多地方遇到此行为。 例如,即使 y 被声明为 As Object,执行 Debug.Print y 仍然会打印出默认属性或方法的值,执行 VarType(y) 仍将返回 8(字符串)。

确切地知道 VB6 何时使用默认成员以及何时不使用可能会造成混乱。 例如,如果您将 y 声明为 Object,则执行 TypeName(y) 将返回 MyOwn.Class,但 VarType(y) 仍将返回 8(字符串)。 但是,如果将 y 声明为 Variant,则 TypeName(y) 返回 String

如果您使用后期绑定,则很难避免这种副作用,因为您只能将对象变量声明为 ObjectVariant

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.

心作怪 2024-08-03 19:27:49

您确定没有省略 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 对象浏览器中用蓝点标记)?

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)?

情绪少女 2024-08-03 19:27:49

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

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文