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 Loops and Final Touches For Loops

Karlijn Willems
Karlijn Willems
9,961 Points

How can I get the TongueLenght from the Frog.cs file into the for loop to return the average?

I think I got the loop right, but I can't figure out how to get the variable of the TongLength into the for loop. At least, I assume this is necessary. I think from there I can, I hope :), figure out how to return the average, but all the help is welcome atm... please check out my code if you can help, thank you in advance!

FrogStats.cs
namespace Treehouse.CodeChallenges
{
    class FrogStats
    {
        double _frog = 0;

        public static double GetAverageTongueLength(Frog[] frogs)
        {

            for (int index = 0; index < frogs.Length; index++)
            {
                double _frog = tongueLength + _frog; 
            }

            double average = _frog / frogs.Length;
            return average;
        } 

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

        public Frog(int tongueLength)
        {
            TongueLength = tongueLength;
        }

    }
}

3 Answers

Steven Parker
Steven Parker
231,268 Points

The name of the property is "TongueLength" (with a capital "T"). And you need to reference that property through one of the "frogs".

But you also can't access your "_frog" variable after the loop if it is declared inside the loop. Plus you can't add to it and declare it at the same time. For both of those reasons, you'll want to declare it before the loop. But you probably don't want it to be a class variable.

Karlijn Willems
Karlijn Willems
9,961 Points

Thanks for helping out! I had been trying some stuff so yes, I need to use the capital T and I declared _frog twice now I see, so I deleted "double" before the _frog in the for loop... what do you mean with that I don't want is to be a class variable? do I need to put private in front of it?

I am watching back the video's on properties, but I still haven't figured it out...

(I think I voted -1 by accident, sorry! still new to this platform. I think now it got a +1 :))

Steven Parker
Steven Parker
231,268 Points

What I meant about "_frog" was that you can declare it inside the method, before the loop. It doesn't need to be part of the class itself (public or private).

To access the property, you need to get from a "frogs" instance, like this: "frogs[index].TongueLength".

Karlijn Willems
Karlijn Willems
9,961 Points

Got it! =) thank you very much!!!