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

Idan Amar
Idan Amar
798 Points

Computed Properties

Its probably way off wrong, i just dont understand it.. Thank you.

Square.cs
namespace Treehouse.CodeChallenges
{
    class Square : Polygon
    {
        public double SideLength { get; private set; }

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

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

1 Answer

andren
andren
28,558 Points

It's actually not way off. There are only two issues, and only one of those are code related.

The first issue is this line:

public Square Area

In that line you declare a property called Area with a return type of Square. The issue is that it does not actually returns a Square, but a double. So you need to change the return type to reflect that.

The second issue is your math. The instructions tell you to return the area of the square. That is done by squaring the side length, not by multiplying the length by 4.

Squaring in case you don't know basically just means to multiply a number by itself. So SideLength * SideLength is what the Area property should return.

If you fix those two issues then your code will work.