Re: Can objects be passed around as in Java?

From: onedaywhen (jamiecollins_at_xsmail.com)
Date: 01/12/05


Date: 12 Jan 2005 01:37:13 -0800

peter wrote:
> Each oMACdefn contains oFUNCTdefn objects of class clsFUNCTdefn5A.
> I want to access the main line's wsWork*** object from a method
(for
> now), but I first have a problem coding the Property method. Then
the
> main line doesn't seem to pass me a good wsWork***, tho it looks
okay
> in the debugger.

A few points. When working with objects, you should use one or both of
a Property Get and a Property Set pair and use a Private (rather than
Public) object variable to retain the reference. And when assigning the
property value, you need to use the Set keyword with the = assignment.

The Class_Initialize() isn't a very useful constructor because it can
take no parameters so I usually write my own constructor (called Init)
which must be explicitly called *after* the object has been
instantiated. Not ideal but there you go. Not that I don't use the
Initialize event as well and your usage is perfectly legitimate... but
for reasons of 'encapsulation' I'd be a little uncomfortable about
using Active*** in the Initialize event of a class; I'd prefer to
pass the Active*** object from the main code. So:

' --- <clsMACdefn5A parent class> ---
Option Explicit
Private wsWork*** As Work***

Public Function Init(ByVal o*** As Work***)
Set wsWork*** = o***
End Function

Public Property Get Work***() As Work***
Set Work*** = wsWork***
End Property
' --- </clsMACdefn5A parent class> ---

' ---<clsFUNCTdefn5A child class> ---
Option Explicit
Private oParent As clsMACdefn5A

Public Property Set Parent(ByVal inn As clsMACdefn5A)
Set oParent = inn
End Property

Public Property Get Parent() As clsMACdefn5A
Set Parent = oParent
End Property
' ---</clsFUNCTdefn5A child class> ---

Sub MainCode()
Dim oMACdefn As clsMACdefn5A
Set oMACdefn = New clsMACdefn5A
oMACdefn.Init Active***

Dim oFUNCTdefn As clsFUNCTdefn5A
Set oFUNCTdefn = New clsFUNCTdefn5A
Set oFUNCTdefn.Parent = oMACdefn

End Sub

BTW As a heads up, I'm generally very wary of references (pointers) to
a parent class because it is a fine way to create a memory leak. It is
usual for an instance of a parent class to hold references to its child
objects, so if the child also holds a reference to the parent you have
a circular reference. This is a problem when it comes to destroy the
parent object: the references to the parent within the children have to
be explicitly released before the parent object can be released from
memory. This can't simply be done in the Class_Terminate event because
the Terminate event will not fire until the object is actually being
destroyed and this will not happen until the references have been
released, catch 22. So you must have a destructor routine which must be
explicitly called before trying to set the parent to Nothing. Failing
to do this will result in the object remaining in memory until the app
quits i.e. leaking memory. I prefer to use a dedicated class to provide
a reference to the parent on demand via an event but that's another
story ...

Jamie.

--

Loading