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 Accessor Methods

blake littleton
blake littleton
14,255 Points

I'm not sure what I'm doing wrong

I thought I understood the getter and setter concept. I'm not sure what I'm doing wrong here.

Frog.cs
namespace Treehouse.CodeChallenges
{
    class Frog
    {
        private int _numFliesEaten;

          private int _numFliesEaten;
    }
     public GetNumFliesEaten()
     {
        return _numFliesEaten;
     }

     public void SetNumFliesEaten(int value)
     {
        _numFliesEaten=value;
     }
}

1 Answer

andren
andren
28,558 Points

You seem to grasp the basic concepts just fine, but there are three issues with your code:

  1. You have added the getter and setter outside the body of the class, rather than inside where it belongs.

  2. You have forgotten to add a return type to the GetNumFliesEaten getter method.

  3. You have also added the _numFliesEaten field twice. You should only define it once.

If you move the methods inside the body (within the {} brackets) of the class, add a return type to GetNumFliesEaten, and remove the duplicate _numFliesEaten field then your code will work fine.