VbClassicObjectProxy

Last edit 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:

  • Have a single container object around one or more objects in your cyclic graph that is not, itself part of a cycle. When this object goes out of scope, it calls methods of the objects it contains to tell them to drop their references to other objects in the network, so cyclical references are broken.
  • Use a non-visible Form object as an event messenger. You can force the Change event handler of a control on the form to fire by changing its Text property (works in MicrosoftAccess anyway).


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