0

I have created a class that requests user input (7 textboxes of individual data)

I need to save that data for later use in a flat file using the StreamWriter Class.

Do I have to create individual instances for each field?

private void btnSaveFile_Click(object sender, EventArgs e) { StreamWriter studentInfo = new StreamWriter(txtFirstName.Text); }

or is there a way I can ask StreamWriter to print all the fields in that class? I tried overloading the method, however apparently that can't be done.

Victor
  • 9
  • 8
  • 1
    You're writing textbox values, so you either need to explicitly specify them all or loop through controls of type `TextBox` on the form and write them to the file. Another approach would be to bind these values to an object, say `UserInformation`, and then serialize the object to xml or json. That way it can be easily deserialized back to an object when you need to load the information; and the serializer will do all the heavy lifting for you. – Arian Motamedi Nov 16 '16 at 17:54

2 Answers2

1

Yes, if you serialize it as JSON or XML first, get it as string... or the simplest way... add a ToString() Method to your class:

    class Person
    {
            public string Name { get; set; }
            public int Age { get; set; }
            public Person (string name, int age) {
              this.Name = name;
              this.Age = age;
            }
            public override string ToString()
            {
                return "Person: " + Name + " " + Age;

            }

     }

And then write your Class instance to write it to a file:

private void btnSaveFile_Click(object sender, EventArgs e)
{
   string mydocpath = ""; // add your path here
   Person newPerson =  new Person ("David", 32);
   using (StreamWriter studentInfo = new StreamWriter(mydocpath + @"\Student.txt")) {
      studentInfo.WriteLine(newPerson.ToString());
   }
}

Hope this helps

David Espino
  • 2,177
  • 14
  • 21
  • As an extension of this, you will then need to write something which can read this input in, and turn it back into the form you started with. I'd actually recommend serialising to JSON or XML, over using `ToString()`; it will be much easier, and the conversion both ways is a well-solved problem – askrich Nov 16 '16 at 18:02
  • Thank you so much! I will give that a whirl! – Victor Nov 16 '16 at 18:24
  • Thank you for your help. I am getting a System.ArgumentNullException {"Path cannot be null.\r\nParameter name: path"} – Victor Nov 16 '16 at 18:59
  • 'private void btnSaveFile_Click(object sender, EventArgs e) { String[] studentInfo = {"First Name: "+ txtFirstName.Text, "Last Name: " + txtLastName.Text, "Student Id: "+txtStudentId.Text, "Address: "+txtAddress.Text, "Cell Phone: "+ txtCellPhone.Text, "Email: " + txtEmail.Text, "Degree Program: " + txtDegreeProgram.Text}; StreamWriter sw = new StreamWriter(File.OpenWrite(path)); sw.WriteLine(studentInfo.ToString()); sw.Close();' – Victor Nov 16 '16 at 18:59
  • Sorry, I just created the toString method and copied the rest of your code... updated with the StreamWritter definition. – David Espino Nov 16 '16 at 19:05
  • If you need human readable format then use XmlSerializer to serialize you instance int stream. Then you can write this stream with any available implementations of StreamWriter. Or you can use BinaryFormatter to serialize your instance into binary form. In both cases you can easily save and restore your data. –  Nov 16 '16 at 19:26
  • No worries,I was able to correct that exception. For some reason, it is working but not working 100% . When I say it is working, I can now open a dialog box and select a .txt file where to save the information. However, it doesn't write the information in the string array. Instead it writes this : System.String[] ion form test not sure why that is happening. I am trying to fix – Victor Nov 16 '16 at 19:27
  • I don't think I can use the xmlSerializer option. We have not covered that in class. Even if I knew what that was, I don't think I could pull that off with the time I have available. – Victor Nov 16 '16 at 19:29
0

You can mark your class with the [Serializable] attribute. You can then use a BinaryFormatter to write the class to the stream. That is if it doesn't matter to you if the data is human readable.

Sefe
  • 13,731
  • 5
  • 42
  • 55