Click here to Skip to main content
15,925,723 members
Home / Discussions / C#
   

C#

 
GeneralSingleton component design problem Pin
leppie20-Feb-03 6:24
leppie20-Feb-03 6:24 
Hi
This one has me completely confused. What is the correct way of doing this? I have some "rules" I want to stick with.

- Designer support
- Object must be a singleton

Now these 2 are conflicting Hmmm | :|

Here's my possible solution:

public class Singleton : Component
{
   private static SingletonInternal instance = null;
   private static int instancecount = 0;

   private class SingletonInternal : IDisposable
   {
      //just the normal "instance" implementation

      public void SomeMethod()
      {
      }

      public void Dispose()
      {
         // free unmanaged resources
      }
   }

   public Singleton()
   {
      if (instance == null)
         instance = new SingletonInternal();
      instancecount++;
   }

   public void SomeMethod()
   {
      instance.SomeMethod();
   }
   
   protected override void Dispose(bool disposing)
   {
      if (!this.disposed)
      {
         if (disposing)
         {
            // free managed resources
         }
         instancecount--;
         if (instancecount == 0)
            instance.Dispose();

         this.disposed = true;
      }
   }
}

Is this the way to go? All comments welcome Smile | :)

Cheers

MyDUMeter: a .NET DUMeter clone
"Thats like saying "hahahaha he doesnt know the difference between a cyberneticradioactivenuclothermolopticdimswitch and a biocontainingspherogramotron", but with words you have really never heard of."
GeneralRe: Singleton component design problem Pin
pete mcquain20-Feb-03 7:24
pete mcquain20-Feb-03 7:24 
GeneralRe: Singleton component design problem Pin
leppie20-Feb-03 8:04
leppie20-Feb-03 8:04 
GeneralWebBrowser control displaying security alert dialogs Pin
GriffonRL20-Feb-03 5:35
GriffonRL20-Feb-03 5:35 
GeneralRe: WebBrowser control displaying security alert dialogs Pin
Stephane Rodriguez.20-Feb-03 5:44
Stephane Rodriguez.20-Feb-03 5:44 
GeneralRe: WebBrowser control displaying security alert dialogs Pin
GriffonRL20-Feb-03 5:57
GriffonRL20-Feb-03 5:57 
GeneralRe: WebBrowser control displaying security alert dialogs Pin
Stephane Rodriguez.20-Feb-03 6:35
Stephane Rodriguez.20-Feb-03 6:35 
GeneralLogin & Application Form Pin
vladmihai20-Feb-03 5:16
vladmihai20-Feb-03 5:16 
GeneralRe: Login & Application Form Pin
Nnamdi Onyeyiri20-Feb-03 5:34
Nnamdi Onyeyiri20-Feb-03 5:34 
GeneralRe: Login & Application Form Pin
Chris Austin20-Feb-03 5:35
Chris Austin20-Feb-03 5:35 
GeneralRe: Login & Application Form Pin
vladmihai20-Feb-03 8:49
vladmihai20-Feb-03 8:49 
GeneralRe: Login & Application Form Pin
Chris Austin20-Feb-03 9:16
Chris Austin20-Feb-03 9:16 
GeneralThe chicken-and-egg problem. OO HELP!! Pin
~toki20-Feb-03 4:01
~toki20-Feb-03 4:01 
GeneralRe: The chicken-and-egg problem. OO HELP!! Pin
Nnamdi Onyeyiri20-Feb-03 5:44
Nnamdi Onyeyiri20-Feb-03 5:44 
QuestionHow to know which version of Microsoft Office are installed Pin
Hawkmoon19-Feb-03 23:34
Hawkmoon19-Feb-03 23:34 
AnswerRe: How to know which version of Microsoft Office are installed Pin
Stephane Rodriguez.20-Feb-03 5:39
Stephane Rodriguez.20-Feb-03 5:39 
GeneralRe: How to know which version of Microsoft Office are installed Pin
Hawkmoon20-Feb-03 23:25
Hawkmoon20-Feb-03 23:25 
QuestionHow to display a window form inside another form Pin
faad19-Feb-03 19:57
faad19-Feb-03 19:57 
AnswerRe: How to display a window form inside another form Pin
Chris Austin20-Feb-03 3:45
Chris Austin20-Feb-03 3:45 
GeneralRe: How to display a window form inside another form Pin
faad20-Feb-03 17:42
faad20-Feb-03 17:42 
GeneralRe: How to display a window form inside another form Pin
Chris Austin20-Feb-03 18:32
Chris Austin20-Feb-03 18:32 
GeneralThis is a dump question. Pin
Braincrash19-Feb-03 14:25
Braincrash19-Feb-03 14:25 
GeneralRe: This is a dump question. Pin
jtmtv1819-Feb-03 19:31
jtmtv1819-Feb-03 19:31 
GeneralRe: This is a dump question. Pin
Braincrash20-Feb-03 13:33
Braincrash20-Feb-03 13:33 
GeneralDesigner doesn't delete code Pin
crosenbury19-Feb-03 10:53
crosenbury19-Feb-03 10:53 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.