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

not able to submit the code , it has errors, need help

is the code correct. not able to check work..

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 (4)
        {

        }

        }
    }
}

3 Answers

Steven Parker
Steven Parker
229,788 Points

:point_right: You defined the "Square" class inside the "Polygon" class.

The definition of "Square" should share the namespace with "Polygon" at the same nesting level.

Also, you forgot to initialize the SideLength field with the value passed to your constructor.

I created the sample code, not able to initialize the value to sideLenght. Any help please:

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks;

namespace TreeHouse_inheritance {

class Polygon
{
    public  int NumSides;

    public Polygon (int numsides)
        {
        this.NumSides = 4;
        }
}

class Square : Polygon
{
    public readonly int SideLength;
    public Square( int SideLength) : base(SideLength)
    {

    }
}
class Program
{
    static void Main(string[] args)
    {
        Polygon p = new Polygon(1);
        Console.WriteLine("Polygon Class {0}", p.NumSides);
        Square s = new Square(1);
        Console.WriteLine("SquareClass {0}", s.SideLength);
        Console.ReadLine();
    }
}

}

Result:

Polygon Class 4 SquareClass 0

public Square( int SideLength) : base(SideLength) { this.SideLength = NumSides; } }

This works,

Polygon Class 4 SquareClass 4,

Can anyone explain me the difference.