C# C# Objects Methods Methods

Matthew Cushing
Matthew Cushing
12,048 Points

Don't understand why I'm failing this challenge. Could somoene see if I'm answering it correctly?

It seems it attached my code to the post. The challenge was:

Write a method inside the Frog class named EatFly. It should take a single integer parameter named distanceToFly and return a bool value. It should return true if the frog can reach the fly with its tongue, and false otherwise.

And the result I get is:

Bummer! Did you create a method named EatFly that returns a bool value?

I don't see what I'm getting wrong. It doesn't tell me I need to name the boolean value that is returned anything specific in the challenge task. Is this a bug or am I missing something right in front of my eyes?

Thanks for any help, I appreciate it! Matt

Frog.cs
namespace Treehouse.CodeChallenges
{
    class Frog
    {
        public readonly int TongueLength;

        public Frog(int tongueLength)
        {
            TongueLength = tongueLength;
        }

        bool EatFly(int distanceToFly)
        {
            bool eat = distanceToFly <= TongueLength;
            return eat;
        }
    }
}

2 Answers

Livia Galeazzi
PRO
Livia Galeazzi
Java Web Development Techdegree Graduate 19,902 Points

Add 'public' before 'bool' to your method signature. If your method isn't public, the tests of the code challenge probably can't see it.

Matthew Cushing
Matthew Cushing
12,048 Points

I had done this initially and is said that it failed to compile every time I tried adding public before it.

EDIT: For some reason it worked this time.....I'm not sure why it wasn't working previously. I also thought that would be the only thing but for some reason it would just tell me I failed because it wouldn't compile. Either way, thanks for having me try it again. Weird....

andren
andren
28,405 Points

While the task does not explicitly state this it requres the method to be public. If you don't specify the access level of a method then it defaults to private.

So all you need to do is add public at the start of the method declaration like this:

public bool EatFly(int distanceToFly)

And then your code will pass.

Matthew Cushing
Matthew Cushing
12,048 Points

Wasn't sure who answered first but his/her comment was on top so I'm assuming he/she was first to reply and gave him/her the "best answer". Odd though cause I had actually tried putting public in front and it kept failing me for "not being able to compile". Odd, but thanks as it worked this time. Maybe I just needed to exit and re-enter the page?

andren
andren
28,405 Points

Livia did indeed answer before me, our answers were 1 minute apart. Sometimes the challenges can be a bit buggy, so it is entirely possible that simply reloading it helped.

In my experience if your code does not compile and you are quite certain you have no typos or other issues then it usually helps to reset the task entirely, that seems to clear out any bugs most of the time. Though I have not personally ran into bugs like that often.