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 9,734 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.