C# C# Objects Inheritance Inheritance

Jun Dong
Jun Dong
2,554 Points

Inheritance Code Challenge problem

I got an error that says: "Did you initialize the field 'numSides' to 4 in the base constructor?" Can someone help me?

Polygon.cs
namespace Treehouse.CodeChallenges
{
    class Polygon
    {
        public readonly int NumSides;

        public Polygon(int numSides)
        {
            NumSides = numSides;
        }

    }

    class Square : Polygon{
        Square square = new Square(4);
        public readonly int SideLength;  

        public Square(int sideLength) : base(sideLength) {
            SideLength = sideLength;
        }
    }
}

1 Answer

Zhaopeng Wang
PRO
Zhaopeng Wang
Full Stack JavaScript Techdegree Graduate 32,205 Points
namespace Treehouse.CodeChallenges
{
    class Polygon
    {
        public readonly int NumSides;

        public Polygon(int numSides)
        {
            NumSides = numSides;
        }
    }

    class Square : Polygon {
        public readonly int SideLength;

        public Square(int sideLength):base(4)
        {
            SideLength = sideLength;
        }

    }
}