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

Jorge Kalil
Jorge Kalil
1,832 Points

How will I use the distanceToFly in this example?

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.

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

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

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

4 Answers

andren
andren
28,558 Points

When you define parameters for a method you do so by first specifying their type and then the name, in your method declaration you only specify the name, not type. You also create the distanceToFly variable again within the method which is wrong, the variable is created automatically when it is specified as a parameter. Also keywords like public and private is only used when creating field variables (variables that are part of a class) not regular variables found within a method.

Fixing those two mistakes results in this code:

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

        public bool EatFly(int distanceToFly) // Specify that distanceToFly is of type int
           { 
            // Removed redundant distanceToFly declaration
            bool canreach = distanceToFly < TongueLength;
            return canreach;
           }

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

Which will allow you to pass the challenge.

Jorge Kalil
Jorge Kalil
1,832 Points

Thank you so much!!!

Jorge Kalil
Jorge Kalil
1,832 Points

I fixed the issues you have highlighted, thank you. Still get the same error.

william williams
william williams
2,912 Points

he forgot <= // here is mine i wrote:

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

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

        public bool EatFly(int distanceToFly)
        {
         bool valid = distanceToFly <= TongueLength;

            return valid;

        }
    }
}

moved around your code to make it easier for me to read but should be this:

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

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

        public bool EatFly(int distanceToFly)
           { 
            // fixed equation
            bool canreach = distanceToFly <= TongueLength;
            return canreach;
           }      
    }
}