Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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
218,639 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
218,639 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.