Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

C# C# Objects Inheritance Inheritance

Muhammad Bah
Muhammad Bah
852 Points

Defining Square class as inner class of the Polygon class.

I totally do not understand what I'm being asked to do. Please help. Thank you.

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

        Polygon square = new Square();

        public Polygon(int numSides)
        {
            NumSides = numSides;
            numSides = 4;
        }
    }
    class Square : Polygon
    {
        public readonly int SideLength;

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

4 Answers

Steven Parker
Steven Parker
231,198 Points

It looks like you have a few issues:

You don't need this line, and that variable is about to be disposed anyway:

            numSides = 4;  // remove this line

The instructions also don't say to add a square property to Polygon. In fact, you should make no changes to the definition of Polygon. It should remain as-is and your additions will only be to the new class Square. So you should also remove this line:

        Polygon square = new Square();

And as Jon suggested, you need to pass the value to the base constructor:

        public Square (int sideLength) : base(4)
Muhammad Bah
Muhammad Bah
852 Points
namespace Treehouse.CodeChallenges
{
    class Polygon
    {
        public readonly int NumSides;

        Polygon square = new Square();

        public Polygon(int numSides)
        {
            NumSides = numSides;
        } 
    }
    class Square : Polygon
    {
        public readonly int SideLength;

        public Square (int sideLength) : base(4)
        {
            SideLength = sideLength;
        }
    }
}
Jon Wood
Jon Wood
9,884 Points

I just ran your code in the challenge and it passed. What error are you seeing?

Muhammad Bah
Muhammad Bah
852 Points

It says "Bummer! Did you define your Square class as an inner class of the Polygon class? Be sure to define the Square class before or after the definition for the Polygon class."

Jon Wood
Jon Wood
9,884 Points

Oh! I only ran what you had in the Square class. Remove this line Polygon square = new Square(); from the Polygon class and it will pass.

Muhammad Bah
Muhammad Bah
852 Points

Thank you so much man that worked!

Jon Wood
Jon Wood
9,884 Points

You are so close!

You have all of it, other than the challenge wanting you to initialize the base class with numSides set to 4. When you call base() on a subclass constructor you are calling the constructor of the class you're inheriting from. In this case you're just passing in the constant 4.

Muhammad Bah
Muhammad Bah
852 Points

I did what you guys said and the issue still persists.

Steven Parker
Steven Parker
231,198 Points

Try starting over and make sure not to change the original code, just add the new class.

Muhammad Bah
Muhammad Bah
852 Points

Did that and I'm still getting the same issue.

Steven Parker
Steven Parker
231,198 Points

It looks like you're still modifying the original code. Don't put anything in the Polygon class!