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

Radu - Adrian Buha
PLUS
Radu - Adrian Buha
Courses Plus Student 5,535 Points

What am I doing wrong?

There's something that I'm missing, but I fail to figure it out. Can anyone give me a hint? Thanks!

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(numSides){
            SideLength = sideLength;
        }
        Polygon polygon = new Poligon(4);
    }
}

2 Answers

Shadab Khan
Shadab Khan
5,470 Points

Hi,

How is the compiler supposed to know what the value of "numSides" is in the Square class? That is something you are required to pass, which is 4, as a square object will always have 4 sides. Had it been another class for e.g. Trapezium class which you would've inherited from the Polygon class, then you would have had to pass 5 in that case, because a trapezium object will always have 5 sides.

Please try the following code and it should work:

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

I am also unable to understand why you're creating a Polygon object within the Square class? Finally, please check the last line of your code: Replace 'Poligon' with 'Polygon' for successful compilation.

Let me know if the above doesn't make sense.