Sub property in VBA Excel class module

走远了吗. 提交于 2020-06-25 05:44:31

问题


I have a module named cTask with the code below in it:

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

So in my sub lets say I initiate

dim currtask as cTask

I would like to write

curtask.Mile=TIM

and also

curtask.Mile.stat=2

just as

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

how do I do the nested properties in my class?

EDIT: so Have in one class named 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

and in class cMile I have

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

then in my sub all i do is declare

dim curtask as cTask

Is this correct? It does not work so I must have missed something


回答1:


An example implementation of nested objects

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

Regular module:

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

What's missing from your original question is this:

 curtask.Mile=TIM

It's not clear what you meant by this: it kind of looks like a "default property" on the cMile class, but that's not really supported in VBA (or at least not easily).



来源:https://stackoverflow.com/questions/21767307/sub-property-in-vba-excel-class-module

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!