2016-04-05 14 views
1

我之前沒有創建自定義類,所以這可能是不可能的,我想讀取一些文本文件並存儲有關它們的特定信息,以便在我的程序中使用它們。在c中構建一個自定義類#

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()); 
     } 
    } 

然而,當我嘗試閱讀列表,它只是返回相同的值「class.text的program.name的名字」

我以前從來沒有內置自定義類任何人都可以點我到一個網站我可以在哪裏學到更多的例子?

預先感謝任何建議:)

+2

您需要重寫文本類中的'.ToString()'-Method以在調用'x.ToString()'時獲得正確的結果。調用ToString()時你期望得到什麼結果? – Camo

回答

1

listOne是類text的名單,所以在循環中,您實際打印類名,而不是內容。您可以通過調用您定義的成員來打印內容

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

作爲一個附註,C#中的類名應該以大寫字母開頭。

3

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); 
    } 
} 
+2

...或者在'text'中重寫'ToString'(OP應該選擇符合.NET大寫約定的另一個名稱,比如'Text')。 –

+0

@TimSchmelter謝謝,我有一個關於重寫ToString昨天的爭論,所以我不想提及它第一http://stackoverflow.com/questions/36398000/create-comma-separated-list-from-list-of-objects/36398086 #36398086 – fubo

+0

但是,而不是'ToString'和反射我更喜歡[this](http://stackoverflow.com/a/36421337/284240)方法爲該任務。 –

1

要使用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,或者使用類的屬性,你會得到更好的結果。