如何传递后期绑定参数 [英] How to Pass a Late Bound Parameter

查看:85
本文介绍了如何传递后期绑定参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

In VB6, I'm trying to pass a late bound object to another form.

frmMain.vb

frmMain.vb

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

frmDialog

frmDialog

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

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

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.

推荐答案

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

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

在立即窗口中使用打印y来 找到y的内容. – ssorrrell 55 分钟前

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

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

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

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

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.

原因是,当您拥有一个类型为Variant的变量来存储Object,并且该对象的类定义了默认方法或属性时,该变量将求值为默认方法的返回值,在大多数情况下都是财产.

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.

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

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.

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

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

确切知道VB6何时使用默认成员以及何时不使用默认成员可能会造成混淆.例如,如果将y声明为Object,则执行TypeName(y)将返回MyOwn.Class,但是VarType(y)仍将返回8(字符串).但是,如果将y声明为Variant,则TypeName(y)返回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.

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

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.

这篇关于如何传递后期绑定参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆