Trapping creation of a new object

  • Thread starter Thread starter Rob Nicholson
  • Start date Start date
R

Rob Nicholson

Is it possible to trap the creation of a new object and carry out other
operations after it's been created? For example, if creating an object of a
specific type, then add a reference to a global collection.

Thanks, Rob.
 
* "Rob Nicholson said:
Is it possible to trap the creation of a new object and carry out other
operations after it's been created? For example, if creating an object of a
specific type, then add a reference to a global collection.

You can do that in the object's constructor.
 
Armin,
I thought this too, but how/when to remove it from the collection?
Implement the Disposable pattern and remove it in the Dispose method, if the
user neglects calling the Dispose method, remove it in the Finalize method.

Of course the global collection will need to be a collection of
System.WeakReference, the WeakReference will refer to the actual object.
Thus the last reference to the actual object will cause the WeakReference to
return Nothing for this entry. The indexer for the global collection can
check for Nothing from the WeakReference and remove the entry for the
WeakReference from the collection....

Hope this helps
Jay
 
Jay B. Harlow said:
Armin,
Implement the Disposable pattern and remove it in the Dispose method,
if the user neglects calling the Dispose method, remove it in the
Finalize method.

Yes, but Dispose must be called manually, that's why I thought it's not what
the OP was looking for.
Of course the global collection will need to be a collection of
System.WeakReference, the WeakReference will refer to the actual
object. Thus the last reference to the actual object will cause the
WeakReference to return Nothing for this entry. The indexer for the
global collection can check for Nothing from the WeakReference and
remove the entry for the WeakReference from the collection....

Ah, ok, didn't know the System.WeakReference thing. Will look into it....
 
Armin,
Yes, but Dispose must be called manually, that's why I thought it's not what
the OP was looking for.
Its probably not what the OP is looking for! ;-) However, remember there is
no real deterministic finalization in .NET, and once the collection has a
"hard" reference to the object that the finalizer or the VB6 Terminate event
"cannot" occur, which is where the .NET WeakReference is rather cool! I
mentioned both (WeakReference & Dispose) as I would probable implement both.

I should add:The GC will set the reference in the WeakReference to nothing if the GC
needs to do a garbage collection, assuming there are no other references to
the object. If there is not garbage collection the WeakReference will
maintain the reference when there are no other references to the object...
Also WeakReference supports a long & short weak references, which allow an
object heading to the Finalization queue to be revived.

I have a project where I am implementing the domain model equivalent of the
DataTable & DataView classes, my plan is that my Domain collection will
maintain a weakreference to its default "View" object, this way when the
view object is in use it will stick around, when the View object is not in
use, it can will quietly disappear... And there is no real extra code in my
logic.

Note WeakReferences are also useful for semi-persistent Singletons. A
singleton that exists while other objects refer to it, however if no other
objects refer to it, and the GC needs memory the Singleton based on a
WeakReference can disappear... In this regard I've considered it in a
special plug-in architecture in a project I use to practice Refactoring
http://www.refactoring.com.

O.K. too much info ;-)

Hope this helps
Jay
 
Like add a handler when the object is created?

Yes, that's the kind of thing. I seem to recall something similar was
possible in C++

Cheers, Rob.
 
You can do that in the object's constructor.

I don't want to add specific code to each object definition.

Cheers, Rob.
 
Yes, but Dispose must be called manually, that's why I thought it's not
what
the OP was looking for.

Correct - I'm looking for something transparent so that the programmer
doesn't have to worry about it. I could insist that all objects are
created/destroyed via another function like this:

Dim MyObj As MyClass = CreateObject(new MyClass)

Where CreateObject is a global function but that requires the programmer to
remember.
Ah, ok, didn't know the System.WeakReference thing. Will look into it....

I'll check this too.

Thanks, Rob.
 
Back
Top