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

Arturs Dubra
Arturs Dubra
4,748 Points

Help needed with computed properties C#

These computed properties confuses me.

My guess is that I need to create 1-2 private fields(I have no idea witch ones) Then get the information about Square area out of them.(Again no idea how code this properly)

I would appreciate any help.

Square.cs
namespace Treehouse.CodeChallenges
{
    class Square : Polygon
    {

        public double SideLength { get; private set; }

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

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

2 Answers

anil rahman
anil rahman
7,786 Points

Not done this part yet but managed to figure it out for you :)

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

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

      public double Area
      {
          get
          {
              return SideLength * SideLength; //you were so close
          }

      }
    }
}

You missed out the area equation

Ole Vølund Skov Mortensen
Ole Vølund Skov Mortensen
27,842 Points

had problems understanding it as well. now that i see it, i feel so stupid for not remembering that bit about SideLength * SideLength = Area; and this was basic school math for me and i didn't remember it!!! Man, i got to brush up on my math skills now