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

Herman Vicens
Herman Vicens
12,540 Points

Error CS0019 Cannot use the "+" in type double....

I am expecting a type double in the frogs[] array but something else is happening and I don't see it. Thanks for your help.

FrogStats.cs
namespace Treehouse.CodeChallenges
{
    class FrogStats
    {
        public static double GetAverageTongueLength(Frog[] frogs)
        {
            double acum = 0.0;

            for (int i = 0; i < frogs.Length; i++ )
            {
                acum = acum + frogs[ i ] ;  
                System.Console.WriteLine(frogs[i]); 
            }
            return acum / frogs.Length;
        }
    }
}
Frog.cs
namespace Treehouse.CodeChallenges
{
    public class Frog
    {
        public int TongueLength { get; }

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

3 Answers

Chris Jones
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Chris Jones
Java Web Development Techdegree Graduate 23,933 Points

Hey Herman,

You're very close :).

acum = acum + frogs[ i ] gets a Frog object and tries to add it to a double variable. You want to get the Frog's TongueLength property and add it to the acum variable. However, the TongueLength property is an int and acum is a double, so you'll want to cast the TongueLength property to a double.

Give it a shot and let me know what you come up with. I'm happy to help with any more questions :).

andren
andren
28,558 Points

The Frogs array does not contain numbers, but Frog objects. So Frogs[i] will actually pull out a Frog object.

If you look at the "Frog.cs" file you can see that the Frog class has a property called TongueLength. That is where the length of their tongue is stored. So you have to reference that property after you pull out the Frog object in the loop.

Like this:

namespace Treehouse.CodeChallenges
{
    class FrogStats
    {
        public static double GetAverageTongueLength(Frog[] frogs)
        {
            double acum = 0.0;

            for (int i = 0; i < frogs.Length; i++ )
            {
                acum = acum + frogs[ i ].TongueLength;
                System.Console.WriteLine(frogs[i]); 
            }
            return acum / frogs.Length;
        }
    }
}
Herman Vicens
Herman Vicens
12,540 Points

Hey Chris!

Thank you for the help. I am beginning to get it! Your explanation was right on.