C# C# Objects Inheritance Inheritance

michael edmondson
michael edmondson
3,369 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

KRIS NIKOLAISEN
PRO
KRIS NIKOLAISEN
Pro Student 51,961 Points

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
3,369 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

KRIS NIKOLAISEN
PRO
KRIS NIKOLAISEN
Pro Student 51,961 Points

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
3,369 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