簡體   English   中英

方法C#沒有重載

[英]No overload for method c#

我已經在眾多網站上搜索了一種解決方案,但是我不太了解重載方法的概念,至少對於這種方法不是這樣,因為我看不到我在哪里出錯了。 每當我嘗試調用下面所述的方法時,都會出現此錯誤-“方法'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[]
    }
}

看來您正在嘗試不帶任何參數的情況下調用此函數。 就您而言,您僅使用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();調用此函數arrayCalculator(); 而是將參數傳遞給函數,以便函數知道在代碼中用什么代替“ m”。

例:

假設計算類型為Calculations 那你有

var mValue = 20;

var result = calculations.arrayCalculator(mValue);

您可能正在按以下方式調用方法:

arrayCalculator();

解決問題的兩種方法。

  1. 將三個必需的參數發送到要調用的方法。
  2. 修改您的arrayCalculator method

1個

arrayCalculator(parameter1, parameter2, parameter3);

2

修改您的方法,以便調用它需要零個參數。

您正在調用沒有參數。 用3個參數調用它。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM