【问题标题】:No overload for method c#方法c#没有重载
【发布时间】:2013-10-16 11:49:34
【问题描述】:

我已经在许多网站上搜索了解决方案,但我不能完全理解重载方法的概念,至少对于这个方法来说不是,因为我看不出我哪里出了问题。每当我尝试调用下面所述的方法时,我都会收到此错误 - “方法'arrayCalculator' 没有重载需要 0 个参数”。我希望你能帮助我解决这个问题。谢谢。

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[]
    }
}

【问题讨论】:

  • 显示调用代码。
  • 这段代码没有错。你怎么称呼它?这就是问题所在!
  • 您可能键入“arrayCalculator();”在调用时,但您还需要提供三个参数,如“arrayCalculator();”不向方法发送任何参数。

标签: c# arguments overloading


【解决方案1】:

您似乎试图在不带任何参数的情况下调用此函数。在你的情况下,你只使用 int 参数,所以你应该使用下面的函数。

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[]
    }
}

编辑:

您通过arrayCalculator(); 调用此函数,而是将您的参数传递给该函数,以便该函数知道在您的代码中使用什么来代替“m”。

例子:

假设计算类型为Calculations。那么你就有了

var mValue = 20;

var result = calculations.arrayCalculator(mValue);

【讨论】:

  • Topic starter 还需要给你上面提供的代码发送1个参数,插件怎么调用这个方法。
  • @MaxMommersteeg 已编辑。感谢您的建议。
【解决方案2】:

您可能正在调用您的方法如下:

arrayCalculator();

解决问题的两种方法。

  1. 将三个需要的参数发送到您要调用的方法。
  2. 修改您的arrayCalculator method

1

arrayCalculator(parameter1, parameter2, parameter3);

2

修改您的方法,以便调用它需要零个参数。

【讨论】:

    【解决方案3】:

    您在没有参数的情况下调用它。使用 3 个参数调用它。

    【讨论】:

      猜你喜欢
      • 2011-07-28
      • 1970-01-01
      • 1970-01-01
      • 2023-03-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-27
      相关资源
      最近更新 更多