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 trialnave lahav
388 Pointswhats wrong
I did what the instructions told me to do and there's an error
namespace Treehouse.CodeChallenges
{
class Polygon
{
public readonly int NumSides;
public Polygon(int numSides)
{
NumSides = numSides;
numSides=4
}
class Square : Polygon
{
public readonly int SideLength
public Square(int sideLength) : base Polygon(numSides)
{
SideLength=sideLength;
}
}
}
}
3 Answers
Jon Wood
9,884 PointsI think you have the signature of the Square
class inside of the Polygon
class.
Simon Coates
28,694 Pointsit seemed to accept:
namespace Treehouse.CodeChallenges
{
class Polygon
{
public readonly int NumSides;
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 PointsYep, that looks good! Though, calling the base constructor for Polygon
should take in the sideLength
.
Simon Coates
28,694 Pointsum, a single sidelength would only apply to shapes that have all equal sides. a single value would not be useful for a generic polygon.
Jon Wood
9,884 PointsSimon Coates, you're right! I jumped the gun on that. I should have double checked. Thanks for letting me know! :)
Christian Mangeng
15,970 PointsYes, at the moment your Square class is inside the Polygon class. Polygon and Square should be two separate classes, although Square inherits from Polygon. Also, don't forget to add a semicolon at the end of the SideLength field. The initialization of numSides to 4 can be done like that:
public Square(int sideLength) : base(4)
Simon Coates
28,694 PointsSimon Coates
28,694 Pointsyour use of {} is probably wrong (correct indenting would make this clearer). You've added a line of code to the Polygon constructor that you don't need (and omitted the ;), and I'm not sure you need the word 'Polygon' on the line with the Square constructor. I'd assume that if you use base, it knows to use polygon.