C# C# Objects Inheritance Inheritance

Ali Dahud
Ali Dahud
3,459 Points

Bummer: Did you initialize the field 'numSides' to 4 in the base constructor? Why?

why am i getting this error?

Polygon.cs
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(numSides=4)
        {

        }
    }
}

2 Answers

Shlomi Bittan
Shlomi Bittan
6,718 Points

Hi Ali, In this challenge you were asked to initialize the 'SideLength' field of the Square class, and call the base construcotor to set the value of numSides to 4.

  • Setting values to named parameters is by the ':' sign NOT the '=' sign. So....:

    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;
        }
    }
    }
    
Ali Dahud
Ali Dahud
3,459 Points

Steven Parker could you please explain to me that why doesn’t it take the parameters of the super class? I hope I was understood

Steven Parker
Steven Parker
177,538 Points

The constructor for the super (base) class (Polygon) takes a number of sides, that's the "4" in "base(4)".
But the constructor for Square takes a side length parameter, that's just the way they are defined.