0

我在 C# winForms 上使用 StreamWriter

如您所见,我需要将信息写给“作家”。

我做错了什么,因为“作家”字段有语法错误吗?

我收到一条消息说:

“‘writer’是一个‘field’,但它被用作‘type’”

请问有什么想法吗?我的代码在下面

 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存在。

在我的示例中,我已声明writerMyMethod方法的局部变量。

在此处阅读有关 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 回答