Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

C# C# Objects Inheritance Inheritance

Alex Ryan
Alex Ryan
1,211 Points

Initialize the field 'numSides' to 4

I got most of the way through this challenge but got stuck on the part where I need to initialize numSides to 4 in the base constructor. After trying everything I could think of and searching forums I still couldn't find the solution.

The error message I keep getting after checking the code is: "Did you initialize the field 'numSides' to 4 in the base constructor?"

Can anyone spot where I'm going wrong?

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(int numSides)
        {
            SideLength = sideLength;

            NumSides = 4;
        }
    }
}

2 Answers

Steven Parker
Steven Parker
221,976 Points

Since the base ("Polygon") constructor uses its argument as the value for the parameter "numSides", anything you pass to the base will be used to initialize the field NumSides.

So to "Use base to initialize NumSides to 4" you would call "base(4)".

Alex Ryan
Alex Ryan
1,211 Points

I replaced "NumSides = 4" with "base(4)", which is what I think you were saying to do but I still get the same error message.

Steven Parker
Steven Parker
221,976 Points

You do need to remove the reference to "NumSides" from "Square"s constructor, but what I was suggesting about "base" is this replacement:

        public Square(int sideLength) : base(int numSides)  // instead of this...
        public Square(int sideLength) : base(4)             // ...pass "4" directly to "base"
Alex Ryan
Alex Ryan
1,211 Points

Ah I see, thank you so much, it's working now!