質問

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

ご覧のとおり、「作家」に情報を書く必要があります。

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

私は次のように言ってメッセージを受け取っています:

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

何かアイデアをお願いしますか?私のコードは以下にあります

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

    }
役に立ちましたか?

解決

メソッド(コンストラクターなど)にないフィールドにステートメントを持つことはできません。

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#フィールドについて読んでください ここ.

他のヒント

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

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の特別なCraveがない限り、ファイルクラスの静的writealltextメソッドを使用できます。

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

このようにして、閉鎖/処分を心配する必要はありませんし、電話する必要もありません ToString() 各クラスの方法は、 Concat.

最初に、ODEDが返信したように、どんな方法にも属さないコードがあります。

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

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top