2014-02-13 95 views
1

我有一个名为cTask用下面的代码在它模块:SUB房产

Private pMile As String 

Public Property Get Mile() As String 
Mile = pMile 
End Property 

Public Property Let Mile(Value As String) 
pMile = Value 
End Property 

所以在我的子可以说我发起

dim currtask as cTask 

我还想写

curtask.Mile=TIM 

curtask.Mile.stat=2 

就像

worksook("qqq").sheets("okko").cells(1,1)... 

我怎么做我班上嵌套的属性?

编辑: 名为cTask

Private pMile As cMile 
Public Property Get Mile() As String 
Mile = pMile 
End Property 

Public Property Let Mile(Value As String) 
pMile = Value 
End Property 

一个类,并在类cMile我

Private pstatus As String 

Public Property Get status() As String 
status = ppstatus 
End Property 

Public Property Let status(Value As String) 
pstatus = Value 
End Property 

然后在我的子我要做的就是等有申报

dim curtask as cTask 

它是否正确?它不工作,所以我一定是错过了一些东西

+3

你需要用适当的属性创建一个'clsMile'类,并且让你的'pMile'类型为clsMile而不是String。一旦'curTask.Mile'属性表示一个对象,你将需要在赋值时使用Set。 –

+0

我不知道我得到了那一切。我写了,我在编辑我的帖子 – user2385809

回答

3

示例实现嵌套的对象

cTask:

Private pMile As cMile 

Public Property Get Mile() As cMile 
    Set Mile = pMile 
End Property 

Public Property Set Mile(Value As cMile) 
    Set pMile = Value 
End Property 

Private Sub Class_Initialize() 
    Set Me.Mile = New cMile 
End Sub 

cMile:

Private pStatus As String 
Private pNumber As Long 

Public Property Get Status() As String 
    Status = pStatus 
End Property 

Public Property Let Status(Value As String) 
    pStatus = Value 
End Property 

Public Property Get Number() As Long 
    Number = pNumber 
End Property 

Public Property Let Number(Value As Long) 
    pNumber = Value 
End Property 

常规模块:

Sub Tester() 

    Dim Task As New cTask 

    Task.Mile.Status = "Done" 
    Task.Mile.Number = 11 

    Debug.Print Task.Mile.Status, Task.Mile.Number 

End Sub 

什么是从你原来的问题缺少的是这样的:

curtask.Mile=TIM 

目前尚不清楚,你这个是什么意思:它看起来像在cMile类中的“默认属性”,但在VBA中并没有真正支持(或者至少不太容易)。

+0

这工作很好。对于TIM很抱歉。 TIM ans Status与我刚刚将其从原始端口更改为偶然编辑相同。为了我的理解Private Sub Class_Initialize()做了什么。当一英里或一英里时,它会清空cMile? – user2385809

+0

以及为什么sis使用set而不是让Mile去做?非常感谢你btw我一直在这里工作一段时间 – user2385809

+0

在VBA中,当分配对象类型属性或变量的值时,使用'Set'。当创建'cTask'的实例时''Class_Initialize'自动运行,并将'Mile'属性设置为'cMile'的实例。 –