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

michael edmondson
michael edmondson
4,510 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,510 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,510 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