2017-06-05 46 views
0

我有一個類菜是這樣的:列表寫文本文件

public class Dish 
{ 
    public Dish(int cost, string name) 
    { 
     Cost = cost; 
     Name = name; 
    } 

    public int Cost { get; set; } 
    public string Name { get; set; } 
} 

在主窗體中,用戶可以輸入以前的數據。 然後,我創建「菜」的列表:

public static List<Piatto> List = new List<Piatto>(); 

我創造了它在一個靜態類,所以我可以從任何地方訪問它。 一旦項目被添加到列表中,我想將它保存在一個文本文件(.txt),所以我試圖用這樣的:

public static void SaveToTxt() 
    { 
     using (TextWriter tw = new StreamWriter(Path)) 
     { 
      foreach (var item in Data.List) 
      { 
       tw.WriteLine(item.ToString()); 
      } 
     } 
    } 

的問題是,當我打開文本文件,其中我保存了我的列表,我得到了「WindowsFormsApplication1.Dish」。

如何保存到顯示費用和名稱的文本文件?

P.s.我想將列表保存在一個文本文件中,因爲我更容易刪除一行,這是我不知道如何在二進制文件中執行的操作。

在此先感謝。

編輯:

重寫ToString()方法工作得很好。謝謝大家的答案!

+0

你可以序列化。檢查這兩個鏈接: https://stackoverflow.com/questions/16352879/write-list-of-objects-to-a-file https://stackoverflow.com/questions/6115721/how-to-save-恢復序列化對象到文件 – Farshad

+0

_「覆蓋ToString()方法工作正常。謝謝大家的答案!」_ - 你應該接受答案然後 – FakeCaleb

+0

是的,對不起!我正在嘗試人們給我的每一段代碼。 –

回答

1

item是一個包含多個屬性的對象,你要訪問它們:

public static void SaveToTxt() 
{ 
    using (TextWriter tw = new StreamWriter(Path)) 
    { 
     foreach (var item in Data.List) 
     { 
      tw.WriteLine(string.Format("Item: {0} - Cost: {1}", item.Name, item.Cost.ToString())); 
     } 
    } 
} 
1

覆蓋Dish中的ToString()方法。例如:

public class Dish 
{ 
    // rest of code.. 


    public override string ToString() 
    { 
     return Cost.ToString() + " " + Name; 
    } 
} 
+0

忘了注意,無論如何快樂編碼..! –

0

您需要使用的屬性,所以是這樣的:

foreach (var item in Data.List) 
{ 
    tw.WriteLine(item.Cost.ToString() + " " + item.Name); 
} 
0

更改您的代碼是這樣的

public static void SaveToTxt() 
{ 
    using (TextWriter tw = new StreamWriter(Path)) 
    { 
     foreach (var item in Data.List) 
     { 
      tw.WriteLine(string.Format("{0} {1}", item.Name, item.Cost)); 
     } 
    } 
} 
+1

爲什麼downvote? – Nino

+0

我很好奇這也... – pitersmx

+0

@pitersmx看起來像某人downvoted所有的答案。它們並不是一些夢幻般的代碼,但它們可以解決OP的問題。 – Nino

0

你需要重寫的ToString方法你類似如下:

public override string ToString() 
{ 
    return string.Format("Name: {0}, Cost: {1}",Name,Cost); 
} 
0

我認爲你必須重寫ToString方法在你的菜類:

public class Dish 
{ 
    public Dish(int cost, string name) 
    { 
     Cost = cost; 
     Name = name; 
    } 

    public int Cost { get; set; } 
    public string Name { get; set; } 

    public override string ToString() 
    { 
     return $"{Name}: {Cost.ToString("c")}"; 
    } 
} 
0

它的簡單,你是幾乎在那裏,我想你忘了在班上覆蓋.ToString()方法。通過覆蓋.ToString()方法,您可以返回其對象的字符串表示形式。你也可以格式化它們。所以你必須添加這個overrided .ToString()方法,使其工作。考慮以下代碼

public class Dish 
{ 
    public Dish(int cost, string name) 
    { 
     Cost = cost; 
     Name = name; 
    } 

    public int Cost { get; set; } 
    public string Name { get; set; } 
    public override string ToString() 
    { 
     return String.Format("Item Name :{0} \n Item Cost : {1}", this.Name,this.Cost); 
    } 
} 
+0

@向下投票人:請說明原因以便我改進帖子。如果它是個人的,那麼不需要指定原因,接受它作爲挑戰。 –

0

如果你只想寫,沒有太多的代碼更改的最快方法是重寫的ToString方法。

public class Dish 
{ 
    public Dish(int cost, string name) 
    { 
     Cost = cost; 
     Name = name; 
    } 

    public int Cost { get; set; } 
    public string Name { get; set; } 

    public override string ToString(){ 
     return "Name: "+Name+" cost: "+cost; 
    } 
} 
0

當convering Dish實例爲String,.NET使用ToString()方法,其默認值(即Object.ToString())實現返回項目的類型名稱:

WindowsFormsApplication1.Dish 

您可以提供您的ToString版本:

public class Dish 
    { 
    ... 

     public override string ToString() 
     { 
      return $"{Cost} {Name}";   
     } 
    } 

或者把正確的格式放在你寫入t的地方他文件(用的LINQ有點幫助 - Select):

public static void SaveToTxt() { 
    // File class allows to get rid of pesky readers and writers 
    File.WriteAllLines(Path, Data.List.Select(dish => $"{dish.Cost} {dish.Name}")); 
    }