singleton classes !!

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

Guest

I want to make a class that :

- will have one and only one object
- when you try to make another object of this class , an IDE error should be
seen . like using undefined variable , or any thing like that ..
 
Hadidi,

The first part is easy, and I'm sure you know the singleton pattern, so
there is no reason to repeat it here.

As for the second part, that's nearly impossible. If you are asking for
an IDE error, you are asking for a compile-time error, and there is no way
to do something like this. The best you could do is settle for a run time
error.

However, if you implement the singleton correctly, you will end up
making your constructor private, and you will have definite control over the
lifetime of the instance, and won't have to worry about someone else
creating the object.

Hope this helps.
 
User defined compile time errors can be generated with #error directive. I
don't think this will help the OP

Nicholas Paldino said:
Hadidi,

The first part is easy, and I'm sure you know the singleton pattern, so
there is no reason to repeat it here.

As for the second part, that's nearly impossible. If you are asking
for an IDE error, you are asking for a compile-time error, and there is no
way to do something like this. The best you could do is settle for a run
time error.

However, if you implement the singleton correctly, you will end up
making your constructor private, and you will have definite control over
the lifetime of the instance, and won't have to worry about someone else
creating the object.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Hadidi said:
I want to make a class that :

- will have one and only one object
- when you try to make another object of this class , an IDE error should
be
seen . like using undefined variable , or any thing like that ..
 

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

Back
Top