Simagen
Simagen

Reputation: 419

StreamWriter syntax

I am using StreamWriter on C# winForms

i need to write information to the 'writer' as you can see.

am i doing something wrong, as the 'writer' field' has syntax errors?

i am receiving a message saying:

"'writer' is a 'field' but is used like a 'type'"

any ideas please? my code is below

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

    }

Upvotes: 0

Views: 1637

Answers (3)

Shadow Wizard
Shadow Wizard

Reputation: 66388

If you want this to run when the class is "created" use the 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());
        }
}

Also use the using statement to have the stream disposed properly.

EDIT: unless you have special crave for Stream, you can use the static WriteAllText method of the File class:

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

This way you don't have to worry about closing/disposing and also don't have to call the ToString() method of each class as it will be done automatically by using the Concat.

Upvotes: 1

Daniel Mošmondor
Daniel Mošmondor

Reputation: 19956

For first, you have the code that doesn't belong to any method, as Oded replied.

Second, your Write() is correct but write() (lowercase first letter) isn't.

Upvotes: 0

Oded
Oded

Reputation: 499002

You can't have statements on a field that are not in a method (constructor or other).

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

You should also take care to use the correct casing - writer.write doesn't exist, whereas writer.Write does.

In my example, I have declared writer as a local variable of the MyMethod method.

Read about C# fields here.

Upvotes: 2

Related Questions