简体   繁体   中英

No overload for method c#

I have searched for a solution on numerious websites but I can't quite grasp the concept of overloading methods, at least not for this one as I can't see where I am going wrong with it. Whenever I try to call the method stated below I get this error - "No overload for method 'arrayCalculator' takes 0 arguments". I hope you will be able to help me with this. Thanks.

public class Calculations
{
    public static int[] arrayCalculator(object sender, EventArgs e, int m)
    {
        int i; 
        int[] result = new int[9];   
        int[] timesTable = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
        for (i = 0; i <= 9; i++)
        {                
            result[i] = m * timesTable[i];
            System.Diagnostics.Debug.WriteLine("Calculation successful: " + m + " * " +  timesTable[i] + " = " + result[i] + "."); 
       }
       return result; // returns int result[]
    }
}

It appears you are trying to call this function without any parameters. In your case, you only use the int parameter, so you should use the function below instead.

public class Calculations
{
    public static int[] arrayCalculator(int m)
    {
        int i; 
        int[] result = new int[9];   
        int[] timesTable = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
        for (i = 0; i <= 9; i++)
        {                result[i] = m * timesTable[i];
            System.Diagnostics.Debug.WriteLine("Calculation successful: " + m + " * " +  timesTable[i] + " = " + result[i] + "."); 
       }
        return result; // returns int result[]
    }
}

Edit:

You are calling this function by arrayCalculator(); Instead, pass in your parameter to the function so the function knows what do use in place of 'm' in your code.

Example:

Assume calculations is of type Calculations . Then you'd have

var mValue = 20;

var result = calculations.arrayCalculator(mValue);

You probably are calling your method as follow:

arrayCalculator();

Two ways to fix your problem.

  1. Send the three needed parameters, to method that you want to call.
  2. Modify your arrayCalculator method .

1

arrayCalculator(parameter1, parameter2, parameter3);

2

Modify your method so that zero parameters are required for calling it.

You are calling it with no parameters. Call it with 3 parameters.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM