473,486 Members | 2,359 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Trapping creation of a new object

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
9 1184
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
* "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
"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
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
"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
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
> 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
> 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
> 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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
2086
by: | last post by:
I have a class with overloading operator new. (Because, if an identical object exists, return a pointer to existed object instead of a new pointer) It has no sense (it is dangerous) to allocate an...
8
3353
by: Pete | last post by:
I'm trying to improve my code so that when I open a recordset object, I can absolutely guarantee it is closed and is set = Nothing. I have read some old threads and they all say to use the...
3
4591
by: Paul | last post by:
I have an Access 2000 database with a form that is giving me some major headaches. When you open the form, it displays all records and allows editing, but has AllowAdditions set to False so that...
13
4430
by: Thelma Lubkin | last post by:
I use code extensively; I probably overuse it. But I've been using error trapping very sparingly, and now I've been trapped by that. A form that works for me on the system I'm using, apparently...
12
13702
by: Wurm | last post by:
Hi all, Got a little problem with a lib that I'm working on. First of all, the problem is that the lib that I have makes connections to DirectSound, and has a host of custom built sound related...
8
3436
by: Anthony Munter | last post by:
I have a web application with impersonate=”true” in Web.config and on my own logon page I allow the user to either - specify a userid/password for the app to impersonate when calling legacy...
6
1461
by: SMG | last post by:
Hi , Sory for incomplete message in last post here is the actual problem.. I am using following code in web.confiig for trapping all the error through out my site.. <customErrors mode="On"...
40
2116
by: Allan M. Bruce | last post by:
I am applying for my first jobs after completing my PhD. I have been asked by a company to go and take a C programming test to see how my C skills are. Apparantly this test is mostly finding...
42
3808
by: smerf | last post by:
Using javascript, is there a way to trap an external page inside a frame? I've seen scripts to break out of frames, but nothing to keep a page trapped in a frame.
0
6964
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7123
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7175
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6842
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7319
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5430
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing,...
0
4559
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3069
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
262
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.