如何将对象传递给VB6中其他类中的方法 [英] How to pass objects to methods in other classes in VB6

查看:77
本文介绍了如何将对象传递给VB6中其他类中的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have created two classes as interface / implementation classes, and wish to pass a particular example of one class to a method in the other. The definitions are as follows...

BigInt类...

Class BigInt...

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

和实施BigImplementation ...

and an implementation BigImplementation...

Option Explicit
Implements BigInt

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

我计划通过的课程是...

The class I am planning to pass is...

Option Explicit

Public Property Get getprop() As Integer
End Property

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

因此实现为MyImplementation ...

implemented as MyImplementation thus...

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

然后使用以下代码段来驱动此操作:-

I am then driving this with the following snippet of code:-

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的调用效果很好。有人知道我在做什么错,以及我需要怎么做才能解决此问题吗?

When the execution reaches the line marked, I get run-time error 438 - Object doesn't support property or method. The call to the ordinary function mysub works perfectly. Does anyone know what I am doing wrong and what I need to do to fix this?

推荐答案

使用其中一个

mybigobj.dothing myobj

Call mybigobj.dothing(myobj)

在引用周围加上多余的括号会评估其默认属性,并将其值作为实际参数传递。

Putting extra parentheses around a reference evaluates its default property and passes it's value as actual argument.

这篇关于如何将对象传递给VB6中其他类中的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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