How to call base class constructor in derived class?

30,619

Solution 1

No, You'll have to explicitly add constructor to your derived classes. Base class's constructor is for base class only. You'll have to chain the constructor of your interested overload.

public class TestClass2 : TestClass1
{
    public TestClass2 (string str1,string str2)
    : base(str1,str2)//Call base constructor explicitly
    {

    }
}

Above sample shows that am particularly interested in constructor overload which takes two string parameters. In this case you are only allowed to use this overload only since TestClass2 doesn't have any other constructors defined.

Compiler will not provide default constructor for you when you define one; So there is only way you can create instance of TestClass2 is new TestClass2(arg1,arg2);

Solution 2

Try this:

public class TestClass2 : TestClass1
{
    public TestClass2()
    {
    }

    public TestClass2(string str1, string str2)
        : base(str1, str2)
    {
    }

    public TestClass2(string str1, string str2, string str3)
        : base(str1, str2, str3)
    {
    }

    public TestClass2(string str1, string str2, string str3, string str4)
        : base(str1, str2, str3, str4)
    {
    }
}

Solution 3

you invoke with single parameter i.e "test" and "test,test"

namespace ConsoleApplication1
{
    public class TestClass1
    {
        public TestClass1()
        {
            Console.WriteLine("This is base class constructor1");
        }


        public TestClass1(string str1, string str2)
        {
            Console.WriteLine("This is base class constructor2");
        }

        public TestClass1(string str1, string str2, string str3)
        {
            Console.WriteLine("This is base class constructor3");
        }

        public TestClass1(string str1, string str2, string str3, string str4)
        {
            Console.WriteLine("This is base class constructor4");
        }
    }

    public class TestClass2 : TestClass1
    {
        public TestClass2(string str)
        {
        }

    }

    class Program
    {
        static void Main(string[] args)
        {
            TestClass2 test = new TestClass2("test");
            TestClass2 test1 = new TestClass2("test,test");

        }
    }
}
Share:
30,619
Microsoft Developer
Author by

Microsoft Developer

Updated on February 17, 2020

Comments

  • Microsoft Developer
    Microsoft Developer about 4 years

    Based on some requirements, I want to add all constructors/methods of the base class into the derived class without writing methods/constructors in the derived class.

    For that I have written code as shown below but it does not work. It shows error "ConsoleApplication1.TestClass2' does not contain a constructor that takes 1 arguments".
    How can I fix that?

    I cannot create any method or constructor in the base class. Is there any other way apart from inheriting a class?

    My code:

    namespace ConsoleApplication1
    {
        public class TestClass1
        {
            public TestClass1()
            {
                Console.WriteLine("This is base class constructor1");
            }
    
            public TestClass1(string str1,string str2)
            {
                Console.WriteLine("This is base class constructor2");
            }
    
            public TestClass1(string str1,string str2,string str3)
            {
                Console.WriteLine("This is base class constructor3");
            }
    
            public TestClass1(string str1,string str2,string str3,string str4)
            {
                Console.WriteLine("This is base class constructor4");
            }
        }
    
        public class TestClass2 : TestClass1
        {
    
        }
    
        class Program
        {
            static void Main(string[] args)
            {
                TestClass2 test = new TestClass2("test");
                TestClass2 test1 = new TestClass2("test,test");
            }
        }
    }