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

C# CS0120 - non-static member reference error

I review the answer on another CS0120 error question, but I'm not following it. The variable name is exactly the same as the private variable I set as a double before the method. I originally thought this was because I was trying to reference the "Frog" property "TongueLength", but when I removed that code, it looks like its just the name of the variables I'm using. I'm getting the below error on all of the variables I'm calling.

error CS0120: An object reference is required to access non-static member `Treehouse.CodeChallenges.FrogStats._totalTongueLength'

Sorry, I thought it was downloaded with the question. Odd, pretty sure I checked that. In any event, see below:

FROG STATS CLASS

namespace Treehouse.CodeChallenges { class FrogStats { private int _tongueLength; private int _sumOfTongueLength; private double _avgTongueLength; private int _numberOfFrogs;

    public static double GetAverageTongueLength(Frog[] frogs)
    {

          for (int i = 0; i < frogs.Length; i++)
          {

             Frog frog = frogs[i];
             _tongueLength = frog.TongueLength;
             _sumOfTongueLength = _tongueLength + _sumOfTongueLength; 

          }
      _numberOfFrogs = frogs.Length += 1;
      _avgTongueLength = _sumOftongueLength /= _numberOfFrogs;


    }
}

}

FROG CLASS

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

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

}

For some reason it isn't formatting the first couple lines of code properly?!

1 Answer

ok, looks like I just needed to add "static" in front of the field type that took care of the CS0120 error.