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

nave lahav
nave lahav
388 Points

whats wrong

I did what the instructions told me to do and there's an error

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

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

        public Square(int sideLength) : base Polygon(numSides)
        {

            SideLength=sideLength;
        }

    }

    } 



}
Simon Coates
Simon Coates
28,694 Points

your use of {} is probably wrong (correct indenting would make this clearer). You've added a line of code to the Polygon constructor that you don't need (and omitted the ;), and I'm not sure you need the word 'Polygon' on the line with the Square constructor. I'd assume that if you use base, it knows to use polygon.

3 Answers

Jon Wood
Jon Wood
9,884 Points

I think you have the signature of the Square class inside of the Polygon class.

Simon Coates
Simon Coates
28,694 Points

it seemed to accept:

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(4)
        {
            SideLength = sideLength;
        }  
    }
}
Jon Wood
Jon Wood
9,884 Points

Yep, that looks good! Though, calling the base constructor for Polygon should take in the sideLength.

Simon Coates
Simon Coates
28,694 Points

um, a single sidelength would only apply to shapes that have all equal sides. a single value would not be useful for a generic polygon.

Jon Wood
Jon Wood
9,884 Points

Simon Coates, you're right! I jumped the gun on that. I should have double checked. Thanks for letting me know! :)

Christian Mangeng
Christian Mangeng
15,970 Points

Yes, at the moment your Square class is inside the Polygon class. Polygon and Square should be two separate classes, although Square inherits from Polygon. Also, don't forget to add a semicolon at the end of the SideLength field. The initialization of numSides to 4 can be done like that:

public Square(int sideLength) : base(4)