C# 重载方法的0个参数
我试图从main调用我的方法之一,但收到一条错误消息: 方法“NextImageName”不重载0个参数 我不知道怎么解决这个问题 基本上,我调用了我的方法“BuildingBlock.NextImage();”,这就是我得到错误消息的地方C# 重载方法的0个参数,c#,overloading,C#,Overloading,我试图从main调用我的方法之一,但收到一条错误消息: 方法“NextImageName”不重载0个参数 我不知道怎么解决这个问题 基本上,我调用了我的方法“BuildingBlock.NextImage();”,这就是我得到错误消息的地方 class BuildingBlock { public static string ReplaceOnce(string word, string characters, int position) { word = wor
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;
}
类构建块
{
公共静态字符串ReplaceOnce(字符串字、字符串字符、int位置)
{
word=word.Remove(位置、字符、长度);
word=word.插入(位置、字符);
返回词;
}
公共静态字符串GetLastName(字符串名称)
{
字符串结果=”;
int posn=name.LastIndexOf(“”);
如果(posn>=0)结果=name.Substring(posn+1);
返回结果;
}
公共静态字符串NextImageName(字符串文件名,int newNumber)
{
如果(新编号>9)
{
返回ReplaceOnce(filename,newNumber.ToString(),(filename.Length-2));
}
如果(新编号<10)
{
返回ReplaceOnce(filename,newNumber.ToString(),(filename.Length-1));
}
如果(newNumber==0)
{
返回ReplaceOnce(filename,newNumber.ToString(),((filename.Length-2)+00));
}
返回文件名;
}
您正在调用该方法,但没有提供调用该方法所需的参数。以下是我的意思示例:
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;
}
}
请指出错误的原因是什么。错误信息是如何使你困惑的?你需要将参数传递给你的方法。这些方法都不需要0个参数,编译器是非常正确的,考虑将一些参数传递给你的方法。这些代码不会得到这个错误。你的问题在其他地方。调试你的代码,看看你在哪里得到。在发布到时,您应该包括编译器抱怨的实际行(提示:双击错误消息,它将带您到那里)。此外,您还应该包括准确的错误消息。您键入的消息没有包括所有详细信息。num*0将始终为0。:)我在那里。:)没有发现您是故意这样做的。很好。