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#

Christer Monsen
Christer Monsen
3,664 Points

[Solved] Can't seem to figure this one out and don't find solution to challenge 2 of 2

How to make this right?

Because I cant seem to understand whats wrong with this.

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

        public int GetNumFliesEaten GetValue()
        {
            return _numFliesEaten;
        }

        public void SetNumFliesEaten(GetNumFliesEaten value)
        {
            _numFliesEaten = value;
        }
    }
}
Chris Shaw
Chris Shaw
26,676 Points

Hi Christer Monsen,

Which challenge is this for?

Can you give me a link to the code challenge?

Christer Monsen
Christer Monsen
3,664 Points

Task 2 of 2. (Accessor Methods)

Task: Write a public getter method named GetNumFliesEaten and a public setter method named SetNumFliesEaten for _numFliesEaten.

https://teamtreehouse.com/library/c-objects/encapsulation-with-properties/accessor-methods-2

6 Answers

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

        public int GetNumFliesEaten GetValue()
        {
            return _numFliesEaten;
        }

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

This is your code. It look really neat! Two problems, though. One, there's no use to say GetValue() in "public int GetNumFliesEaten GetValue()" . Try getting rid of that GetValue(). Second, you called the parameter for SetNumFliesEaten value, which is a reserved keyword in C#. "value" is like a built-in reserved word in C#, just like how "public" and "void" are. You cannot use them as variable names, or it will cause an error. instead, try giving it a different name that isn't a reserved word. I'm going to use "val" as the new name, but you can call it whatever you want. Otherwise, I really like your code!

EDIT: Also, val's type should be a int, not a GetNumFliesEaten!

Here's the solution (please read the text above this first, please please! :smile:):

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

        public int GetNumFliesEaten()
        {
            return _numFliesEaten;
        }

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

Hope this helps! ~Alex ;smile:

David Axelrod
David Axelrod
36,073 Points

Seems to me that value is a contextual keyword :( https://msdn.microsoft.com/en-us/library/x53a06bb.aspx

Try a different variable name. Otherwise your code looks sharp (hehehe) to me!

Christer Monsen
Christer Monsen
3,664 Points

Thanks very much Alex, but I get message that it will not compile for some odd reason. And thanks that you like my code :) Really appreciated.

No problem :) What's the message? I can help you.

Christer Monsen
Christer Monsen
3,664 Points

Bummer! Your code could not be compiled. Please click on "Preview" to view the compiler errors.

Frog.cs(5,17): error CS0246: The type or namespace name `GetNumFliesEaten' could not be found. Are you missing an assembly reference? Compilation failed: 1 error(s), 0 warnings

Can you press Preview And tell me what that says? Thx

Christer Monsen
Christer Monsen
3,664 Points

Got it! Here is what worked for me :)

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

    public int GetNumFliesEaten()
    {
        return _numFliesEaten;
    }

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

}

Cool! I'm new to C# and I didn't know that you could use value as a parameter. I though is would cause an error :)

Thanks a ton! ~Alex

I can't make my code to work. Any suggestion?

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

    public int GetNumFliesEaten()
    {
        return _numFilesEaten;
    }

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

} }

Never mind, it worked.