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

Cannot Initialize with base

Im not sure how to use base to initialize the NumSides to 4 in my Square Class.

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 readonly (int NumSides) : base(NumSides)
        {
        }

    }
}

1 Answer

Steven Parker
Steven Parker
231,248 Points

There are a few issues too, here are some hints:

  • make no changes to the definition of Polygon
  • since Polygon's constructor sets NumSides, you can initialize it to 4 by calling base(4)
  • use how Polygon sets NumSides as an example of how Square needs to handle SideLength
  • Polygon can also be an example of the syntax for a constructor

Thank you!!! I did all of those the first time I did the challenge except I didn't write base(4), i wrote base(numSides = 4).