Object Instantiation check ...HOW

M

Maheshkumar.R

Hi groups,

(1) How i can check whether the object is created or not. Let me say...during runtime i'm creating 'n' objects with unique name. I have to check whether object has been instantiated or not.

(2) What are the possible ways are there to check this object instantiation..?

(3) I want to dispose if it is instantiated else intantiate new object..? so how to dispose and check the same..?



Thanks.
 
J

Jon Skeet [C# MVP]

Maheshkumar.R said:
(1) How i can check whether the object is created or not. Let me
say...during runtime i'm creating 'n' objects with unique name. I
have to check whether object has been instantiated or not.

(2) What are the possible ways are there to check this object
instantiation..?

(3) I want to dispose if it is instantiated else intantiate new
object..? so how to dispose and check the same..?

That sounds like you basically need a hashtable mapping the name to the
object. If there's an entry in your hashtable when you want to create a
new object, then remove it, dispose it, then create the new one and put
that in the table.
 
M

Maheshkumar.R

You redefined by problem in different way..? but i'm looking few guidelines
or codes to be followed. Would anyone please share fewlines of code for the
same..??

Mahesh
 
J

Jon Skeet [C# MVP]

Maheshkumar.R said:
You redefined by problem in different way..? but i'm looking few guidelines
or codes to be followed. Would anyone please share fewlines of code for the
same..??

Well, it's pretty simple:

Hashtable table = new Hashtable();

then:

MyClass CreateNewInstance (string name)
{
MyClass old = (MyClass) table[name];
if (old != null)
{
old.Dispose();
}
MyClass ret = new MyClass(name);
table[name] = ret;
return ret;
}

(Note that this isn't thread-safe - if you need to do this from
multiple threads, you'll need locking.)
 
S

S. Senthil Kumar

"runtime i'm creating 'n' objects with unique name."
What does that mean? Are you passing unique names to the constructors
of the object? Or are you referring to variable names?

If you simply want to know how many objects have been created, you can
simply have a static count variable that is incremented everytime the
constructor runs.

"(3) I want to dispose if it is instantiated else intantiate new
object..? so how to dispose and check the same..?"

Simply check if the reference is null, if it's not null, call Dispose,
otherwise create a new object.

Regards
Senthil
 
S

shiv_koirala

How i can check whether the object is created or not. Let me
say...during runtime i'm creating 'n' objects with unique name. I have
to check whether object has been instantiated or not.

to check whether object is instantiated or not is by

if (object1 != null)
{
}

What are the possible ways are there to check this object
instantiation..?

I know one as said above

I want to dispose if it is instantiated else intantiate new object..?
so how to dispose and check the same..?

Implement the Idisposable interface and write your clean up routine in
that

Can happen that i would have misunderstood your question

Shivprasad Koirala
C# , VB.NET , SQL SERVER , ASP.NET Interview Questions
http://www.geocities.com/dotnetinterviews/
 
J

Jimbo

Jon said:
Maheshkumar.R said:
You redefined by problem in different way..? but i'm looking few guidelines
or codes to be followed. Would anyone please share fewlines of code for the
same..??


Well, it's pretty simple:

Hashtable table = new Hashtable();

then:

MyClass CreateNewInstance (string name)
{
MyClass old = (MyClass) table[name];
if (old != null)
{
old.Dispose();
}
MyClass ret = new MyClass(name);
table[name] = ret;
return ret;
}

(Note that this isn't thread-safe - if you need to do this from
multiple threads, you'll need locking.)

Personally I'd then stick that Hashtable and code into a Singleton so
that only one list of objects exists for application.

Jimbo
 
J

Jon Skeet [C# MVP]

Jimbo said:
Personally I'd then stick that Hashtable and code into a Singleton so
that only one list of objects exists for application.

That really depends on the situation. You could well want to have
several of these tables - one per web request in a web service, for
instance. We don't know enough about the OP's needs to say, really.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top