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

I don't understand which value is passed in for the constructor to initialize the TongueLength object with. Help?

In part of the code challenge on Initialization, I rewatched the video and I don't see where the values are the constructor would initialize with. I thought that

TongueLength = tonguelength; as in the video, but this doesn't seem to be the case and I don't understand why. Also, what value is provided by the Code Challenge for the constructor to initialize the object with in any case?

I'm drawing a mental blank here. Thanks in advance!

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

        }
    }
}

1 Answer

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi there, Ra Bha! Again, capitalization comes into play. If you check the video, the properties Width and Height are capitalized. But the parameters for the Map constructor are not. In the constructor, we used width and height. Take a look around 2:06 of the previous video.

So my solution to the second and third parts of this challenge looked a lot like yours:

namespace Treehouse.CodeChallenges
{
    class Frog
    {
        public readonly int TongueLength;
        public Frog(int tongueLength)  // This is the method that is run when we do new Frog()
        {
            TongueLength = tongueLength;   // set the property to the value we passed in
        }
    }
}

The constructor is the method that runs whenever you use the new keyword. So Map map = new Map(3, 7) runs the Map constructor and the width parameter of the constructor gets a value of 3, the height parameter of the constructor gets a 7.

So if I were to do Frog sillyFrog = new Frog(20); it run the constructor and pass in 20 to tongueLength, which in turn gets assigned to the property TongueLength.

Hope this helps! :sparkles:

P.S. Capitalization is super important! :smiley: