Is it possible to access a parent property from a child that is in a collection?

后端 未结 1 378
时光取名叫无心
时光取名叫无心 2021-02-06 12:44

I\'ve researched as much as I can and never found a definitive answer on this for VBA.

This older StackOverflow post has almost everything, but not quite. VBA Classes

1条回答
  •  感情败类
    2021-02-06 13:08

    If you figure out how to access the kernel memory to do this, let me know. Take a look at the source code of vbWatchDog for some hints. I have been studying it to try to gain access to the call stack. I haven't figured it out yet.

    I'll show you how to fake it though. I'm going to simplify this a bit. You'll need to apply the principle to your own code. The trick is kind of ugly. It requires that we call an Initialize routine each time we create a new child object

    The Parent Class:

    'Class Parent
    Option Explicit
    
    Private mName as String
    Public Property Get Name() as String
        Name = mName()
    End Property
    
    Public Property Let Name(value As String)
        mName = value
    End Property
    

    The Child class

    'Class Child
    Option Explicit
    
    Private mParent as Parent    
    
    Public Property Get Parent() as Parent
        Set Parent = mParent
    End Property
    
    Public Property Let Name(Obj as Parent)
        Set mParent = Obj
    End Property
    
    Public Sub Initialize(Obj as Parent)
        Set Me.Parent = Obj
    End Sub
    

    Creating a Child object:

    Sub CreateChild()
        Dim parentObject As New Parent
        ' create child object with parent property
        Dim childObject As New Child
        childObject.Initialize(parentObject)
    End Sub
    

    0 讨论(0)
提交回复
热议问题