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 trialMuhammad Bah
852 PointsDefining Square class as inner class of the Polygon class.
I totally do not understand what I'm being asked to do. Please help. Thank you.
namespace Treehouse.CodeChallenges
{
class Polygon
{
public readonly int NumSides;
Polygon square = new Square();
public Polygon(int numSides)
{
NumSides = numSides;
numSides = 4;
}
}
class Square : Polygon
{
public readonly int SideLength;
public Square (int sideLength) : base (numSides)
{
SideLength = sideLength;
}
}
}
4 Answers
Steven Parker
231,236 PointsIt looks like you have a few issues:
You don't need this line, and that variable is about to be disposed anyway:
numSides = 4; // remove this line
The instructions also don't say to add a square property to Polygon. In fact, you should make no changes to the definition of Polygon. It should remain as-is and your additions will only be to the new class Square. So you should also remove this line:
Polygon square = new Square();
And as Jon suggested, you need to pass the value to the base constructor:
public Square (int sideLength) : base(4)
Muhammad Bah
852 Pointsnamespace Treehouse.CodeChallenges
{
class Polygon
{
public readonly int NumSides;
Polygon square = new Square();
public Polygon(int numSides)
{
NumSides = numSides;
}
}
class Square : Polygon
{
public readonly int SideLength;
public Square (int sideLength) : base(4)
{
SideLength = sideLength;
}
}
}
Jon Wood
9,884 PointsI just ran your code in the challenge and it passed. What error are you seeing?
Muhammad Bah
852 PointsIt says "Bummer! Did you define your Square class as an inner class of the Polygon class? Be sure to define the Square class before or after the definition for the Polygon class."
Jon Wood
9,884 PointsOh! I only ran what you had in the Square
class. Remove this line Polygon square = new Square();
from the Polygon
class and it will pass.
Muhammad Bah
852 PointsThank you so much man that worked!
Jon Wood
9,884 PointsYou are so close!
You have all of it, other than the challenge wanting you to initialize the base class with numSides
set to 4. When you call base()
on a subclass constructor you are calling the constructor of the class you're inheriting from. In this case you're just passing in the constant 4.
Muhammad Bah
852 PointsI did what you guys said and the issue still persists.
Steven Parker
231,236 PointsTry starting over and make sure not to change the original code, just add the new class.
Muhammad Bah
852 PointsDid that and I'm still getting the same issue.
Steven Parker
231,236 PointsIt looks like you're still modifying the original code. Don't put anything in the Polygon class!