0

In the following code, I am trying to override the calculate method, but I am getting the following error. What am I doing wrong?

A local or parameter named "calculate" cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter

using System;

namespace Assignment_7_George
{
    class RandomNumbers
    {
        static void Main(string[] args)
        {
            // initialize variables
            int sumofrandom = 0;
            double average = 0;
            double total = 0;

            // create random number object
            Random randomNumbers = new Random();

            //for loop that repeats twenty times to find a random number between 1 - 100
            for (int i = 0; i < 20; i++)
            {
                //find random number method
                int getRandom()
                {
                    int random = 1 + randomNumbers.Next(1,101);
                    sumofrandom = sumofrandom + random;
                    return sumofrandom;
                }
                getRandom();
            }

            // calculate average
            double calculate(double sumofrandom, ref double average, int count)
            {
                int x = 20;
                Convert.ToDouble(x);
                average = sumofrandom / x;
                Convert.ToInt32(average);
                return average;
            }

            // call method
            calculate(sumofrandom, ref average, 20);

            Console.WriteLine("The average of the 20 random numbers is " + Convert.ToInt32(average) + ".");
            Console.WriteLine(" ");

            //loop repeats 5 times
            for (int i = 0; i < 5; i++)
            {

                Console.Write("Enter a double value ");
                double input = Convert.ToDouble(Console.ReadLine());
       
                // cal method adds input to total
                double calculate(double input, ref double total)
                {
                    total = total + input;
                    return total;
                }
                // call method
                calculate(input, ref total);

                // prints results
                if (i > 3)
                {
                    Console.WriteLine("The total is " + calculate(input, ref total));
                    break;
                }
            }
        }
    }
}
D M
  • 5,769
  • 4
  • 12
  • 27
  • I think you may be struggling with some basics of C# and programming in general. First thing I'd do is move the calculate methods out of the main method. Make them static too since it holds no state. Then you can call RandomNumbers.calculate(..)). Lots of other problems. Count isn't used in the method. Convert.ToDouble isn't assigned anywhere. The math is off a bit. The average is always fixed to 20 numbers. No problem with jumping in at the deep end with programming - we all started somewhere but SO isn't the best for learning a subject - its is better helping out of specific holes. – MarkD Feb 02 '21 at 02:42
  • You use calculate as a function name twice, but at least at first sight, these two functions are not related. What happens is that the compiler thinks you try to use the same function with different parameters and you cannot do that with a local function. My first recommendation would be to rename both calculate functions to represent better what they do, e.g. the first one seems to calculate the average, so name it calculateAverage. The second might be calculateSum. Personally I would not use local functions in this way. It makes code less readable. – RudolfJan Feb 02 '21 at 12:54

1 Answers1

2

Method overloading does not work for local functions. The code is using local functions and they follow regular rules for defining variables in a scope - you can't redefine it if parent scope already contains something with the same name (see Variable scope confusion in C#). Note that only name of the local method is taken into account for that check unlike class level methods that use both name and parameters to find matching function ("method overloading").

You can either

  • name local functions differently. This is likely help best approach if exercise you are trying to is about local functions. "Calculate" is pretty meaningless name and likely cause confusion. Something like "ModifyTotal" would be reasonable name for second function.
  • in some cases you can use same variable and assign lambda to it. That would work if methods have the same signature. I'd not try to go that route in the case shown in the question as parameters are different and ref/out parameters are tricky to get right (Cannot use ref or out parameter in lambda expressions).
  • avoid local methods altogether to allow parameter overloading to work by moving methods out to class level. Note that you can't capture local variable in class-level methods - since methods in the question don't capture local variable moving them at class level will make main method shorter and easier to follow.
  • inline the local methods, especially if they are called once.
Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
  • Thank you for your explanation! I was creating local functions which can't be overloaded. I moved them out of main and made them static. That fixed my problem. I also noticed that my math was off a bit so I fixed that as well. Thanks for everyone that helped me figure this out. – Ibleedcoffee Feb 03 '21 at 03:10