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

Travis John Villanueva
Travis John Villanueva
5,052 Points

How to convert my result in to double?

I think i got the idea of For loops but my problem is the return as they need a double. Apparently, my return type is not a double type. How can i concvert my return type to double?

FrogStats.cs
namespace Treehouse.CodeChallenges
{
    class FrogStats
    {
        public static double GetAverageTongueLength(Frog[] frogs)
        {
            for(int i=0; i<frogs.Length; i ++)
            {
                 Frog frog = frogs[i];
                 double result = (double)frog;
                 return result;
            }
        }
    }
}
Frog.cs
namespace Treehouse.CodeChallenges
{
    public class Frog
    {
        public int TongueLength { get; }

        public Frog(int tongueLength)
        {
            TongueLength = tongueLength;
        }
    }
}
Steven Parker
Steven Parker
231,184 Points

It looks like you asked about this same task in another question. I left you a few hints there you might find helpful.

Shadab Khan
Shadab Khan
5,470 Points

Hi Travis,

You are trying to convert an object reference 'Frog' to double type, which is invalid. I can see your logic isn't correct either; you need to sum up all frog tongue lengths first using the for loop and then divide that by the total number of frogs in the array. Refer to my code below (Great effort though!)

namespace Treehouse.CodeChallenges
{
    class FrogStats
    {
        public static double GetAverageTongueLength(Frog[] frogs)
            {
            //initialise totalTongueLength variable.
            //This variable will be used to store the totalTongueLength of all frogs in for loop

            double totalTongueLength = 0.0;

            //loop through the list of frogs

                    for(int i=0; i<frogs.Length; i++)
                    {
                //Get a reference to the frog object from the array

                        Frog frog = frogs[i];

                //sum up total tongue length of all frogs below

                        totalTongueLength += frog.TongueLength;
                    }

            //finally return the average
            return ( totalTongueLength / frogs.Length );
            }
    }
}

1 Answer

Steven Parker
Steven Parker
231,184 Points

You can't convert "frog" because it is not a value.

You're probably interested in frog.TongueLength anyway, which is a value. Then you have a few more steps yet to do to get the average length.

See the hints I left for you in your previous question about this same task.