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

John Paige
John Paige
7,436 Points

Accessor Method Challenge

Frog.cs(10,19): error CS1014: A get or set accessor expected
Compilation failed: 1 error(s), 0 warnings

This is the error message I'm getting. I'd appreciate some insight.

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


        public int NumFliesEaten 
        {
           public GetNumFliesEaten()
        {
         return _numFlieEaten;   
        }

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

    }

}

2 Answers

Steven Parker
Steven Parker
231,236 Points

:point_right: I spotted 5 issues:

  • on line 8 you appear to still have the original "NumFliesEaten" definition (but without the ";")
  • on line 9 you have an open brace starting a unneeded block that is never closed
  • on line 10 you forgot to indicate the return type of GetNumFliesEaten()
  • on line 12 you spelled "_numFlieEaten" without an "s"
  • on line 15 you used "NumFliesEaten" as the type of "value" (which should probably be int)
John Paige
John Paige
7,436 Points

Thanks. Here is my revised code. Unfortunately, It still isn't passing.

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


        public int NumFliesEaten;

           public int GetNumFliesEaten()
        {
         return _numFliesEaten;   
        }

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

    }

}

//Frog.cs(15,55): error CS1525: Unexpected symbol `value', expecting `)', `,', `[', `]', or `='
Compilation failed: 1 error(s), 0 warnings
Steven Parker
Steven Parker
231,236 Points

You're almost there.

In task 1, you were supposed to change NumFliesEaten into a private field. You created the private field, but you forgot to remove the public one.

On line 15 you added "int", but you forgot to remove "NumFliesEaten".

John Paige
John Paige
7,436 Points

Thanks. My final revision passed the challenge.

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


     //   public int NumFliesEaten;

           public int GetNumFliesEaten()
        {
         return _numFliesEaten;   
        }

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

    }

}
mattmilanowski
mattmilanowski
15,323 Points

First count your brackets { } I think you have an extra } close bracket. After this I believe it will be expecting you to have the words 'get' and 'set' in place of existing methods like 'SetNumOfFliesEaten'.

Steven Parker
Steven Parker
231,236 Points

:x: This challenge is about accessor methods for fields.

The keywords "get" and "set" refer to properties which will be discussed in a later video.

John Paige
John Paige
7,436 Points

I went ahead and double checked my brackets, then swapped the "GetNumFliesEaten" and "SetNumFliesEaten" terms for "get" and "set". I actually corrected the brackets prior to posting my code because the "unexpected symbol: }" error popped.

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


        public int NumFliesEaten 
        {
           get
        {
         return _numFlieEaten;   
        }

        set
        {
         _numFliesEaten = value;   
        }


    }

}
//On to the get and set methods, after i changed the methods to
// simply "get" and "set", it came up with this exception:
Frog.cs(22,246): error CS1525: Unexpected symbol `end-of-file'
Compilation failed: 1 error(s), 0 warnings

Then i added a '}'

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


        public int NumFliesEaten 
        {
           get
        {
         return _numFlieEaten;   
        }

        set
        {
         _numFliesEaten = value;   
        }

        }
    }

}
//Now this exception:
Frog.cs(12,17): error CS0103: The name `_numFlieEaten' does not exist in the current context
Compilation failed: 1 error(s), 0 warnings