C#初学者问题

标签 c# .net class

我有一个“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() 返回一个 intPrint() 期望您传入一个 字符串。因此,您需要从 int 返回到 string 并且您在 ToString() 的正确轨道上。这将完成您想要实现的目标:

dc.Print(dc.GetEventsLogged().ToString());

关于C#初学者问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17721536/

相关文章:

c# - 如何在 StepArgumentTransformation 中使用 REGEX 查找带前缀的字符串

c# - 将枚举绑定(bind)到 WinForms 组合框,然后设置它

python - 为什么在类里面使用 bottle 时 self 对于方法来说是多余的?

C# 路径操作

c# - 如何在构建/部署时忽略 servicefabric list 文件中的服务类型

c# - 从 Helix Toolkit 保存图像

c# - AppDomain.CurrentDomain.Evidence 抛出 SerializationException

.net - CS1607 : The version specified for the 'file version' is not in the normal 'major.minor.build.revision' format in . 网络

Python 极其动态的类属性

c++ - 我可以避免 Matrix 类的迭代器中的循环依赖吗?