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 trialAndrew Hawke
1,115 PointsAn object reference is required to access non-static member
Stuck on frog code challenge regarding loops. Get an error "An object reference is required to access non-static member".
Can't adress TongueLength to use it in my loop for whatever reason.
namespace Treehouse.CodeChallenges
{
class FrogStats
{
double total;
public static double GetAverageTongueLength(Frog[] frogs)
{
for(int index = 0; index < frogs.Length; index++)
{
total += Frog.TongueLength;
}
}
}
}
namespace Treehouse.CodeChallenges
{
public class Frog
{
public int TongueLength { get; }
public Frog(int tongueLength)
{
TongueLength = tongueLength;
}
}
}
1 Answer
Steven Parker
231,269 PointsThe name "Frog
" is the class itself, not an instance.
You could build a reference to an instance by using the argument "frogs
" and subscripting it ("[]
") with your loop index.
Andrew Hawke
1,115 PointsAndrew Hawke
1,115 PointsThis is by far the hardes code challenge for me, because i don't have a slightest idea how to pull it off. I just don't understand. In order to count average length, i need to adress tongueLength from the Frog class, but no matter how i try to do this, i get the compiler error, and i really don't understand. Shouldnt the public TongueLength be accessible from every other class? I'm just stuck and i don't know what to do anymore.
Update: Oh, i figured it out. Just had to do a bit of thinking. Thanks for the tip.
Steven Parker
231,269 PointsSteven Parker
231,269 PointsSometimes it only takes a different perspective.