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 Object-Oriented Programming Initialization

leosa
PLUS
leosa
Courses Plus Student 4,622 Points

Error

I keep getting an error, but I am not sure what to type. Here is my error: Frog.cs(7,16): error CS1520: Class, struct, or interface method must have a return type Compilation failed: 1 error(s), 0 warnings

Help is greatly appreciated.

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

        public TongueLength()
        {
        }
    }
}

2 Answers

Steven Parker
Steven Parker
231,275 Points

Any method other than a constructor must have a return type, and none is given here.

But constructors must have the same name as the class, and since the class is "Frog" and the method name is "TongueLength" the compiler does not consider this to be a constructor and is insisting that a return type must be present.

To create a constructor, you'll have to change the name.

Steven Parker
Steven Parker
231,275 Points

Erik is correct, but it's tricky when you use the same name as the field for your parameter. I'd suggest naming the parameter with the "camel case" version of the name, which makes the assignment less complicated. I believe that's also the naming convention used in the course examples.

I case you're not familiar with the term, the "camel case" version would start with a lower-case letter.

Erik Gabor
Erik Gabor
6,427 Points

James you have to assign a value for the readonly int TongueLength. You can do it also in the constructor.