Question

J'utilise StreamWriter sur C # WinForms

i besoin d'informations d'écriture sur le « écrivain » comme vous pouvez voir.

ce que je fais quelque chose de mal, comme le champ « écrivain » » a des erreurs de syntaxe?

Je reçois un message disant:

« écrivain» est un « champ », mais est utilisé comme un «type »

Toutes les idées satisfont? mon code est ci-dessous

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

    }
Était-ce utile?

La solution

Vous ne pouvez pas avoir des déclarations sur un champ qui ne sont pas dans une méthode (constructeur ou autre).

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

Vous devez également prendre soin d'utiliser le boîtier correct -. writer.write n'existe pas, alors que writer.Write ne

Dans mon exemple, je l'ai déclaré writer comme une variable locale de la méthode MyMethod.

Lisez sur C # champs .

Autres conseils

Si vous voulez que cela fonctionne lorsque la classe est « créée » utiliser le constructeur:

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

Utilisez également la déclaration de using avoir le flux correctement placé.

EDIT: à moins que vous avez Crave spécial pour Stream, vous pouvez utiliser la méthode WriteAllText de statique de la classe du fichier:

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

De cette façon, vous n'avez pas à vous soucier de la fermeture / élimination et aussi ne pas avoir à appeler la méthode ToString() de chaque classe comme il sera fait automatiquement en utilisant le Concat.

Pour la première, vous avez le code qui ne appartiennent à toute méthode, comme Oded a répondu.

Deuxièmement, votre Write() est correct, mais write() (première lettre minuscule) n'est pas.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top