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

michael edmondson
michael edmondson
4,500 Points

Where am i going wrong with my code ? the challenge is not like prev ex in video

its feels like im close but it also feels like i may be mis reading the question

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;
            }   
        }
    }
}

4 Answers

Follow the error messages. The first one you receive is:

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

You did. Just need to adjust some brackets so that Square is defined after Polygon.

The next error you receive is:

Does your Square constructor take a single parameter named sideLength

Check the case of sideLength. S should be lowercase.

The last error message you receive is:

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

You need to initialize numSides to 4

michael edmondson
michael edmondson
4,500 Points

What Exactly is wrong with my brackets? it wasnt extactly went over like this in the videos they had seperate files for the classes

Based on your brackets the Square class is inside the Polygon class. You have (some code omitted)

class Polygon
{
     class Square : Polygon
     { 
     } 
}

It should be:

class Polygon
{
}

class Square : Polygon 
{ 
}
michael edmondson
michael edmondson
4,500 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(numSides = 4)
    {

    }
}

}

how is then done is the base constructor initialize numSides to 4 this was never went over in the video