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 Methods

Philip Collins
Philip Collins
13,249 Points

Write a method inside the Frog class named EatFly. It should take a single integer parameter named distanceToFly and ret

Why is this not running as a correct answer.. :/

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

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

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

2 Answers

Hello!

As the task said, it should return a bool value. Your function header doesn't have a type. Functions can either be void or have a return type.

public void FunctionThatDoesntReturnAnything() 
// or
public string FunctionThatReturnsAString() 

You should add the return type of the function to the function header.

You may be confused about the Frog "function". That's the constructor. The constructor doesn't have a return type because it's the function that instantiates the class. The constructor always has the same name (and capitalization) as the class itself.

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

Hope it helped!

Jakub Krajniak
Jakub Krajniak
5,351 Points

I don't understand why it dosent work:

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

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

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

I have message "Did you create a parameter in your method named distanceToFly?" And yes i create this parametr