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

Derek Aldana
Derek Aldana
1,095 Points

Computed Propeties

I have watched the video a couple of times now and I cant figure this method of coding out. I dont get it. I need to write the computed property. I have tried multiple ways now and I cant get it.

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


        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

A computed property is essentially just a method that gets treated like a property. In the sense that it can be used exactly like a property would be used normally but when you reference it you don't just pull out a value already stored in it like you do with normal properties, you actually run a method and get what the method ends up returning.

You place the method you want to run in brackets directly following the get keyword. So if you for example wanted a computer property that was called TwoPlusTwo that simply calculated what 2+2 is each time it was called it would be written like this:

public int TwoPlusTwo {
    get {
        return 2 + 2;
    }
}

That computed property would of course return 4, and while it's a fairly useless computed property it does show you how computed properties are structured. In order to calculate the area of a square you multiply the sides together, so the solution to this challenge simply looks like this:

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

You don't need to define any read only properties or anything like that to solve this challenge.

Derek Aldana
Derek Aldana
1,095 Points

Ok cause it kept asking if my variable was a readonly which is why I kept doing that. Thanks a bunch!!!! Cleared it up nicely.

Ashenafi Ashebo
Ashenafi Ashebo
15,021 Points

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

    public double Area { // This is a property but treated as the method because it is a computed property.
        get {
            return SideLength * SideLength; 
        }
     }

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

}

The computed property needs to look more like this. It's important to include the return; public double Area { get { return SideLength * SideLength ; } }