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 Method Overloading

Li Yu
Li Yu
2,067 Points

C# return type

What is wrong with my return type?

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

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

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

        public readonly int ReactionTime;

        public Frog2(int reactionTime)
        {
          ReactionTime = reactionTime;
            return reactionTime;
        }
        //This is the error" Class, struct, or interface method must have a return type"

    }
}

2 Answers

William Li
PLUS
William Li
Courses Plus Student 26,868 Points

I guess you're in part 1 of the challenge, no?

Add another public readonly integer field to the Frog class named ReactionTime.

It asked you to add a new field to Frog class, it appears that you wrote the code inside the Frog method, which is wrong here.

Where to put the new field? It should be placed under this line public readonly int TongueLength;

namespace Treehouse.CodeChallenges
{
    class Frog
    {
        public readonly int TongueLength;
        public readonly int ReactionTime;   // add the ReactionTime field

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

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

hope it helps.

Li Yu
Li Yu
2,067 Points

It works, thanks William.

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

    public Frog(int tongueLength)

    {
        TongueLength = tongueLength;

    }

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

}