By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,327 Members | 849 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,327 IT Pros & Developers. It's quick & easy.

Trapping creation of a new object

P: n/a
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.
Nov 20 '05 #1
Share this Question
Share on Google+
9 Replies


P: n/a
Like add a handler when the object is created?
"Rob Nicholson" <rob.nicholson@nospam_unforgettable.com> wrote in message
news:V1*****************@newsfep4-winn.server.ntli.net...
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.

Nov 20 '05 #2

P: n/a
* "Rob Nicholson" <rob.nicholson@nospam_unforgettable.com> scripsit:
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.

--
Herfried K. Wagner [MVP]
<http://www.mvps.org/dotnet>
Nov 20 '05 #3

P: n/a
"Herfried K. Wagner [MVP]" <hi***************@gmx.at> schrieb
* "Rob Nicholson" <rob.nicholson@nospam_unforgettable.com>
scripsit:
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.


I thought this too, but how/when to remove it from the collection?
--
Armin

Nov 20 '05 #4

P: n/a
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

"Armin Zingler" <az*******@freenet.de> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl... "Herfried K. Wagner [MVP]" <hi***************@gmx.at> schrieb
* "Rob Nicholson" <rob.nicholson@nospam_unforgettable.com>
scripsit:
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.


I thought this too, but how/when to remove it from the collection?
--
Armin

Nov 20 '05 #5

P: n/a
"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> schrieb
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.


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

http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html

Nov 20 '05 #6

P: n/a
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:
Thus the last reference to the actual object will cause the
WeakReference to return Nothing for this entry.

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

"Armin Zingler" <az*******@freenet.de> wrote in message
news:O4**************@TK2MSFTNGP10.phx.gbl... "Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> schrieb
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.


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

http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html

Nov 20 '05 #7

P: n/a
> 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.
Nov 20 '05 #8

P: n/a
> You can do that in the object's constructor.

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

Cheers, Rob.
Nov 20 '05 #9

P: n/a
> 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.
Nov 20 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.