Domanda

Sto usando StreamWriter su C # WinForms

ho bisogno di informazioni scrivere al 'scrittore' come si può vedere.

Sto facendo qualcosa di sbagliato, come il campo 'scrittore'' ha gli errori di sintassi?

Sto ricevendo un messaggio che dice:

" 'scrittore' è un 'campo', ma è utilizzato come un 'tipo'"

tutte le idee per favore? il mio codice è inferiore a

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

    }
È stato utile?

Soluzione

Non è possibile avere dichiarazioni su un campo che non sono in un metodo (costruttore o altro).

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

Si dovrebbe anche fare attenzione a utilizzare il corretto alloggiamento -. writer.write non esiste, mentre writer.Write fa

Nel mio esempio, ho dichiarato writer come variabile locale del metodo MyMethod.

Leggi su C # campi qui .

Altri suggerimenti

Se si desidera che questo venga eseguito quando la classe viene "creato" utilizzare il costruttore:

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

Anche utilizzare la dichiarazione using di avere il flusso smaltito correttamente.

EDIT: a meno che non si dispone di Crave speciale per Stream, è possibile utilizzare il metodo statico WriteAllText della classe file:

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

In questo modo non c'è bisogno di preoccuparsi di chiusura / smaltimento e anche non c'è bisogno di chiamare il metodo ToString() di ciascuna classe sarà fatto automaticamente utilizzando il Concat.

Per prima, si ha il codice che non appartiene ad alcun metodo, come ha risposto Oded.

In secondo luogo, la vostra Write() è corretto ma write() (minuscolo prima lettera) non è.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top