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

Joshua Thao
Joshua Thao
6,439 Points

Set method

I am not sure how the set method is working or why its not working.

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

        public int GetNumFliesEaten
        {
            get{return _numFliesEaten; }
        }


        public int SetNumFliesEaten
        {
            set{_numFliesEaten; }
        } 
    }
}

3 Answers

Cam Richardson
seal-mask
MOD
.a{fill-rule:evenodd;}techdegree
Cam Richardson
Front End Web Development Treehouse Moderator 16,895 Points

Hey Joshua!

You've almost got the right idea, but your issue is that you're mixing properties with accessor methods. You shouldn't use the get and set keywords in methods like that. Additionally your set method is written to return an int, but you're not actually returning anything. Here's a look at the difference between the two:

Property Example:

private int _myValue;
public int MyValue
{
    get { return _myValue; }
    set { _myValue = value; }
}

Accessor Method Example:

private int _myValue;

public int GetMyValue()
{
    return _myValue;
}

public void SetMyValue(int value)
{
    _myValue = value;
}

Take another look at how Jeremy does it in the video. Hopefully these examples helped.

Joshua Thao
Joshua Thao
6,439 Points

Thank you that helped a lot!

Cam Richardson
seal-mask
.a{fill-rule:evenodd;}techdegree
Cam Richardson
Front End Web Development Treehouse Moderator 16,895 Points

No problem. Glad to hear it helped! It's been a while since I've went through the C# course but I think Jeremy goes on to explain Properties in more depth.

Joshua Thao
Joshua Thao
6,439 Points

He does but its just soo much to take in at one time. I was hoping Treehouse would create a Discord that way we don't have to wait the next day or so for a reply