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 Encapsulation with Properties Computed Properties

Could not compile

I have been working with this code for a while now, and no matter what i try it will not compile can I please get some help or explaining on what I did wrong.

Square.cs
namespace Treehouse.CodeChallenges
{
    class Square : Polygon
    {
        public double SideLength

        public double area
        { 
            get
            {
                return SideLength * SideLength; 
            }
        }

        public Square(double sideLength) : base(4)
        {
            SideLength = sideLength;
        }
    }
}
Polygon.cs
namespace Treehouse.CodeChallenges
{
    public class Polygon
    {
        public int NumSides { get; private set; }

        public Polygon(int numSides)
        {
            NumSides = numSides;
        }
    }
}

3 Answers

andren
andren
28,526 Points

You (accidentally I presume) have removed the get and set declaration for the SideLength property, when the task starts it looks like this:

public double SideLength { get; private set; }

And you are not meant to change that line at all.

In addition you have named the property area instead of Area.

If you restore the SideLength line back to what it's supposed to be, and fix the typo in the Area property name like this:

namespace Treehouse.CodeChallenges
{
    class Square : Polygon
    {
        public double SideLength { get; private set; } // Fixed this line

        public double Area // area changed to Area
        { 
            get
            {
                return SideLength * SideLength; 
            }
        }

        public Square(double sideLength) : base(4)
        {
            SideLength = sideLength;
        }
    }
}

Then your code will work.

Thank you for the answer, but in the video Jeremy gets rid of the auto property. Please explain.

andren
andren
28,526 Points

In the video he changes the Location property from not having a computed getter, to having one. In this challenge you are not asked to change an existing property but to add a new one. The SideLength property and the Area property are entirely independent. You don't have to do anything to SideLength in order to create the Area property.

okay thank you!