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

Have I missed something?

According to the question I have done what it has asked right? I have initialised the variable NumSides and set it to 4, I have initialised the Square class with inheritance from the Polygon class. The SideLength variable was created in the Square class and is public and readonly. I don't know what I have done wrong here

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

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

    }
    class Square : Polygon
    {
        public readonly SideLength;

        public Square(NumSides) : base(NumSides)
        {
            NumSides = 4;
            SideLength = sidelength;
        }
    }
}

1 Answer

Steven Parker
Steven Parker
222,288 Points

There's a few issues:

  • you need to declare the type (int) of "SideLength"
  • the contructor's parameter name should be "sideLength" instead of "NumSides"
  • the parameter also needs to have a type (int)
  • "Numsides" will not be set directly, but by passing 4 to base ("base(4)")
  • the assignment has "sidelength" (lower case) instead of "sideLength" (with capital "L")