0

C#winFormsでStreamWriterを使用しています

ご覧のとおり、「ライター」に情報を書き込む必要があります。

'writer'フィールド'に構文エラーがあるので、私は何か間違ったことをしていますか?

次のようなメッセージが表示されます。

「「ライター」は「フィールド」ですが、「タイプ」のように使用されます」

何かアイデアはありますか?私のコードは以下です

 class Booking
    {   
        //what other details do you need to save at the end?...
        public Show bookedShow { get; private set; }
        public Seat selectedSeat { get; private set; }
        public Show selectedShow { get; private set; }
        public Seat finalPrice { get; private set; } //hasnt been defined yet, but this would be the amount of seats selected * the Price

        //i will also need customer details which are:
        public dateAndTime dateTime { get; private set; }
        public Customer custName { get; private set; }
        public Customer custAddress { get; private set; }
        public Customer custTelephone { get; private set; }

        System.IO.StreamWriter writer = new System.IO.StreamWriter(@"C:\BookingInfo.txt"); //open the file for writing.               
        writer.Write(dateTime.ToString()); //write the current date to the file. change this with your date or something.
        writer.write(bookedShow.ToString());
        writer.write(selectedShow.ToString());
        writer.write(selectedSeat.ToString());
        writer.write(finalPrice.ToString());
        writer.write(custName.ToString());
        writer.write(custAddress.ToString());
        writer.write(custTelephone.ToString());
        writer.Close();

    }
4

3 に答える 3

2

メソッド(コンストラクターまたはその他)にないフィールドにステートメントを含めることはできません。

class Booking
{   
    //what other details do you need to save at the end?...
    public Show bookedShow { get; private set; }
    public Seat selectedSeat { get; private set; }
    public Show selectedShow { get; private set; }
    public Seat finalPrice { get; private set; } //hasnt been defined yet, but this would be the amount of seats selected * the Price

    //i will also need customer details which are:
    public dateAndTime dateTime { get; private set; }
    public Customer custName { get; private set; }
    public Customer custAddress { get; private set; }
    public Customer custTelephone { get; private set; }

    public void MyMethod()
    {
      System.IO.StreamWriter writer = new System.IO.StreamWriter(@"C:\BookingInfo.txt"); //open the file for writing.               
      writer.Write(dateTime.ToString()); //write the current date to the file. change this with your date or something.
      writer.Write(bookedShow.ToString());
      writer.Write(selectedShow.ToString());
      writer.Write(selectedSeat.ToString());
      writer.Write(finalPrice.ToString());
      writer.Write(custName.ToString());
      writer.Write(custAddress.ToString());
      writer.Write(custTelephone.ToString());
      writer.Close();
    }
 }

また、正しいケーシングを使用するように注意する必要があります-writer.write存在しませんが、存在writer.Writeします。

私の例では、メソッドwriterのローカル変数として宣言しました。MyMethod

ここでC#フィールドについて読んでください。

于 2010-12-26T15:54:58.627 に答える
1

クラスが「作成」されたときにこれを実行する場合は、コンストラクターを使用します。

public Booking()
{
        using (System.IO.StreamWriter writer = new System.IO.StreamWriter(@"C:\BookingInfo.txt")) //open the file for writing.             
        { 
                writer.Write(dateTime.ToString()); //write the current date to the file. change this with your date or something.
                writer.Write(bookedShow.ToString());
                writer.Write(selectedShow.ToString());
                writer.Write(selectedSeat.ToString());
                writer.Write(finalPrice.ToString());
                writer.Write(custName.ToString());
                writer.Write(custAddress.ToString());
                writer.Write(custTelephone.ToString());
        }
}

また、usingステートメントを使用して、ストリームを適切に破棄します。

編集: Stream に特別な欲求がない限り、File クラスの静的な WriteAllText メソッドを使用できます。

public Booking()
{
    File.WriteAllText(@"C:\BookingInfo.txt", string.Concat(dateTime, bookedShow, selectedShow, selectedSeat, finalPrice, custName, custAddress, custTelephone));
}

この方法では、クローズ/破棄について心配する必要はなくToString()Concat.

于 2010-12-26T16:00:06.717 に答える
0

まず、Oded が答えたように、どのメソッドにも属さないコードがあります。

第二に、あなたWrite()は正しいですが、write()(小文字の最初の文字) は違います。

于 2010-12-26T16:00:56.290 に答える