Vb Classic Object Proxy

last modified: January 31, 2009

Sometimes in VbClassic you want to get hold of an object but not keep a reference to it.

An example would be a Parent property in a collection item. If you do it by holding a reference to the parent in each member of the collection, you end up with circular referenes.

Instead you can hold a Proxy to the parent object.

http://www.vb-faq.com/Articles/Hughson/proxyobjects.asp

This link seems to be dead, here is some example code

You will need the Child, the Parent, and the Proxy Object

ParentProxy Object
--------------------------------
Option Explicit
Public Event GetRef(ByRef RHS As MyParent)

Public Function GetParent() As MyParent
   Dim Ref As MyParent
   RaiseEvent GetRef(Ref)
   Set GetParent = Ref
End Function

Child Object
----------------------
..
Private aParent as ParentProxy
..
Public Property Get Parent() as MyParent
   Set Parent = aParent.GetParent
End Property

Public Property Set Parent(RHS as MyParent)
   Set aParent as MyParent.Proxy
End Property

Parent Object
----------------------------

Private WithEvents priParentProxy as ParentProxy

Private Sub Class_Initialize()
 ..
 Set priParentProxy = New ParentProxy
 ..
End Sub

Friend Property Get Proxy() As ParentProxy
   Set Proxy = priParentProxy
End Property

Private Sub priParentProxy_GetRef(RHS As MyParent)
   Set RHS = Me
End Sub

Occasionally, you may still be forced to work in Office 97, in which case, you can't define custom events. In these cases, you have 2 alternatives that I know of:


One question: When can it happen that the proxied object is released, but you still try to reference it through the proxy? Without this knowledge, I am not certain that I dare to use this pattern. -- ThomasEyde


CategoryVbClassic


Loading...