Ibleedcoffee
Ibleedcoffee

Reputation: 13

How do I override a method in C#?

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;
                }
            }
        }
    }
}

Upvotes: 0

Views: 186

Answers (1)

Alexei Levenkov
Alexei Levenkov

Reputation: 100620

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.

Upvotes: 2

Related Questions