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

using constructor to initialize field

Use constructor to initialize the TongueLength field to the value passed in

Frog.cs
namespace Treehouse.CodeChallenges
{
    class Frog
    {
        public readonly int TongueLength;
       public Frog(int tonguelength)
        {
             TongueLength = tonguelength;
             TongueLength tonguelength = new TongueLength(tonguelength);

        }
    }
}
Shlomi Bittan
Shlomi Bittan
6,718 Points

Hi Dean, Do you have a class named 'TongueLength' that excepts int in its constructor? What is the error you get?

1 Answer

Dane Parchment
MOD
Dane Parchment
Treehouse Moderator 11,023 Points

The problem here, is you are treating the value of the primitive data type (int) tongueLength as though it is a reference to a completely different class.

You have created primitive datatype variables before so how would you assign an integer value to a variable?

Like so correct?

int integerValue = 30;

So if you are told to assign the readonly integer variable TongueLength an integer parameter you created in a constructor wouldn't you just assign TongueLength to that parameter? You aren't referencing a new class so no need for the new className() syntax. Also isn't it odd that there isn't a class called TongueLength that was created for you to reference?

So to solve this problem just do the following:

class Frog
    {
        public readonly int TongueLength;
       public Frog(int tonguelength)
        {
             this.TongueLength = tonguelength;
        }
    }

You were already so close! Keep at it, have fun programming and don't get discouraged, watch earlier c# videos if you aer having trouble remembering things like this.