我創建了兩個類作爲接口/實現類,並希望將一個類的特定示例傳遞給另一個類中的方法。該定義如下...如何將對象傳遞給VB6中的其他類中的方法
類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的調用完美。有誰知道我做錯了什麼,我需要做什麼來解決這個問題?
您的任何參數都不需要是「ByRef」,我個人發現它們具有誤導性,例如,我的調用代碼將不得不測試通過`ByRef`的引用在調用之後是否指向另一個對象。但如果它是'ByVal',我將能夠信任被調用者。 – onedaywhen 2011-02-11 15:57:02
@onedaywhen,好點;謝謝你。 – 2011-02-11 16:06:12