Base class does not contain a constructor that takes '0' argument

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

    Base class does not contain a constructor that takes '0' argument

    I have

    class BaseClass
    {
    public BaseClass(strin g s1, string s2)
    {
    this.S1 = s1;
    this.S2 = s2;
    }
    public string S1 { get; set;}
    public string S2 { get; set;}
    }

    class SubClass: BaseClass
    {
    public SubClass(int int1, int int2)
    {
    this.Int1 = int1;
    this.Int2 = int2;
    }
    public int Int1 { get; set; }
    public int Int2 { get; set; }
    }

    VS2008 intellisense underlines the constructor of SubClass and says:

    Error 1 'ConsoleApplica tion1.BaseClass ' does not contain a constructor
    that takes '0' arguments.

    Why? Any hint is highly appreciated.
  • Jeroen Mostert

    #2
    Re: Base class does not contain a constructor that takes '0' argument

    Author wrote:
    I have
    >
    class BaseClass
    {
    public BaseClass(strin g s1, string s2)
    {
    this.S1 = s1;
    this.S2 = s2;
    }
    public string S1 { get; set;}
    public string S2 { get; set;}
    }
    >
    class SubClass: BaseClass
    {
    public SubClass(int int1, int int2)
    {
    this.Int1 = int1;
    this.Int2 = int2;
    }
    public int Int1 { get; set; }
    public int Int2 { get; set; }
    }
    >
    VS2008 intellisense underlines the constructor of SubClass and says:
    >
    Error 1 'ConsoleApplica tion1.BaseClass ' does not contain a constructor
    that takes '0' arguments.
    >
    Why? Any hint is highly appreciated.
    C# enforces that construction of the base class must be properly completed
    before the derived class constructor is executed. Because your derived
    constructor doesn't explicitly call a base class constructor, the compiler
    automatically inserts a call to the constructor without arguments. But
    there's no such constructor, so it fails.

    To fix this, you must make a constructor available for the derived class to
    call. In this case, initializing the properties does not seem to be
    mandatory (since users can pass null for both, and even unset them later) so
    there's no problem making a public parameterless constructor available. In
    the general case, you could make a protected constructor available
    specifically for derived classes to initialize the base state with.

    --
    J.

    Comment

    • Ashutosh Bhawasinka

      #3
      Re: Base class does not contain a constructor that takes '0' argument



      Author wrote:
      I have
      >
      class BaseClass
      {
      public BaseClass(strin g s1, string s2)
      {
      this.S1 = s1;
      this.S2 = s2;
      }
      public string S1 { get; set;}
      public string S2 { get; set;}
      }
      >
      class SubClass: BaseClass
      {
      public SubClass(int int1, int int2)
      {
      this.Int1 = int1;
      this.Int2 = int2;
      }
      public int Int1 { get; set; }
      public int Int2 { get; set; }
      }
      >
      VS2008 intellisense underlines the constructor of SubClass and says:
      >
      Error 1 'ConsoleApplica tion1.BaseClass ' does not contain a constructor
      that takes '0' arguments.
      >
      Why? Any hint is highly appreciated.
      >
      Let's say you create an object of SubClass like this
      SubClass sc = new SubClass(1,2);

      Now, when subclass' object is created, it's base class object is also
      created internally. So, it's constructor also must be invoked. Now, the
      derived class is not explicitly specifying which constructor to use for
      the base class, in which case the compiler searches for a constructor
      with 0 parameters.

      So, to solve this error, you can either add a default constructor to
      BaseClass or do something like this

      class SubClass : BaseClass
      {
      public SubClass(int int1, int int2):base(""," ")
      {
      this.Int1 = int1;
      this.Int2 = int2;
      }
      public int Int1 { get; set; }
      public int Int2 { get; set; }
      }

      Comment

      • Nicholas Paldino [.NET/C# MVP]

        #4
        Re: Base class does not contain a constructor that takes '0' argument

        Author,

        The compiler basically assumes this when you write your constructor:

        class SubClass : BaseClass
        {
        public SubClass(int int1, int int2) : base()
        {

        Since you have defined only one constructor in BaseClass and it takes
        arguments, that's where the error comes from. Basically, you need to
        explicitly call the constructor on the base class.


        --
        - Nicholas Paldino [.NET/C# MVP]
        - mvp@spam.guard. caspershouse.co m

        "Author" <gnewsgroup@gma il.comwrote in message
        news:94aef90b-bfc7-44c9-b3e6-011c84024b41@h2 3g2000prf.googl egroups.com...
        >I have
        >
        class BaseClass
        {
        public BaseClass(strin g s1, string s2)
        {
        this.S1 = s1;
        this.S2 = s2;
        }
        public string S1 { get; set;}
        public string S2 { get; set;}
        }
        >
        class SubClass: BaseClass
        {
        public SubClass(int int1, int int2)
        {
        this.Int1 = int1;
        this.Int2 = int2;
        }
        public int Int1 { get; set; }
        public int Int2 { get; set; }
        }
        >
        VS2008 intellisense underlines the constructor of SubClass and says:
        >
        Error 1 'ConsoleApplica tion1.BaseClass ' does not contain a constructor
        that takes '0' arguments.
        >
        Why? Any hint is highly appreciated.

        Comment

        • Author

          #5
          Re: Base class does not contain a constructor that takes '0' argument

          Many thanks to both of you.

          Actually, I did notice that if I call the base class's contructor
          using :base(blah blah), it error goes away.

          So, the conclusion is: either the base class's constructor must be
          explicitly called using base() or a parameterless constructor must be
          provided for the base class. Correct?

          Comment

          • Jeroen Mostert

            #6
            Re: Base class does not contain a constructor that takes '0' argument

            Author wrote:
            So, the conclusion is: either the base class's constructor must be
            explicitly called using base() or a parameterless constructor must be
            provided for the base class. Correct?
            Well, *a* base class constructor must be called (you can have any number of
            them) and if you don't call one explicitly, there indeed must be a
            parameterless one to call implicitly.

            --
            J.

            Comment

            • Author

              #7
              Re: Base class does not contain a constructor that takes '0' argument

              On Nov 6, 2:38 pm, Jeroen Mostert <jmost...@xs4al l.nlwrote:
              Author wrote:
              So, the conclusion is: either the base class's constructor must be
              explicitly called using base() or a parameterless constructor must be
              provided for the base class. Correct?
              >
              Well, *a* base class constructor must be called (you can have any number of
              them) and if you don't call one explicitly, there indeed must be a
              parameterless one to call implicitly.
              >
              --
              J.
              Perfecto! Gotcha. Thx.

              Comment

              Working...