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 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,920 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,521 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.