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#

Anyone have issues with C# methods challenge: https://teamtreehouse.com/library/c-objects/methods/method-overloading

I can't figure out what is wrong with this code:

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

    public Frog(int tongueLength, int reactionTime)
    {
        TongueLength = tongueLength;
        ReactionTime = reactionTime;
    }

    public bool EatFly(int distanceToFly, int flyReactionTime)
    {
        return TongueLength >= distanceToFly && ReactionTime <= flyReactionTime;
    }

}

}

1 Answer

andren
andren
28,558 Points

There is nothing technically wrong with the code itself, but you have not followed the instructions that the task specified. The error message that pops up when running your code is actually pretty straightforward:

Did you alter the original EatFly method instead of creating a new one?

It is pointing out that you have altered the original EatFly method, which you are not meant to do. The instructions specify that you are to add a new EatFly method that takes two parameters, it does not ask you to modify the existing EatFly method.

If you keep the original EatFly method around and just add your method below it like this:

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

        public Frog(int tongueLength, int reactionTime)
        {
            TongueLength = tongueLength;
            ReactionTime = reactionTime;
        }

        public bool EatFly(int distanceToFly)
        {
            return TongueLength >= distanceToFly;
        }

        public bool EatFly(int distanceToFly, int flyReactionTime)
        {
            return TongueLength >= distanceToFly && ReactionTime <= flyReactionTime;
        }
    }
}

Then you'll be able to pass the challenge.

Worked like a charm. Thanks for your help.