首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >StreamWriter语法

StreamWriter语法
EN

Stack Overflow用户
提问于 2010-12-26 23:52:25
回答 3查看 1.6K关注 0票数 0

我在C# winForms上使用StreamWriter

正如你所看到的,我需要写信息给‘作者’。

我是不是做错了什么,因为'writer‘字段有语法错误?

我收到一条消息说:

"'writer‘是一个'field’,但它的用法与‘type’类似“

有什么想法吗?我的代码如下

代码语言:javascript
复制
 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();

    }
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2010-12-26 23:54:59

字段上的语句不能不在方法(构造函数或其他方法)中。

代码语言:javascript
复制
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的信息。

票数 2
EN

Stack Overflow用户

发布于 2010-12-27 00:00:07

如果您希望在“创建”类时运行该函数,请使用构造函数:

代码语言:javascript
复制
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方法:

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

通过这种方式,您不必担心关闭/处理,也不必调用每个类的ToString()方法,因为它将通过使用Concat自动完成。

票数 1
EN

Stack Overflow用户

发布于 2010-12-27 00:00:56

首先,你有不属于任何方法的代码,就像Oded回答的那样。

其次,您的Write()是正确的,但write() (小写的第一个字母)不正确。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4534418

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档