我有一个"Debug"类,它只是将信息打印到控制台等.从其他代码我希望能够调用其中的方法,但到目前为止它只是部分工作.
通话dc.Print()工作正常,但一旦我打电话,dc.Print(dc.GetEventsLogged())我就会收到一条红线
"最好的重载方法匹配有一些无效的参数"以及参数1:无法从'int'转换为'string'.
基本上:为什么我对dc.Print的争论是错误的?另外,我能做些什么"无法从int转换为字符串?我尝试过.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;
}
}
}
Run Code Online (Sandbox Code Playgroud)
在我的"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");
}
}
}
Run Code Online (Sandbox Code Playgroud)
你看到错误的原因是因为GetEventsLogged()返回an int而Print()你希望你传入一个string.因此,您需要从返回到int,string并且您在正确的轨道上ToString().这将做你想要实现的目标:
dc.Print(dc.GetEventsLogged().ToString());
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
241 次 |
| 最近记录: |