我有一个“Debug”类,它只是将信息打印到控制台等。我希望能够从其余代码中调用其中的方法,但到目前为止它只能部分工作。
调用 dc.Print()
工作正常,但是当我调用 dc.Print(dc.GetEventsLogged())
时,我收到一条红线消息
“最佳重载方法匹配有一些无效参数” 以及参数 1:无法从“int”转换为“string”。
基本上:为什么我对 dc.Print 的论点是错误的?另外,对于“无法从 int 转换为 string?我试过 .ToString 但也没有用。
这是我的“Debug.cs”类:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Test
{
public class Debug
{
private int events_logged;
public Debug()
{
events_logged = 0;
}
public void Print(string Message)
{
Console.WriteLine("[" + DateTime.UtcNow + "] " + Message);
events_logged++;
}
public int GetEventsLogged()
{
return events_logged;
}
}
}
在我的“Program.cs”类中我有:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Test
{
class Program
{
static void Main(string[] args)
{
Debug dc = new Debug();
dc.Print("Test");
}
}
}
最佳答案
您看到错误的原因是 GetEventsLogged()
返回一个 int
而 Print()
期望您传入一个 字符串
。因此,您需要从 int
返回到 string
并且您在 ToString()
的正确轨道上。这将完成您想要实现的目标:
dc.Print(dc.GetEventsLogged().ToString());
关于C#初学者问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17721536/