我在C# winForms上使用StreamWriter
正如你所看到的,我需要写信息给‘作者’。
我是不是做错了什么,因为'writer‘字段有语法错误?
我收到一条消息说:
"'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();
}发布于 2010-12-26 23:54:59
字段上的语句不能不在方法(构造函数或其他方法)中。
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#字段here的信息。
发布于 2010-12-27 00:00:07
如果您希望在“创建”类时运行该函数,请使用构造函数:
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语句来正确地处理流。
EDIT:除非你对Stream有特殊的渴望,否则你可以使用File类的静态WriteAllText方法:
public Booking()
{
File.WriteAllText(@"C:\BookingInfo.txt", string.Concat(dateTime, bookedShow, selectedShow, selectedSeat, finalPrice, custName, custAddress, custTelephone));
}通过这种方式,您不必担心关闭/处理,也不必调用每个类的ToString()方法,因为它将通过使用Concat自动完成。
发布于 2010-12-27 00:00:56
首先,你有不属于任何方法的代码,就像Oded回答的那样。
其次,您的Write()是正确的,但write() (小写的第一个字母)不正确。
https://stackoverflow.com/questions/4534418
复制相似问题