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

Frog.cs(8,20): error CS0103: The name `_numFilesEaten' does not exist in the current context Frog.cs(13,29): error CS010

Hello to everyone!

I can't figure out what the problem is. Please, help. Variable is private but it should be accessible by methods inside the class. But it's now working.

Frog.cs
namespace Treehouse.CodeChallenges
{
    class Frog
    {
        private int _numFliesEaten;
        public int GetNumFilesEaten() 
        {
            return _numFilesEaten;
        }

        public void SetNumFilesEaten(int numFilesEaten) 
        {
            numFilesEaten = _numFilesEaten;
        }
    }
}

2 Answers

The reason you are getting the "does not exist" error is because of a few typos in your code. You have been writing "numFilesEaten" instead if "numFliesEaten" (mixed up the I and the L).

Once the type has been fixed then all you need to do is switch the variables around in the setter method so that the private property in the class is getting set to the variable passed in instead of the variable passed in getting set to the private property. Like this:

namespace Treehouse.CodeChallenges
{
    class Frog
    {
        private int _numFliesEaten;
        public int GetNumFliesEaten() 
        {
            return _numFliesEaten;
        }

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

Connor, thank you very much!