【问题标题】:0 arguments for overload method重载方法的 0 个参数
【发布时间】:2014-01-04 22:05:03
【问题描述】:

我正在尝试从 main 调用我的一个方法,但我收到一条错误消息:

方法“NextImageName”没有重载需要 0 个参数

不知道如何解决这个问题 在我的主程序上,我调用了我的方法“BuildingBlock.NextImage();”这是我收到错误消息的地方。

class BuildingBlock
{
    public static string ReplaceOnce(string word, string characters, int position)
    {
        word = word.Remove(position, characters.Length);
        word = word.Insert(position, characters);
        return word;
    }

    public static string GetLastName(string name)
    {
        string result = "";
        int posn = name.LastIndexOf(' ');
        if (posn >= 0) result = name.Substring(posn + 1);
        return result;
    }

    public static string NextImageName(string filename, int newNumber)
    {

        if (newNumber > 9)
        {
            return ReplaceOnce(filename, newNumber.ToString(), (filename.Length - 2));
        }
        if (newNumber < 10)
        {
            return ReplaceOnce(filename, newNumber.ToString(), (filename.Length - 1));
        }
        if (newNumber == 0)
        {
            return ReplaceOnce(filename, newNumber.ToString(), ((filename.Length - 2) + 00));
        }
        return filename;
    }

【问题讨论】:

  • 请指出是哪一行导致了这个错误。错误信息如何让您感到困惑?
  • 您需要将参数传递给您的方法。
  • 这些方法都没有带0参数,编译器很对,考虑给你的方法传递一些参数。
  • 这些代码不会出现此错误。你的问题在别的地方。调试你的代码,看看你在哪里。
  • 发布到 SO 时,您应该包括编译器抱怨的实际行(提示:双击错误消息,它会带您到那里)。此外,您应该包括 EXACT 错误消息。您输入的内容未包含所有详细信息。

标签: c# overloading


【解决方案1】:

您在调用该方法时没有提供调用它所需的参数。这是我的意思的一个例子:

public class Program
{
    public void Main()
    {
        int answer = GetAnswer(4); //4 is the argument
        //don't do `GetAnswer()`;
        Console.WriteLine(answer);
    }

    public static int GetAnswer(int num)
    {
        return (num*0) + 42;
    }
}

【讨论】:

  • num*0 将始终为 0。:)
  • 让我到了那里。 :) 没想到你是故意的。不错。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-05-26
  • 1970-01-01
相关资源
最近更新 更多