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 Auto-Properties

Use the private access modifier to make it so that classes other than Frog can get the value of NumFliesEaten but can’t

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

    public int NumFliesEaten{get;set;}
    {
         return _numFliesEaten;
    }

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

}

}

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

        public int NumFliesEaten{get;set;}
        {
             return _numFliesEaten;
        }

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

    }
}

1 Answer

Steven Parker
Steven Parker
231,248 Points

When you passed task 1, you may have been closer to the solution. The code shown here won't pass task 1, so I'm not sure what you had before you started on task 2.

As part of task 1, the backing field should have been removed, but it's still here plus it looks like a new setter method has been added. And the auto property now has function body, which is not allowed.

I would suggest starting over with task 1, and when that is done correctly task 2 will only require the addition of a single keyword.