2014-02-13 48 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'的實例。 –