2016-07-11 4 views
1

ファイルに保存されている引用符のリストを作成しようとしています。引用符がコンソールに表示されたら、boolをtrueに変更します。このインデックスは、コンソールに表示する見積りを処理するために使用されます。最初に私はFile.WriteAllLinesを試しましたが、それは私のQuotesクラスでは動作しません。リストをバイナリに読み込む<T>

リストをファイルにシリアライズしようとしても問題はないようですが、ファイルからmyList2に読み込むコードでCS1061を取り除く方法がわかりません。

私は本当にこれに関するいくつかのフィードバックをしたいと思います。コードは自分の学習と娯楽のためのものです。

using System; 
using System.Collections.Generic; 
using System.IO; 
using System.Runtime.Serialization.Formatters.Binary; 

namespace Quotes 
{ 
    // A quote followed by a bool to show if it has been showed recently and an index to navigate the list. 
    [Serializable] 
    class Quotes 
    { 
     private string quote; 
     private bool shown; 
     private int index; 
     public Quotes(string _quote, bool _shown, int _index) 
     { 
      quote = _quote; 
      shown = _shown; 
      index = _index; 
     } 

     public string Quote 
     { 
      get 
      { 
       return quote; 
      } 
      set 
      { 
       quote = value; 
      } 
     } 

     public bool Shown 
     { 
      get 
      { 
       return shown; 
      } 
      set 
      { 
       shown = value; 
      } 
     } 

     public int Index 
     { 
      get 
      { 
       return index; 
      } 
      set 
      { 
       index = value; 
      } 
     } 

     public override string ToString() 
     { 
      return string.Format("{0} {1} {2}", quote, shown, index); 
     } 
    } 

    class Program 
    { 
     static void Main(string[] args) 
     { 
      // Set a variable to the My Documents path. 
      string mydocpath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments); 
      //List<Quotes> myList = new List<Quotes>(); 
      var myList = new List<Quotes>(); 
      myList.Add(new Quotes("One", false, 1)); 
      myList.Add(new Quotes("Two", false, 2)); 
      myList.Add(new Quotes("Three", false, 3)); 
      myList.Add(new Quotes("Four", false, 4)); 
      //Write the list to a file. Expand to accept user input and add at the end of the file. 
      try 
      { 
       using (Stream stream = File.Open(mydocpath + @"\WriteLines.txt", FileMode.Create)) 
       { 
        BinaryFormatter bin = new BinaryFormatter(); 
        bin.Serialize(stream, myList); 
       } 
      } 
      catch (IOException) 
      { 
      } 

      //Read from a file and write to the list.Put in a method when it works. 
      try 
      { 
       using (Stream stream = File.Open(mydocpath + @"\WriteLines.txt", FileMode.Open)) 
       { 
        BinaryFormatter bin = new BinaryFormatter(); 
        var myList2 = (List<Quotes>)bin.Deserialize(stream); 
        foreach (var quote in myList2) 
        { 
         //Why is this not working? Where should I define quote?? 
         Console.WriteLine("{0}, {1}, {2}", myList2.quote, myList2.shown, myList2.index); 
        } 
       } 
      } 
      catch (IOException) 
      { 
      } 
     } 
    } 
} 
+1

これは2番目の質問の正しい場所です:http://codereview.stackexchange.com/ –

+3

フィードバックの最初の部分:コードを書式設定します。フィードバックの第2の部分:自動的に実装されるプロパティについて学びます。フィードバックの第3の部分:これよりも短く、エラーについてはより正確な[mcve]を指定します。 –

+0

おそらく関連している:http://stackoverflow.com/questions/18501120/cs1061-does-not-contain-a-definition-for – Codor

答えて

4

現在、あなたのコードはmyList2.quoteにアクセスしようとしたが、それでもforeachブロック内で、myList2はまだリスト自体ではなく、「そのリスト内の現在の項目」です。

foreachは、リスト内の個々のQuoteオブジェクトをvar quote変数に割り当てます。あなたが使用している引用符のプロパティにアクセスすることができますforeachブロック内:

Console.WriteLine("{0}, {1}, {2}", quote.Quote, quote.Shown, quote.Index); 

quote.Quoteはあなたがアクセスできる公共の財産である一方で、quote.quoteがプライベートフィールドであることに注意してください)

2

をあなたのforeachループを作成しますそれぞれの引用符のインスタンスが実行されるたびに、引用符付き変数

foreach (var quote in myList2) 

したがって、ループ内のコードでその変数を参照する必要があります。

{ 
    Console.WriteLine("{0}, {1}, {2}", quote.Quote, quote.Shown, quote.Index); 
} 
0

ありがとうございます。私は自分自身を混乱させることができた。今私がどこで失敗したのかは明らかです。

私はこのサイトに投稿する場所と方法について作業する必要があることも明らかです。私のような奴隷に優しいことに感謝します。

関連する問題