2012-09-14 57 views
1

我有一個實現另一個對象的類。我爲已實現的對象的每個屬性設置了一個屬性函數,但不斷收到'無效的屬性使用'錯誤。這裏是我的代碼:從接口實現getter和setter時的問題

測試子:

Sub tst() 

Dim a As Derived 

Set a = New Derived 

a.Base_name = "ALGO" 'Error happens when this executes 
End Sub 

派生類模塊:

Option Explicit 
Implements Base 
Private sec As Base 
Private Sub Class_Initialize() 
    Set sec = New Base 
End Sub 
Public Property Get Base_name() As String 
    Call sec.name 
End Property 
Public Property Let Base_name(value As String) 
    Call sec.name(value) 'Error happens here 
End Property 

基類模塊:

Private pname As String 

Public Property Get name() As String 
    name = pname 
End Property 
Public Property Let name(value As String) 
    pname = value 
End Property 
+0

上的 「私人秒作爲基」 爲我的錯誤發生。你應該調用Let而不是給吸氣劑分配「ALGO」,不是?試試a.Base_name(「ALGO」)。順便說一下,派生的是什麼? – fabiopagoti

+0

我打電話給let,如果我做了a.Base_name(「ALGO」),我在sub甚至運行之前得到了屬性的無效使用。 Derived是一個繼承基類的類。 – postelrich

+0

我有點困惑。你能否編輯你的帖子,包括基地?我在基地添加了 – fabiopagoti

回答

3

這是你想要的嗎?

模塊1

Sub tst() 

Dim a As Derived 

Set a = New Derived 

Debug.Print a.Base_name 
a.Base_name = "ALGO" 
Debug.Print a.Base_name 
End Sub 

基類模塊

Private pname As String 

Public Property Get name() As String 
    name = pname 
End Property 
Public Property Let name(value As String) 
    pname = value 
End Property 

派生類模塊

Option Explicit 
Implements Base 
Private sec As Base 
Private Sub Class_Initialize() 
    Set sec = New Base 
End Sub 
Public Property Get Base_name() As String 
    Base_name = sec.name 
End Property 
Public Property Let Base_name(value As String) 
    sec.name = value 
End Property 
+0

令人敬畏,我發現我沒有爲派生類中的get函數返回任何內容。我明白如何正確使用它。謝謝! – postelrich