private instance constructor

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • TonyJ

    private instance constructor

    Hello!

    If you have private instance constructor you can then instance object from
    within the class itself.
    It is any point in doing this? I mean is there situation where this can be a
    point doing this.

    //tony


  • Marc Gravell

    #2
    Re: private instance constructor

    3 common reasons:
    * in 1.1, as part of a static class
    * a ctor used by the public ctors
    * for use from static members (such as the singleton pattern).

    ctor example:

    private MyClass(int value, string name) {
    // do all the actual work
    }
    public MyClass(string value) : this(0, value) {}
    public MyClass(int value) : this(value, "") {}

    -------

    singleton (simple field approach; other patterns are available ;-p):

    private MyClass() {}
    public static readonly MyClass Default = new MyClass();

    -------

    other static method:

    private static int counter;
    private MyClass(int id) {
    this.id = id;
    }
    public static MyClass Create() {
    // give each a unique id, or do something else useful ;-p
    int newId = Interlocked.Inc rement(ref counter);
    return new MyClass(newId);
    // (ok, this could be done in the instance ctor, but hey!)
    }

    -----

    Marc

    Comment

    • Michael Nemtsev, MVP

      #3
      Re: private instance constructor

      Hello TonyJ,

      This is mostly used when you need to control the creation of your object,
      for example in Singleton pattern
      when we hide the constructor and manually controlling when create the new
      instance and when return existed one

      ---
      WBR,
      Michael Nemtsev [.NET/C# MVP] :: blog: http://spaces.live.com/laflour

      "The greatest danger for most of us is not that our aim is too high and we
      miss it, but that it is too low and we reach it" (c) Michelangelo


      TIf you have private instance constructor you can then instance object
      Tfrom within the class itself.
      T>


      Comment

      • Chris Shepherd

        #4
        Re: private instance constructor

        TonyJ wrote:
        Hello!
        >
        If you have private instance constructor you can then instance object from
        within the class itself.
        It is any point in doing this? I mean is there situation where this can be a
        point doing this.
        How about where you want to take mostly similar but slightly different
        actions on data passed when it's coming from a different constructor? e.g.:

        public class A
        {

        public A()
        : this("some","de fault","values" ,0.01m,false)
        {}

        public A(string a, string b, string c, decimal d)
        : this(a,b,c,d,tr ue)
        {

        }

        private A(string a, string b, string c, decimal d, bool hidden)
        {
        if (hidden)
        {
        // Do some automatic stuff, rather than popping
        // up a dialog box and asking the user.
        }
        else
        {
        // Prompt a user for some information
        }

        // Do stuff common to both constructors.
        }
        }

        Chris.

        Comment

        • Ignacio Machin \( .NET/ C# MVP \)

          #5
          Re: private instance constructor

          Hi,

          It's used in a lot of places.

          In addition to the other posts, consider the case of a DataRow. It's
          existence and its very same structure depends of a containing table. What
          is the solution?
          DataTable has a NewRow method that internally create a DataRow and return
          it. You cannot create a DataRow on your own.

          The above pattern assure that the DataRow will be correctly initialized.

          There are more examples like this. just post back if you need more.

          --
          Ignacio Machin
          The #1 Warehouse Management System & Direct Store Delivery Software (DSD) for QuickBooks & ERP Systems – LaceUp Solutions

          Mobile & warehouse Solutions.
          "TonyJ" <johansson.ande rsson@telia.com wrote in message
          news:%23S9mK2HH IHA.2064@TK2MSF TNGP06.phx.gbl. ..
          Hello!
          >
          If you have private instance constructor you can then instance object from
          within the class itself.
          It is any point in doing this? I mean is there situation where this can be
          a
          point doing this.
          >
          //tony
          >
          >

          Comment

          Working...