how to call a private constructor

Discussion in 'C#' started by cmrhema, Sep 10, 2007.

  1. cmrhema

    cmrhema New Member

    Joined:
    Sep 10, 2007
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    Hello
    I have created a class also a constructor which is (should be, its 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.

    public class con
    {
    private con()
    {
    Console.WriteLine("private constructor");
    }
    public static int j = 5;
    }
    }

    Thanking you
    cmrhema
     
  2. DaWei

    DaWei New Member

    Joined:
    Dec 6, 2006
    Messages:
    835
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Semi-retired EE
    Location:
    Texan now in Central NY
    Home Page:
    http://www.daweidesigns.com
    Overload operator new in your class, and make it public.
     
  3. cmrhema

    cmrhema New Member

    Joined:
    Sep 10, 2007
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    Thanks for the reply

    I am not supposed to make the constructor public(One of the conditions).
    Moreover I am required to write a method inside the constructor that can be public and i am supposed to call it in the main()
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice