Pregunta

Estoy utilizando StreamWriter en C # WinForms

i necesidad de información de escritura en el 'escritor' como se puede ver.

estoy haciendo algo mal, ya que el campo 'escritor'' tiene errores de sintaxis?

Estoy recibiendo un mensaje que dice:

" 'escritor' es un 'campo', pero se utiliza como un 'tipo'"

alguna idea por favor? mi código está por debajo

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

    }
¿Fue útil?

Solución

No se puede tener declaraciones en un campo que no están en un método (el constructor o el otro).

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

También debe tener cuidado de usar la caja correcta -. writer.write no existe, mientras que writer.Write hace

En mi ejemplo, he declarado writer como una variable local del método MyMethod.

Lea sobre C # campos aquí .

Otros consejos

Si desea que esta se ejecute cuando la clase se "crea" utilizar el 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());
        }
}

También utilizar la instrucción using tener la corriente dispuesta correctamente.

EDIT: a menos que tenga Crave especial para la corriente, se puede utilizar el método WriteAllText estático de la clase del archivo:

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

De esta manera usted no tiene que preocuparse por el cierre / eliminación y también no tiene que llamar al método ToString() de cada clase, ya que se realiza de forma automática mediante el uso de la Concat.

En primer lugar, usted tiene el código que no pertenece a ningún método, como respondió Oded.

En segundo lugar, su Write() es correcta, pero write() (primera letra en minúscula) no lo es.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top