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 trialAli Dahud
3,459 PointsBummer: Did you initialize the field 'numSides' to 4 in the base constructor? Why?
why am i getting this error?
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(numSides=4)
{
}
}
}
2 Answers
Shlomi Bittan
6,718 PointsHi Ali, In this challenge you were asked to initialize the 'SideLength' field of the Square class, and call the base construcotor to set the value of numSides to 4.
-
Setting values to named parameters is by the ':' sign NOT the '=' sign. So....:
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; } } }
Ali Dahud
3,459 PointsSteven Parker could you please explain to me that why doesnβt it take the parameters of the super class? I hope I was understood
Steven Parker
231,261 PointsThe constructor for the super (base) class (Polygon) takes a number of sides, that's the "4" in "base(4)".
But the constructor for Square takes a side length parameter, that's just the way they are defined.