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

Herman Vicens
Herman Vicens
12,540 Points

I don't know what's wrong here!!

No compile error but I keep getting this inner class error and regardless where I place the class, i get the same message. Any ideas?

thanks,

Polygon.cs
namespace Treehouse.CodeChallenges
{
    class Polygon
    {
        public readonly int NumSides;
        public readonly int SideLength;

        public int Polygon(int numSides)
        {
            NumSides = numsides;
        }
    }
    class Square : Polygon
    {      
         public int Square(int sideLength) : base (int numSides)
         {
            NumSides = 4;
            SideLength = sideLength;
         }
    } 
}

2 Answers

andren
andren
28,558 Points

That error message is misleading. But there are a couple of issues with your code:

  1. The SideLength field should be placed inside the Square class, not inside the Polygon class.

  2. You have added int as a return type to the Polygon constructor. Adding a return type to a constructor is not allowed. Constructors does not have return types. That is part of what separates a constructor from a normal method.

  3. You have changed numSides to numsides within the Polygon constructor, numsides is not the name of the parameter so that is incorrect.

  4. You have also added int as a return type to the Square constructor, which as explained above is not valid.

  5. Within the base call you are meant to pass an argument that will be passed directly into the constructor of the parent class. Meaning in this case that you should actually be passing the number 4 in directly, not defining it in the constructor.

Here is the fixed code:

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;
         }
    } 
}
Herman Vicens
Herman Vicens
12,540 Points

Thank you very much. It worked as you said. Now I get it!! Thanks,