题
我在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();
}
解决方案
您不能在未使用方法(构造函数或其他)的字段上具有语句。
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#字段的信息 这里.
其他提示
如果您希望在“创建”类时运行该运行,请使用constructor:
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
声明使流正确处置。
编辑:除非您有特殊渴望的流,否则可以使用文件类的静态writeallText方法:
public Booking()
{
File.WriteAllText(@"C:\BookingInfo.txt", string.Concat(dateTime, bookedShow, selectedShow, selectedSeat, finalPrice, custName, custAddress, custTelephone));
}
这样,您不必担心关闭/处置,也不必致电 ToString()
每个类的方法将通过使用 Concat
.
首先,您的代码不属于任何方法,如ODED答复。
第二,你 Write()
是正确的,但是 write()
(小写的第一个字母)不是。
不隶属于 StackOverflow