如何将对象传递给VB6中其他类的方法

时间:2011-02-11 13:23:08

标签: class vb6

我创建了两个类作为接口/实现类,并希望将一个类的特定示例传递给另一个类中的方法。定义如下......

Class BigInt ......

Option Explicit
Public Sub dothing(ByRef passed_object As MyInt)
End Sub

并实现BigImplementation ...

Option Explicit
Implements BigInt

Public Sub BigInt_dothing(ByRef passed_obj As MyInt)
Dim i As Integer
i = passed_obj.getprop
End Sub

我打算通过的课程是......

Option Explicit

Public Property Get getprop() As Integer
End Property

Public Property Let letprop(ByVal myval As Integer)
End Property

实现为MyImplementation因此......

Option Explicit
Implements MyInt
Private myval As Integer

Public Property Get myint_getprop() As Integer
myint_getprop = myval
End Property

Public Property Let myint_letprop(ByVal passed_int As Integer)
myval = passed_int
End Property

然后我使用以下代码片段来驱动它: -

Private Sub Command_Click()
Dim myobj As MyInt
Set myobj = New MyImplementation
Dim mybigobj As BigInt
Set mybigobj = New BigImplementation

myobj.letprop = 1
Call mysub(myobj)
mybigobj.dothing (myobj)   ' Line with problem
End Sub

Private Sub mysub(ByVal passed_obj As MyInt)
Dim i As Integer
i = passed_obj.getprop
End Sub

当执行到达标记的行时,我得到运行时错误438 - 对象不支持属性或方法。对普通函数mysub的调用非常有效。有谁知道我做错了什么以及我需要做些什么来解决这个问题?

2 个答案:

答案 0 :(得分:6)

使用

mybigobj.dothing myobj

Call mybigobj.dothing(myobj)

在引用周围加上额外的括号会计算其默认属性,并将其值作为实际参数传递。

答案 1 :(得分:0)

mybigobj.dothing要求其参数为MyInt

Public Sub BigInt_dothing(ByRef passed_obj As MyInt)
Dim i As Integer
i = passed_obj.getprop
End Sub

您正在传递MyImplementation

Set myobj = New MyImplementation

所以可能就像(我的vb生锈了)

mybigobj.dothing (myobj.myint_getprop())