Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Single intance

Single intance

Scheduled Pinned Locked Moved C#
csharphelptutorial
13 Posts 6 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    kinnuP
    wrote on last edited by
    #1

    hi all, i want to know how to restrict the class not to instaciate more than once in C# help me in this plse...

    P.gurukiran

    C S C 3 Replies Last reply
    0
    • K kinnuP

      hi all, i want to know how to restrict the class not to instaciate more than once in C# help me in this plse...

      P.gurukiran

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      A private constructor and a create method. A static boolean flag that starts as false, is set true by the Create method, and returns null once that flag is true.

      Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog

      K C 3 Replies Last reply
      0
      • C Christian Graus

        A private constructor and a create method. A static boolean flag that starts as false, is set true by the Create method, and returns null once that flag is true.

        Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog

        K Offline
        K Offline
        kinnuP
        wrote on last edited by
        #3

        thank u Christian Graus , but i need any key words are available for it to restrict it is more help full

        P.gurukiran

        1 Reply Last reply
        0
        • K kinnuP

          hi all, i want to know how to restrict the class not to instaciate more than once in C# help me in this plse...

          P.gurukiran

          S Offline
          S Offline
          Schmullus
          wrote on last edited by
          #4

          Hi, my you use an static variable within the class that counts the instances. namespace ConsoleApplication1 { class TestClass { // static variable public static int instanceCount = 0; // Constructor public TestClass() { instanceCount++; } // Some code // ... } class Program { static void Main(string[] args) { TestClass myTestClass1 = new TestClass(); TestClass myTestClass2 = new TestClass(); Console.WriteLine(TestClass.instanceCount); Console.ReadLine(); } } } You can use the variable 'instanceCount' to restrict the instance generation. Is it that what you're looking for? Regards Erik

          C P 2 Replies Last reply
          0
          • C Christian Graus

            A private constructor and a create method. A static boolean flag that starts as false, is set true by the Create method, and returns null once that flag is true.

            Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog

            C Offline
            C Offline
            Colin Angus Mackay
            wrote on last edited by
            #5

            Christian Graus wrote:

            A private constructor and a create method. A static boolean flag that starts as false, is set true by the Create method, and returns null once that flag is true.

            Ummmm.... I'm not sure how that describes a singleton. I've never used a Boolean flag in any of my singletons. They look a bit like this:

            public SingletonClass
            {
            private static SingletonClass onlyInstance;

            // Private constructor - only this class can instantiate itself
            private SingletonClass()
            {
            }
            
            public SingletonClass Instance
            {
                get
                {
                    if (onlyInstance == null)
                        onlyInstance = new SingletonClass();
                    return onlyInstance;
                }
            }
            
            // Do your stuff here.
            

            }


            Upcoming Scottish Developers events: * We are starting a series of events in Glasgow in 2007. Are you interested in a particular subject, or as a speaker? * Developer Day Scotland: are you interested in speaking or attending? My: Website | Blog | Photos

            P 1 Reply Last reply
            0
            • S Schmullus

              Hi, my you use an static variable within the class that counts the instances. namespace ConsoleApplication1 { class TestClass { // static variable public static int instanceCount = 0; // Constructor public TestClass() { instanceCount++; } // Some code // ... } class Program { static void Main(string[] args) { TestClass myTestClass1 = new TestClass(); TestClass myTestClass2 = new TestClass(); Console.WriteLine(TestClass.instanceCount); Console.ReadLine(); } } } You can use the variable 'instanceCount' to restrict the instance generation. Is it that what you're looking for? Regards Erik

              C Offline
              C Offline
              Colin Angus Mackay
              wrote on last edited by
              #6

              I think the original poster was describing a singleton class.


              Upcoming Scottish Developers events: * We are starting a series of events in Glasgow in 2007. Are you interested in a particular subject, or as a speaker? * Developer Day Scotland: are you interested in speaking or attending? My: Website | Blog | Photos

              1 Reply Last reply
              0
              • K kinnuP

                hi all, i want to know how to restrict the class not to instaciate more than once in C# help me in this plse...

                P.gurukiran

                C Offline
                C Offline
                Colin Angus Mackay
                wrote on last edited by
                #7

                What you are looking for is called a Singleton:

                public SingletonClass
                {
                private static SingletonClass onlyInstance;

                // Private constructor - only this class can instantiate itself
                private SingletonClass()
                {
                }
                
                public SingletonClass Instance
                {
                    get
                    {
                        if (onlyInstance == null)
                            onlyInstance = new SingletonClass();
                        return onlyInstance;
                    }
                }
                
                // Do your stuff here.
                

                }


                Upcoming Scottish Developers events: * We are starting a series of events in Glasgow in 2007. Are you interested in a particular subject, or as a speaker? * Developer Day Scotland: are you interested in speaking or attending? My: Website | Blog | Photos

                K 1 Reply Last reply
                0
                • C Colin Angus Mackay

                  What you are looking for is called a Singleton:

                  public SingletonClass
                  {
                  private static SingletonClass onlyInstance;

                  // Private constructor - only this class can instantiate itself
                  private SingletonClass()
                  {
                  }
                  
                  public SingletonClass Instance
                  {
                      get
                      {
                          if (onlyInstance == null)
                              onlyInstance = new SingletonClass();
                          return onlyInstance;
                      }
                  }
                  
                  // Do your stuff here.
                  

                  }


                  Upcoming Scottish Developers events: * We are starting a series of events in Glasgow in 2007. Are you interested in a particular subject, or as a speaker? * Developer Day Scotland: are you interested in speaking or attending? My: Website | Blog | Photos

                  K Offline
                  K Offline
                  kinnuP
                  wrote on last edited by
                  #8

                  thank u Colin Angus Mackay

                  P.gurukiran

                  1 Reply Last reply
                  0
                  • C Christian Graus

                    A private constructor and a create method. A static boolean flag that starts as false, is set true by the Create method, and returns null once that flag is true.

                    Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog

                    K Offline
                    K Offline
                    kinnuP
                    wrote on last edited by
                    #9

                    hi Christian Graus, i am trying above code i am not able restrict the class please can u provide smple code it will help me lot

                    P.gurukiran

                    1 Reply Last reply
                    0
                    • C Colin Angus Mackay

                      Christian Graus wrote:

                      A private constructor and a create method. A static boolean flag that starts as false, is set true by the Create method, and returns null once that flag is true.

                      Ummmm.... I'm not sure how that describes a singleton. I've never used a Boolean flag in any of my singletons. They look a bit like this:

                      public SingletonClass
                      {
                      private static SingletonClass onlyInstance;

                      // Private constructor - only this class can instantiate itself
                      private SingletonClass()
                      {
                      }
                      
                      public SingletonClass Instance
                      {
                          get
                          {
                              if (onlyInstance == null)
                                  onlyInstance = new SingletonClass();
                              return onlyInstance;
                          }
                      }
                      
                      // Do your stuff here.
                      

                      }


                      Upcoming Scottish Developers events: * We are starting a series of events in Glasgow in 2007. Are you interested in a particular subject, or as a speaker? * Developer Day Scotland: are you interested in speaking or attending? My: Website | Blog | Photos

                      P Offline
                      P Offline
                      Prakash Nadar
                      wrote on last edited by
                      #10

                      where is the removed declared? and how is onlyInstance initialised?


                      -Prakash

                      C 1 Reply Last reply
                      0
                      • S Schmullus

                        Hi, my you use an static variable within the class that counts the instances. namespace ConsoleApplication1 { class TestClass { // static variable public static int instanceCount = 0; // Constructor public TestClass() { instanceCount++; } // Some code // ... } class Program { static void Main(string[] args) { TestClass myTestClass1 = new TestClass(); TestClass myTestClass2 = new TestClass(); Console.WriteLine(TestClass.instanceCount); Console.ReadLine(); } } } You can use the variable 'instanceCount' to restrict the instance generation. Is it that what you're looking for? Regards Erik

                        P Offline
                        P Offline
                        Prakash Nadar
                        wrote on last edited by
                        #11

                        your code sample can not be used to retrict the number of instances, it only helps in couting the instances. As long as you keep the constructor public, it will be difficult (maybe impossible) to create a singleton class.


                        -Prakash

                        1 Reply Last reply
                        0
                        • P Prakash Nadar

                          where is the removed declared? and how is onlyInstance initialised?


                          -Prakash

                          C Offline
                          C Offline
                          Colin Angus Mackay
                          wrote on last edited by
                          #12

                          Mr.Prakash wrote:

                          where is the removed declared? and how is onlyInstance initialised?

                          The curious thing is that I thought I'd messed up my post, but when I went to modify it the code displays correctly in the edit window. "removed" should read "onlyInstance" and I have absolutely no idea why it says removed.


                          Upcoming Scottish Developers events: * We are starting a series of events in Glasgow in 2007. Are you interested in a particular subject, or as a speaker? * Developer Day Scotland: are you interested in speaking or attending? My: Website | Blog | Photos

                          N 1 Reply Last reply
                          0
                          • C Colin Angus Mackay

                            Mr.Prakash wrote:

                            where is the removed declared? and how is onlyInstance initialised?

                            The curious thing is that I thought I'd messed up my post, but when I went to modify it the code displays correctly in the edit window. "removed" should read "onlyInstance" and I have absolutely no idea why it says removed.


                            Upcoming Scottish Developers events: * We are starting a series of events in Glasgow in 2007. Are you interested in a particular subject, or as a speaker? * Developer Day Scotland: are you interested in speaking or attending? My: Website | Blog | Photos

                            N Offline
                            N Offline
                            Nader Elshehabi
                            wrote on last edited by
                            #13

                            Looks like Bob is teasing you a little!!:laugh:

                            Regards:rose:

                            1 Reply Last reply
                            0
                            Reply
                            • Reply as topic
                            Log in to reply
                            • Oldest to Newest
                            • Newest to Oldest
                            • Most Votes


                            • Login

                            • Don't have an account? Register

                            • Login or register to search.
                            • First post
                              Last post
                            0
                            • Categories
                            • Recent
                            • Tags
                            • Popular
                            • World
                            • Users
                            • Groups