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 trialTony Lawrence
3,056 PointsConfusion with the second half of the challenge for Getters and Setters
Hi all,
I'm having trouble in understanding what I did wrong. It asks to have GetNumFliesEaten and SetNumFliesEaten in a Getters and Setters setup. Here's what I have so far: namespace Treehouse.CodeChallenges { class Frog { private int _numFliesEaten;
private int NumFliesEaten;
public NumFliesEaten int GetNumFliesEaten()
{
return _numFliesEaten;
}
public void int SetNumFliesEaten(NumFliesEaten value)
{
_numFliesEaten = value;
}
}
}
I even have GetNumFliesEaten and SetNumFliesEaten on separate lines to be declared, but it states that these variables are not required to be stated. When presented as set above, I get errors stating that NumFliesEaten was placed as a field not a type on my Getter and Setter block. I'm not sure what I'm missing from the challenge.
namespace Treehouse.CodeChallenges
{
class Frog
{
private int _numFliesEaten;
private int NumFliesEaten;
public NumFliesEaten int GetNumFliesEaten()
{
return _numFliesEaten;
}
public void int SetNumFliesEaten(NumFliesEaten value)
{
_numFliesEaten = value;
}
}
}
1 Answer
Steven Parker
231,198 PointsYou used NumFliesEaten several times, but it is no longer part of the project.
In task 1, it was changed to _numFliesEaten. It's no longer needed and should be removed.
Your getter should be simply a "public int
" type, to match the type of the variable that it is "getting".
Your setter has both "void
" and "int
" but it cannot be both (it's just "void
"). And the value parameter given to your setter needs to be the same type as the _numFliesEaten variable that it gets assigned to.
Tony Lawrence
3,056 PointsTony Lawrence
3,056 PointsFollowing your advice and making the edits, this is what I have so far:
namespace Treehouse.CodeChallenges { class Frog { private int _numFliesEaten;
}
But I'm getting this error as currently presented: Frog.cs(12,42): error CS1001: Unexpected symbol `)', expecting identifier Compilation failed: 1 error(s), 0 warnings
Which I know has to do with the Setter's parameter.