2012-09-14 23 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