如何通过后界参数

时间:2021-07-22 15:55:34

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

在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

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.

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

4 个解决方案

#1


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

我用过VarType(y)。对于vbString,结果是8。对象应该是9。 - ssorrrell 1小时前

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

使用立即窗口中的打印y查找y的内容。 - ssorrrell 55分钟前

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

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

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.

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

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.

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

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.

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

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

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

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.

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

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

#2


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.

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

#3


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

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

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

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

#4


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.

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

#1


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

我用过VarType(y)。对于vbString,结果是8。对象应该是9。 - ssorrrell 1小时前

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

使用立即窗口中的打印y查找y的内容。 - ssorrrell 55分钟前

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

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

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.

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

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.

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

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.

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

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

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

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.

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

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

#2


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.

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

#3


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

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

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

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

#4


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.

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