disposing object

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

hello everyone,
How can i tell how many instances of a Class have i created with successive
uses of the 'new' keyward in C#?

cheers

Zest
 
Zest4Csharp said:
hello everyone,
How can i tell how many instances of a Class have i created with successive
uses of the 'new' keyward in C#?

cheers

Zest

No in-built way, you could have a static creation count but why?

JB
 
Thanks John,
I think i should have explained the source of the problem which i will do now.
Ok, here is the scenario i am dealing with: I have Class1 creating an
instance of Class2 as in the following line of code:

Class2 MyChild = new Class2;

Class2 is defined in an exteranl DLL written with C# as well.

Now, MyChild will prompt the user for user id (which is a public property in
Class2). I want Class1 to be 'aware' that MyChild has captured the required
info before it can go and do other stuff.

I was thinking that if i can get an 'array' of the newly created objects
then i can inspect them on at a time to see if the information was catured.
Otherwise how will Class1 'know' what has happened to Class2?

I hope i am making sense now!

Cheers
Zest
===
 
Zest4Csharp said:
I think i should have explained the source of the problem which i will do now.
Ok, here is the scenario i am dealing with: I have Class1 creating an
instance of Class2 as in the following line of code:

Class2 MyChild = new Class2;

Class2 is defined in an exteranl DLL written with C# as well.

Now, MyChild will prompt the user for user id (which is a public property in
Class2). I want Class1 to be 'aware' that MyChild has captured the required
info before it can go and do other stuff.

I was thinking that if i can get an 'array' of the newly created objects
then i can inspect them on at a time to see if the information was catured.
Otherwise how will Class1 'know' what has happened to Class2?

Perhaps you should give Class2 an event which Class1 could subscribe
to. Then when Class2 has done whatever it needs to do, it could raise
the event to tell Class1 that.
 
hi John,
Thanks mate for your reply. What you mentioned is exaclty what i need. The
question now is: can someone please give me some pointers on where to find
literature about building an event in Class1 that will fire when an object
created off Class2 'dies'

Thanks for your kind assistance
Cheers

Zest
==
 
Zest4Csharp said:
Thanks mate for your reply. What you mentioned is exaclty what i need. The
question now is: can someone please give me some pointers on where to find
literature about building an event in Class1 that will fire when an object
created off Class2 'dies'

You can't do that (and you'd want the event in Class2 anyway, with
Class1 subscribing to it) - but I thought it was a case of Class2
capturing the required information? That's very different from Class2
"dying".
 
hi jon,
The scenrio is that Class1 is the main application start point. Class2 will
be then fired. Class2 is where the user is promtped to enter database login
info. Once the connection to the database is done successfully, i want Class2
to dispose itself. But before its gone, i want Class1 to 'know' that Class2
is about to be disposed and 'grabs' the database connection object
(OleDbConnection) to then access the database.
You see, the aim of the whole exercise is to modularise the login process
and not have to repeat building the login form in each and every project i do.

I hope this further clarifies my delimma !

zest
====
 
Zest4Csharp said:
The scenrio is that Class1 is the main application start point. Class2 will
be then fired. Class2 is where the user is promtped to enter database login
info. Once the connection to the database is done successfully, i want Class2
to dispose itself. But before its gone, i want Class1 to 'know' that Class2
is about to be disposed and 'grabs' the database connection object
(OleDbConnection) to then access the database.
You see, the aim of the whole exercise is to modularise the login process
and not have to repeat building the login form in each and every project i do.

I hope this further clarifies my delimma !

So at the point where Class2 is about to dispose of itself, you raise
an event that you define in Class2. Class1 will have subscribed to it,
and can react appropriately.
 
jon,
Spot on ! Ok now this narrows my search down to finding the C# documentation
that describes this subscription mechanism. Pls note that i am new to boh OO
and C# so its quite a steep learnng curve for me :)

Thanks a lot for you assistance again
 
Zest4Csharp said:
Spot on ! Ok now this narrows my search down to finding the C# documentation
that describes this subscription mechanism. Pls note that i am new to boh OO
and C# so its quite a steep learnng curve for me :)

I suggest you could start with the MSDN tutorial (watch for wrap):

http://msdn.microsoft.com/library/default.asp?url=/library/en-
us/csref/html/vcwlkEventsTutorial.asp

Do you have a book to learn C# from? Learning it from the docs can be
quite difficult sometimes...
 
Actually i was trying to learn from the Docs i find on the MSDN website. but
i reckon i need a hardcopy so may as well get myself a book.

Ok, my program scenario is now deviating from what i started with. So
probably i will post a new message so that this thread doesnt get exceedingly
long !

And thanks jon for you patience !

Zest
=======
 
Back
Top