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

evanpavan
evanpavan
5,025 Points

Can someone lend a hand with the code challenge?

I'm getting a compiler error below. How do I introduce a variable in the loop without this error or avoid using a variable altogether?

"FrogStats.cs(11,20): error CS0103: The name `runningTotalTonguelength' does not exist in the current context Compilation failed: 1 error(s), 0 warnings"

namespace Treehouse.CodeChallenges
{
    class FrogStats
    {
        public static double GetAverageTongueLength(Frog[] frogs)
        {                 
            foreach(Frog frog in frogs)
            {
                double runningTotalTonguelength = runningTotalTonguelength + frog.TongueLength;  
            }
            return runningTotalTonguelength / frogs.Length;
        }
    }
}

3 Answers

If you declare a variable outside of a static method, and you want to use it inside of the method, the variable has to be static too.

You can learn more about static classes and static class members variables here.

This code worked for me in the challenge:

namespace Treehouse.CodeChallenges
{
    class FrogStats
    {
        public static double GetAverageTongueLength(Frog[] frogs)
        {
            double runningTotalTonguelength = 0.0;
            foreach(Frog frog in frogs)
            {
                runningTotalTonguelength = runningTotalTonguelength + frog.TongueLength;  
            }
            return runningTotalTonguelength / frogs.Length;
        }
    }
}

You need to declare your variable outside of the scope of the foreach loop, remember that a variable only lives in the scope where it was created.

evanpavan
evanpavan
5,025 Points

Thanks. This is the code that ended up working.

Can anyone tell me why the runningTotalTonguelength variable had to be a static variable? The definition of static methods and classes makes sense but static member variables are not clear to me.

namespace Treehouse.CodeChallenges
{
    class FrogStats
    {
        public static double runningTotalTonguelength = 0;
        public static double GetAverageTongueLength(Frog[] frogs)
        {                 
            foreach(Frog frog in frogs)
            {
                double runningTotalTonguelength = runningTotalTonguelength + frog.TongueLength;  
            }
            return runningTotalTonguelength / frogs.Length;
        }
    }
}