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

Ryan Wood
Ryan Wood
888 Points

How to properly write a getter and setter method.

Here is the question: Write a public getter method named GetNumFliesEaten and a public setter method named SetNumFliesEaten for _numFliesEaten.

I am getting errors when compiling. This example is different than the videos in that the "type" I believe is just 'int' instead of another class. I've spent too much time on this one question and would like to reach out for assistance.

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

        public int GetNumFliesEaten()
        {
            return _numFliesEaten;
        }

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

1 Answer

Allan Clark
Allan Clark
10,810 Points

Only a couple small errors in syntax here but you are on the right track. In the SetNumFliesEaten, remove the int as the return type (its setting so it will return void). Also change the type of the value parameter to int. The final method should look like this

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