calling a private constructor

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • cmrhema
    Contributor
    • Jan 2007
    • 375

    calling a private constructor

    Hello
    I have created a class also a constructor which is (should be, one of the conditions) private.
    Now I am supposed to call it in the main program. My team leader says there is a trick to call it. I can call the variable j(as it is static). but not con.
    Please help me out.
    [code=cpp]
    public class con
    {
    private con()
    {
    Console.WriteLi ne("private constructor");
    }
    public static int j = 5;
    }
    }
    [/code]
    Thanking you
    cmrhema
    Last edited by Frinavale; Sep 10 '07, 06:29 PM. Reason: Added [code] tags to make more legible
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    This is C#.

    I am moving this to the .NET forum.

    Comment

    • Plater
      Recognized Expert Expert
      • Apr 2007
      • 7872

      #3
      You would need to make a public wrapper for that constructor I think

      so like:
      Code:
      public class myclass
      {
         public myclass(string temp)
         {
            return myclass();
         }
         private myclass()
         {//private constructor
         }
      }

      Comment

      • Frinavale
        Recognized Expert Expert
        • Oct 2006
        • 9749

        #4
        Originally posted by Plater
        You would need to make a public wrapper for that constructor I think

        so like:
        Code:
        public class myclass
        {
           public myclass(string temp)
           {
              return myclass();
           }
           private myclass()
           {//private constructor
           }
        }
        I'm just wondering why you would want to have a private constructor only to expose it publicly? Why not just make it public since you need to call it in another function anyways?

        -Frinny

        Comment

        • r035198x
          MVP
          • Sep 2006
          • 13225

          #5
          Originally posted by Plater
          You would need to make a public wrapper for that constructor I think

          so like:
          Code:
          public class myclass
          {
             public myclass(string temp)
             {
                return myclass();
             }
             private myclass()
             {//private constructor
             }
          }
          Erm, constructors cannot return a value.

          Perhaps you meant something like

          [CODE=css]using System;
          public class myclass {
          public myclass():this( "") {

          }
          private myclass(String s) {
          Console.WriteLi ne("Private constructor called");
          }
          public static void Main() {
          new myclass();
          }
          }[/CODE]

          Comment

          • Plater
            Recognized Expert Expert
            • Apr 2007
            • 7872

            #6
            Oops, yeah. I knew there was something funny about it but I was moving really quick at the time
            Originally posted by r035198x
            Erm, constructors cannot return a value.

            Perhaps you meant something like

            [CODE=css]using System;
            public class myclass {
            public myclass():this( "") {

            }
            private myclass(String s) {
            Console.WriteLi ne("Private constructor called");
            }
            public static void Main() {
            new myclass();
            }
            }[/CODE]

            Comment

            • cmrhema
              Contributor
              • Jan 2007
              • 375

              #7
              Originally posted by r035198x
              Erm, constructors cannot return a value.

              Perhaps you meant something like

              [CODE=css]using System;
              public class myclass {
              public myclass():this( "") {

              }
              private myclass(String s) {
              Console.WriteLi ne("Private constructor called");
              }
              public static void Main() {
              new myclass();
              }
              }[/CODE]
              Thanks for the reply

              Yes I exactly meant very similiar thing. Infact I should write a method that should return a value. The only condition was that I should create a class and a private constructor inside it. Thereafter call in the main function.
              I tried the above but it did not call.

              Comment

              • r035198x
                MVP
                • Sep 2006
                • 13225

                #8
                Originally posted by cmrhema
                Thanks for the reply

                Yes I exactly meant very similiar thing. Infact I should write a method that should return a value. The only condition was that I should create a class and a private constructor inside it. Thereafter call in the main function.
                I tried the above but it did not call.
                I didn't try it last night but I've just tried it now and it works.
                Something must be rotten in the state of Denmark then.

                Comment

                • cmrhema
                  Contributor
                  • Jan 2007
                  • 375

                  #9
                  Originally posted by r035198x
                  I didn't try it last night but I've just tried it now and it works.
                  Something must be rotten in the state of Denmark then.

                  Sorry ro35198x
                  It works So silly of me to forget "static" main

                  thank you
                  regards
                  cmrhema

                  Comment

                  • r035198x
                    MVP
                    • Sep 2006
                    • 13225

                    #10
                    Originally posted by cmrhema
                    Sorry ro35198x
                    It works So silly of me to forget "static" main

                    thank you
                    regards
                    cmrhema
                    No need to apologize for that static. What you need to apoligize for is spelling my name wrong.
                    It's r035198x not ro35..whatever.

                    Comment

                    • cmrhema
                      Contributor
                      • Jan 2007
                      • 375

                      #11
                      Originally posted by r035198x
                      No need to apologize for that static. What you need to apoligize for is spelling my name wrong.
                      It's r035198x not ro35..whatever.
                      Hello
                      r035198x
                      I was about to edit it(your name) because I wanted to add one more question to the problem.

                      Now I need to call a method inside the private constructor and I should call from the main. Kindly help
                      Code:
                      using System;
                      public class myclass {
                          public myclass():this("") {
                              
                          }
                          private myclass(String s) {
                      // A method to be added here with a return value
                              Console.WriteLine("Private constructor called");
                          }
                          public static void Main() {
                              new myclass();
                      // need to call the method and value here
                          }
                      }
                      Thank you r035198x once again

                      regards
                      cmrhema

                      Comment

                      • Plater
                        Recognized Expert Expert
                        • Apr 2007
                        • 7872

                        #12
                        Originally posted by cmrhema
                        Now I need to call a method inside the private constructor and I should call from the main. Kindly help
                        I am assuming you mean you need the private constructor to call a method?
                        Just create another one in the class (mark it private if you want)
                        And tell the private constructor to call it.

                        This is starting to sound like a homework/project for school though.

                        Comment

                        • r035198x
                          MVP
                          • Sep 2006
                          • 13225

                          #13
                          Originally posted by Plater
                          ...
                          This is starting to sound like a homework/project for school though.
                          And for homework we really like to see some effort first.

                          Comment

                          • cmrhema
                            Contributor
                            • Jan 2007
                            • 375

                            #14
                            Originally posted by r035198x
                            And for homework we really like to see some effort first.
                            should have replied long long ago.
                            This is what I wanted

                            using System;
                            using System.Collecti ons.Generic;
                            using System.Text;

                            namespace constructor
                            {
                            class Program
                            {
                            static void Main(string[] args)
                            {

                            myclass ji = myclass.con();

                            }
                            }
                            public class myclass
                            {

                            public myclass()
                            : this("")
                            {
                            }
                            private myclass(string a)
                            {
                            Console.WriteLi ne("private constructor opened");


                            a = "static";


                            }
                            public static myclass con()
                            {

                            Console.WriteLi ne("ccheckin insided static");

                            return new myclass();

                            }

                            }
                            }

                            Comment

                            • r035198x
                              MVP
                              • Sep 2006
                              • 13225

                              #15
                              Originally posted by cmrhema
                              should have replied long long ago.
                              This is what I wanted

                              using System;
                              using System.Collecti ons.Generic;
                              using System.Text;

                              namespace constructor
                              {
                              class Program
                              {
                              static void Main(string[] args)
                              {

                              myclass ji = myclass.con();

                              }
                              }
                              public class myclass
                              {

                              public myclass()
                              : this("")
                              {
                              }
                              private myclass(string a)
                              {
                              Console.WriteLi ne("private constructor opened");


                              a = "static";


                              }
                              public static myclass con()
                              {

                              Console.WriteLi ne("ccheckin insided static");

                              return new myclass();

                              }

                              }
                              }
                              1.) Please use code tags when posting code.
                              2.) What is this code supposed to be doing?

                              Comment

                              Working...