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 Methods Method Overloading

Jeongeui Ji
Jeongeui Ji
3,761 Points

Can't get it...

after the if statement, I dont know what code to put, to express that the tongue is faster or equal to the time in code.

the return type is bool and what i cant get is what to code in the body of the if statement to get the boolean return value.. Thanks a lot in advance.
I just did as following;

Frog.cs
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)
        {   
              if (TongueLength >= distanceToFly)
              {
                  return ReactionTime <= flyReactionTime;
              }

        }


    }
}

1 Answer

Umesh Ravji
Umesh Ravji
42,386 Points

Hi Jeongeui Ji, if you click on preview, you can view the error message.

Frog.cs(18,21): error CS0161: `Treehouse.CodeChallenges.Frog.EatFly(int, int)': not all code paths return a value Compilation failed: 1 error(s), 0 warnings

The problem is that in cases where the first condition evaluates to false, no value will be returned from the method. One way to solve this would be to simply return false at the end, but it is possible to solve this challenge with just one line of code by combining both statements with the and operator (&&).

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