Click here to Skip to main content
15,888,351 members
Home / Discussions / C#
   

C#

 
GeneralRe: Help with simple user control Pin
Heath Stewart8-Dec-03 9:25
protectorHeath Stewart8-Dec-03 9:25 
GeneralRe: Help with simple user control Pin
Charlie Williams8-Dec-03 11:56
Charlie Williams8-Dec-03 11:56 
QuestionDynamic Table Creation on Database Using SQL Datatypes from a Dataset, Possible? Pin
Paul Evans8-Dec-03 6:46
Paul Evans8-Dec-03 6:46 
AnswerRe: Dynamic Table Creation on Database Using SQL Datatypes from a Dataset, Possible? Pin
Heath Stewart8-Dec-03 7:01
protectorHeath Stewart8-Dec-03 7:01 
GeneralRe: Dynamic Table Creation on Database Using SQL Datatypes from a Dataset, Possible? Pin
Paul Evans8-Dec-03 7:19
Paul Evans8-Dec-03 7:19 
GeneralRe: Dynamic Table Creation on Database Using SQL Datatypes from a Dataset, Possible? Pin
Heath Stewart8-Dec-03 9:04
protectorHeath Stewart8-Dec-03 9:04 
GeneralThread-Safe Singleton Pin
bzurer8-Dec-03 4:18
bzurer8-Dec-03 4:18 
GeneralRe: Thread-Safe Singleton Pin
Heath Stewart8-Dec-03 5:07
protectorHeath Stewart8-Dec-03 5:07 
statics are initialized with the Type, but the above code won't get you a singleton. The code below is oft-used and recommended by Microsoft for the .NET Framework:
public sealed class Singleton
{
  private static Singleton instance;
 
  private Singleton() {}
  // Makes sure that only one instance exists.
  private static Singleton Instance
  {
    get
    {
      // Thread-safe initialization.
      if (instance == null)
        lock (typeof(Singleton))
          if (instance == null)
            instance = new Singleton();
      return instance;
    }
  }
  private void doSomething()
  {
    Console.WriteLine("Something done.");
  }
  public static void DoSomething()
  {
    Instance.doSomething();
  }
}
Notice how the public methods are static? In most cases, this is what you want because you control what the caller can do. The Instance property is a thread-safe initializer that makes sure that only one instance is created. Your public static methods use that property (which will initialize the class instance if necessary) to call private instance methods or use private instance properties or fields. That is a singleton.

Keep in mind that the methods themselves are not thread-safe this way, though. To do that, you'll either want to use the lock keyword (on a static object, like the typeof(Singleton) I used, or this for instance methods / property accessors).

Now, you can accomplish this another way by using a ContextBoundObject where you can intercept calls but to the caller - it's just creating a new instance (where you return an existing instance). This gets into remoting issues which are more advanced, so you should probably try this out first. It's by far easier and accomplishes the same task.

 

-----BEGIN GEEK CODE BLOCK-----
Version: 3.21
GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++
-----END GEEK CODE BLOCK-----
GeneralRe: Thread-Safe Singleton Pin
Paul Evans8-Dec-03 6:37
Paul Evans8-Dec-03 6:37 
GeneralRe: Thread-Safe Singleton Pin
Heath Stewart8-Dec-03 6:56
protectorHeath Stewart8-Dec-03 6:56 
GeneralRe: Thread-Safe Singleton Pin
Paul Evans8-Dec-03 7:05
Paul Evans8-Dec-03 7:05 
GeneralRe: Thread-Safe Singleton Pin
Heath Stewart8-Dec-03 7:12
protectorHeath Stewart8-Dec-03 7:12 
GeneralRe: Thread-Safe Singleton Pin
Paul Evans8-Dec-03 7:20
Paul Evans8-Dec-03 7:20 
GeneralRe: Thread-Safe Singleton Pin
Heath Stewart8-Dec-03 10:08
protectorHeath Stewart8-Dec-03 10:08 
GeneralRe: Thread-Safe Singleton Pin
bzurer8-Dec-03 7:07
bzurer8-Dec-03 7:07 
GeneralRe: Thread-Safe Singleton Pin
Heath Stewart8-Dec-03 7:10
protectorHeath Stewart8-Dec-03 7:10 
GeneralRe: Thread-Safe Singleton Pin
bzurer8-Dec-03 7:56
bzurer8-Dec-03 7:56 
GeneralRe: Thread-Safe Singleton Pin
Heath Stewart8-Dec-03 9:14
protectorHeath Stewart8-Dec-03 9:14 
GeneralRe: Thread-Safe Singleton Pin
Zhang Ye Fan8-Dec-03 7:33
Zhang Ye Fan8-Dec-03 7:33 
GeneralRe: Thread-Safe Singleton Pin
Heath Stewart8-Dec-03 9:08
protectorHeath Stewart8-Dec-03 9:08 
GeneralRe: Thread-Safe Singleton Pin
bzurer11-Dec-03 3:31
bzurer11-Dec-03 3:31 
GeneralRe: Thread-Safe Singleton Pin
Heath Stewart11-Dec-03 4:30
protectorHeath Stewart11-Dec-03 4:30 
GeneralRe: Thread-Safe Singleton Pin
bzurer11-Dec-03 4:56
bzurer11-Dec-03 4:56 
Generalabout setup projects Pin
Mauricio Ritter8-Dec-03 3:50
Mauricio Ritter8-Dec-03 3:50 
GeneralRe: about setup projects Pin
Heath Stewart8-Dec-03 3:55
protectorHeath Stewart8-Dec-03 3:55 

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.