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

Carsten Dollerup
Carsten Dollerup
9,278 Points

Is this Challenge faulty?!

How does this not work... Tried (I think) everything. Challenge seems broken to me...

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 * SideLength; }
            }
        }
    }
}
Polygon.cs
namespace Treehouse.CodeChallenges
{
    public class Polygon
    {
        public int NumSides { get; private set; }

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

3 Answers

Steven Parker
Steven Parker
229,645 Points

It looks like you put your property inside the constructor.

Just move it out of the constructor and it should compile and pass the challenge.

Carsten Dollerup
Carsten Dollerup
9,278 Points

Thanks for you help Steven. That did the trick. I'm confused, however, how it makes sense to have the Area computed property within the Polygon class description instead of within the Square class. Esp. as some polygons wouldn't have the correct area calculated from the Area property...? I realize this isn't a problem to the C# programming in and of itself, I just don't get the logic of the Challenge... (?) Am I missing something here? In other words, how would I make sure the correct Area was calculated no matter how many sides to the Polygon object in a "real world situation"? Would I create a method that assesses the number of sides before calculating the Area instead of a computed property?

Steven Parker
Steven Parker
229,645 Points

In this example, the Square class has an Area property but the Polygon class does not.

It wouldn't make sense to attempt to provide an Area for a generic polygon knowing only the number of sides, that's not enough information to do the job in all cases. But we can do it for the Square because we know the side length (and the fact that it is square).

Carsten Dollerup
Carsten Dollerup
9,278 Points

Ok, thanks again, but that wasn't my "issue" :-) My thoughts were that by moving the Area property outside of the Square constructor it's not "locked" to the Square subclass, but I must be missing something here, as judging from your answer it clearly isn't correct how I think of it :-)

Steven Parker
Steven Parker
229,645 Points

You moved it out of the constructor, but not out of the class. It is still a method of the Square class.

Carsten Dollerup
Carsten Dollerup
9,278 Points

Ok. Thanks a lot, Steven! Very, very gratious of you to help me with three answers. Thank you again :-)