Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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
216,175 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).