473,609 Members | 2,145 Online
Bytes | Software Development & Data Engineering Community
+ 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 1193
Like add a handler when the object is created?
"Rob Nicholson" <rob.nicholson@ nospam_unforget table.com> wrote in message
news:V1******** *********@newsf ep4-winn.server.ntl i.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_unforget table.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_unforget table.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.WeakRefe rence, 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*******@free net.de> wrote in message
news:%2******** ********@TK2MSF TNGP11.phx.gbl. .. "Herfried K. Wagner [MVP]" <hi************ ***@gmx.at> schrieb
* "Rob Nicholson" <rob.nicholson@ nospam_unforget table.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.WeakRefe rence, 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.WeakRefe rence 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*******@free net.de> wrote in message
news:O4******** ******@TK2MSFTN GP10.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.WeakRefe rence, 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.WeakRefe rence 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(ne w MyClass)

Where CreateObject is a global function but that requires the programmer to
remember.
Ah, ok, didn't know the System.WeakRefe rence 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
2096
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 object of that class in stack. So, is there a trick to avoid stack creation of an object of this class? I mean, to take compile-time error instead of runtime. I want to create this class object only in heap. Thanks.
8
3367
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 functional equivalent of the following code: Public Sub CloseRecordset() On Error GoTo Sub_Error Dim rs As Recordset rs.FindFirst "This Will Error"
3
4610
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 the user has to use my New Record button. When you click the New Record button, the form presents a new record for editing. My client wants to use the Escape key to cancel changes to new or existing records. On existing records, Access already...
13
4465
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 runs into problems on the system where it will actually be used, and since I used so little error-trapping it dies very ungracefully. I will of course try to fix whatever is causing the error and add error-trapping to the functions where the...
12
13726
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 functions. Now, in the lib is a 'destroy' function which when called will release all instances of DirectSound that I have created and if that is used, then great, no problem. The application that uses this lib is a console application running on...
8
3445
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 COM objects - or, just use the interactive user If they choose to use the interactive option, the impersonate="true" means that the process runs under the interactiv user (which I've confirmed works correctly). If they specify a userid/password, I...
6
1471
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" defaultRedirect="WebForm1.aspx"> <error statusCode="404" redirect="ServerError.aspx"></error> <error statusCode="500" redirect="WebForm3.aspx"></error>
40
2155
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 errors in small snippets of code but I was wondering if anyone could give me tips on what kind of things I should be looking out for. The one area I dont feel confident in is how to declare arrays of pointers and initialising multi-dimensional arrays....
42
3855
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
8127
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, well explore What is ONU, What Is Router, ONU & Routers main usage, and What is the difference between ONU and Router. Lets take a closer look ! Part I. Meaning of...
0
8567
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8527
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8398
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6053
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupr who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5509
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4015
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4076
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1658
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.