ファイルに保存されている引用符のリストを作成しようとしています。引用符がコンソールに表示されたら、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)
{
}
}
}
}
これは2番目の質問の正しい場所です:http://codereview.stackexchange.com/ –
フィードバックの最初の部分:コードを書式設定します。フィードバックの第2の部分:自動的に実装されるプロパティについて学びます。フィードバックの第3の部分:これよりも短く、エラーについてはより正確な[mcve]を指定します。 –
おそらく関連している:http://stackoverflow.com/questions/18501120/cs1061-does-not-contain-a-definition-for – Codor