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

Hitting a wall here...is my code on the right track regarding the computed property?

On this Computed Property code challenge, I see that Area is the property to be computed. Every time the SideLength changes, the Area changes, so I understand that the Area property needs to be written as a computed property. I understand further the computed property is the Area property written as a method with an expanded Getter returning how the Area changes whenever the SideLength changes. Is this understanding correct so far?

Second, I am not quite sure how to think about next steps. In the example, there was a code smell caused by the duplicate definitions of the property Location but I am not quite clear where a similar situation is happening here.

Third, are there changes that are supposed to be made to the Polygon class, and why?

Thanks so much, this one has really challenged me and I'm not sure how to proceed.

Square.cs
namespace Treehouse.CodeChallenges
{
    class Square : Polygon
    {
        public double Area 
        { 
            get; 
            {
            return Area.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;
        }
    }
}

1 Answer

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi there, Ra Bha! You're overthinking this. The area of any square (or rectangle) is the base times the height. We're calling it SideLength here. Because a square always has four equal sides that join at right angles. So if a square has a side length of 4cm then it has an area of 16cm². If it has a side length of 10m then its area is 100m² and so forth. This challenge can be solved by adding a single additional line to the Square class.

public double Area => SideLength * SideLength;

You do not need to alter the getters or setters. Just set the Area property to be the result of multiplying the SideLength by itself.

Hope this helps! :sparkles: