locked
inheritance and instantiation error RRS feed

  • Question

  • I've got a class that inherits another class and they both need to be declared in a similar way but I don't know why this simple example doesn't work

    public partial class Form1 : Form
        {
            public Form1()
            {
    
            }
        }
    
        public class classA
        {
            public string strA = "A";
            public classA(string strA)
            {
                this.strA = strA;
            }
        }
    
        public class classB : classA
        {
    
            public classB(string strB)
            {
                this.strA = strB;
            }
        }

    how come I get the error:

    There is no argument given that corresponds to the required formal parameter 'strA' of 'classA.classA(string)' test inheritance C:\Users\Christ\Documents\C#\test inheritance\Form1.cs 33 Active

    none of the c# inheritance articles I've read seem to cover this.


    my code is perfect until i don't find a bug



    Saturday, September 19, 2020 4:42 PM

Answers

  • Try this fix:

    public class classB : classA

    {

       public classB( string strB )

          : base( strB )

       {

       }

    }

    • Marked as answer by Christ Kennedy Saturday, September 19, 2020 7:42 PM
    Saturday, September 19, 2020 7:09 PM