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

Cannot use operand "+=" for the frog code challenge for some reason

FrogStats.cs(11,17): error CS0019: Operator +=' cannot be applied to operands of typedouble' and `Treehouse.CodeChallenges.Frog' That is the error I am getting.

I tried using + just to test and it seems like any operand I use causes an error.

Not sure what I am doing wrong

FrogStats.cs
namespace Treehouse.CodeChallenges
{
    class FrogStats
    {
        public static double GetAverageTongueLength(Frog[] frogs)
        {   double result;
            int num = frogs.Length;

            for(int i=0; i< frogs.Length; i++)
            {
                result += frogs[i];
            }
            result /= num;

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

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

1 Answer

Steven Parker
Steven Parker
231,248 Points

The "preview" button error message is telling you that you're trying to add the frog itself, so instead use just the TongueLength:

                result += frogs[i].TongueLength;

The other issue is that you cannot use the '+=' operator on variables that haven't been initialized (only declared), so give "result" an initial value:

            double result = 0;

Thank you very much that solved it!.