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

Tony Lawrence
Tony Lawrence
3,056 Points

Confusion with the second half of the challenge for Getters and Setters

Hi all,

I'm having trouble in understanding what I did wrong. It asks to have GetNumFliesEaten and SetNumFliesEaten in a Getters and Setters setup. Here's what I have so far: namespace Treehouse.CodeChallenges { class Frog { private int _numFliesEaten;

    private int NumFliesEaten;


    public NumFliesEaten int GetNumFliesEaten()
    {
        return _numFliesEaten;
    }

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

}

I even have GetNumFliesEaten and SetNumFliesEaten on separate lines to be declared, but it states that these variables are not required to be stated. When presented as set above, I get errors stating that NumFliesEaten was placed as a field not a type on my Getter and Setter block. I'm not sure what I'm missing from the challenge.

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

        private int NumFliesEaten;


        public NumFliesEaten int GetNumFliesEaten()
        {
            return _numFliesEaten;
        }

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

1 Answer

Steven Parker
Steven Parker
231,261 Points

:point_right: You used NumFliesEaten several times, but it is no longer part of the project.

In task 1, it was changed to _numFliesEaten. It's no longer needed and should be removed.

Your getter should be simply a "public int" type, to match the type of the variable that it is "getting".

Your setter has both "void" and "int" but it cannot be both (it's just "void"). And the value parameter given to your setter needs to be the same type as the _numFliesEaten variable that it gets assigned to.

Tony Lawrence
Tony Lawrence
3,056 Points

Following your advice and making the edits, this is what I have so far:

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

    public int GetNumFliesEaten()
    {
        return _numFliesEaten;
    }

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

}

But I'm getting this error as currently presented: Frog.cs(12,42): error CS1001: Unexpected symbol `)', expecting identifier Compilation failed: 1 error(s), 0 warnings

Which I know has to do with the Setter's parameter.