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

It wont work and i have been trying for a long time

I don't know can someone point me in the right direction

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

        public Frog(int tongueLength)
        {
            TongueLength = tongueLength;
        }
         public bool EatFly(int distanceToFly)
        {
            bool reach;
            if ( distanceToFly <= tongueLength)
            {
                reach = true;
            }
            else ()
            {
                reach = false;
            }

             return reach;
        }
    }
}

You have to put TongueLength because it is global and can be used anywhere inside the class. and you have to remove the parenthesis at the end of else.

1 Answer

Antonio De Rose
Antonio De Rose
20,884 Points
//the approach is fine, almost there
//if not for 2 mistakes

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

        public Frog(int tongueLength)
        {
            TongueLength = tongueLength;
        }
         public bool EatFly(int distanceToFly)
        {
            bool reach;
            if ( distanceToFly <= tongueLength) //error number 1, can you access tongueLength, 
//it has got a different scoping, that is of a different function, 
//you cannot access a variable which is residing in a different function, 
//but you can access the property / variable which is mentioned, just after the class Frog
            {
                reach = true;
            }
            else () //error number 2, why do you need parenthesis, just after else
            {
                reach = false;
            }

             return reach;
        }
    }
}