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#

Gil Huss
Gil Huss
2,750 Points

C# Objects "Code Challenge" I don't get it...

This is the challenge:

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.

This is my code: namespace Treehouse.CodeChallenges { class Frog { public readonly int TongueLength;

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

//here starts the part i need to figure out...

    public EatFly(int DistanceToFly)
    {    
        int distanceToFly = DistanceToFly ;
            bool  ableToEat = tongueLength >= distanceToFly ;
        return ableToEat ;
    }
}

}

Sorry for those reading this, wanting to hack my address to come and slap me - I'm really new to C# :)

I tried it with: " if(tongueLength >= distanceToFly){ bool ableToEat = True; }"

Doesn't seam to do the trick...

I hope someone could help me out. Thanks, Gil

1 Answer

Steven Parker
Steven Parker
231,210 Points

:point_right: It looks like you forgot to declare the return type of the method.

You wrote:

    public EatFly(int DistanceToFly)

But remember, this method return a bool value, so you need to declare it:

    public bool EatFly(int DistanceToFly)

Fixing that should get you past the challenge, unless the challenge complains about using a different name for the parameter (I would have tested it but you didn't link to the challenge). In that case, you might want to use the next suggestion also (which is otherwise just a general optimization tip).


You don't need to create a variable in a method if it is only used to copy a parameter. Just use the parameter directly, as shown here with "distanceToFly":

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

And you can save one more variable if you directly return the result of the calculation:

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

Finally, there's another syntax for "expression bodied methods" that condenses this even more (but this is probably not introduced until a more advanced course):

    public bool EatFly(int distanceToFly) => tongueLength >= distanceToFly;
Gil Huss
Gil Huss
2,750 Points

thank you so much for your effort! makes sense now that i see it and you were right! Have a great day!