C# C# Objects Methods Methods

Sarah Newton
Sarah Newton
2,179 Points

Cannot understand what I am required to do to generate the return value in the task.

The task felt unclear as to how to go about getting the return value. Not sure if I need to initialize an instance of tongueLength and assign it a value and then calculate a boolean return value based upon the distance value being less than tongueLength.

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

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

        public bool EatFly(int distanceToFly)
        {
            bool canReachFly = distanceToFly < Frog.tongueLength;
            return canReachFly;
        }
    }
}

2 Answers

KRIS NIKOLAISEN
KRIS NIKOLAISEN
53,537 Points

If you click preview you'll see

error CS0117: `Treehouse.CodeChallenges.Frog' does not contain a definition for 'tongueLength'

but it does have a definition for TongueLength. So if you change

bool canReachFly = distanceToFly < Frog.tongueLength;

to

bool canReachFly = distanceToFly < Frog.TongueLength;

you'll see error CS0120: An object reference is required to access non-static member `Treehouse.CodeChallenges.Frog.TongueLength'

If you then change to

bool canReachFly = distanceToFly < this.TongueLength;

or just

bool canReachFly = distanceToFly < TongueLength;

you'll pass

Sarah Newton
Sarah Newton
2,179 Points

Perfect answer, thanks!