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

Arturs Dubra
Arturs Dubra
4,748 Points

Getters and setters, can't figure out the value I must set.

As far as I understand, I have to get and set the value to _numFliesEaten. I think that I got the value, but I have no clue how to set it!

It feels strange since in the video before challenge there was MapLocation field to which value could be assigned, but here there is no field to which I can assign any value.

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

        public int GetNumFliesEaten()
        {
          return _numFliesEaten;
        }

        public void SetNumFliesEaten()
        {
          _numFliesEaten = ??;
        }



    }
}

2 Answers

Russell Kree
Russell Kree
1,956 Points

Remember you will be setting the _numflieseaten variable from another class using the constructor. You cannot change the private variable so you must equal it to the value passed into the parameter.

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

    public int GetNumFliesEaten()
    {
      return _numFliesEaten;
    }

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



}

}

Why is 'int' required for the getter but not the setter?

Niek Beenen
Niek Beenen
1,066 Points

Mohammad Hanbali int is the return type. A set will not return. So it will be a void.

Yes that was what I was doing wrong! Thanks