【问题标题】:Building a custom class in c#在 C# 中构建自定义类
【发布时间】:2016-04-05 08:21:28
【问题描述】:

我之前没有创建过自定义类,所以这可能是不可能的,我想读取一些文本文件并存储有关它们的某些信息以在我的程序中使用。

class text
    {
        public int IDnum { get; set; }
        public string file { get; set; }
        public int lineNum { get; set; }
        public string FileText { get; set; }
        public string lineType { get; set; }
    }

    List<text> listOne = new List<text>();
    internal void ReadFile()
    {
        try
        {
            int IDtype = 0;
            foreach (string x in resultFiles)
            {
                using (StreamReader sr = new StreamReader(x))
                {
                    string s;//text line

                    int LINECOUNT = 0;
                    string type = "not defined";
                    while ((s = sr.ReadLine()) != null)// this reads the line
                    {
                        if(s.Contains("COMPUTERNAME="))
                        {
                            type = "PC Name";
                        }

                        if (s.Contains("Original Install Date:     "))
                        {
                            type = "Original Install Date";
                        }
                        if (LINECOUNT==2)
                        {
                            type = "other Date";
                        }
                        if (s.Contains("DisplayName\"="))
                        {
                                type = "Add/Remove Programs";
                        }

                        text text1 = new text { IDnum = IDtype,  lineNum=LINECOUNT, file=x, FileText=s, lineType=type}; 
                        LINECOUNT++;
                        IDtype++;
                        listOne.Add(text1);
                    }

                    sr.Close();
                }
            }
            foreach(var x in listOne)
            {
                MessageBox.Show(x.ToString());
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.ToString());
        }
    }

但是,当我尝试读取列表时,它只返回相同的值“程序名称。类名称。文本”

我之前从未构建过自定义类,谁能指出我可以学习更多示例的网站?

提前感谢您的任何建议:)

【问题讨论】:

  • 在调用x.ToString() 时,您需要覆盖文本类中的.ToString()-Method 以获得正确的结果。调用 ToString() 时你期望得到什么结果?

标签: c# list class


【解决方案1】:

x.ToString() 不起作用,因为它是您的类的类型而不是字符串。

您可以访问项目的属性

foreach (var x in listOne)
{
    MessageBox.Show(x.file + " " + x.FileText);
}

在您的类中覆盖 ToString() 方法 - 然后您可以使用 x.ToString()

class text
{
    public int IDnum { get; set; }
    public string file { get; set; }
    public int lineNum { get; set; }
    public string FileText { get; set; }
    public string lineType { get; set; }

    public override string ToString()
    {
        return string.Format("{0}, {1}", this.file, this.FileText);
    }
}

【讨论】:

  • ... 或覆盖 text 中的 ToString(OP 应该选择另一个符合 .NET 大写约定的名称,例如 Text)。
  • @TimSchmelter 谢谢,昨天我有一个关于覆盖 ToString 的争论,所以我不想先提及它stackoverflow.com/questions/36398000/…
  • 但是,我更喜欢 this 方法来完成该任务,而不是 ToString 和反射。
【解决方案2】:

要使用ToString() 方法,您必须覆盖它,例如通过以下方式:

class text
{
    public int IDnum { get; set; }
    public string file { get; set; }
    public int lineNum { get; set; }
    public string FileText { get; set; }
    public string lineType { get; set; }

    public override ToString()
    {
        return fileText; // return here whatever you want to use
    }
}

您使用ToString() 获取有关您的测试类实例的信息。如果你像上面那样实现 ToString,或者使用类的属性,你会得到更好的结果。

【讨论】:

    【解决方案3】:

    listOne 是类text 的列表,因此在循环中您实际上打印的是类名而不是内容。您可以通过调用您定义的成员来打印内容

    foreach(var x in listOne)
    {
         MessageBox.Show(x.IDnum + " " + x.file + ...);
    }
    

    附带说明,C# 中的类名应以大写字母开头。

    【讨论】:

      猜你喜欢
      • 2011-05-04
      • 2015-07-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-22
      相关资源
      最近更新 更多