0

Hey guys I am working on a program that will take a person's name and comment and then serialize it to a binary file. It must also be able to deserialize and load the data to the form. I need it to be able to go up and down the list of files(names and comments) by using buttons on the form. In my code I have it separated into a class that builds the object and the code for the form, and no i'm not allowed to separate it. (Just in case anyone thought that'd work)
the form code:

private void btnAdd_Click(object sender, EventArgs e)
{
    bool blnValid = true;
    if (string.IsNullOrWhiteSpace(txtName.Text))
    {
        MessageBox.Show("Please enter a valid name");
    }
    if (string.IsNullOrWhiteSpace(txtComment.Text))
    {
        MessageBox.Show("Please enter a valid comment");
        blnValid = false;
    }

    if (blnValid)
    {
        //create class instance and assign property values
        //set file name

        DateTime CurrentDate = DateTime.Now;
        string strFileName;

        strFileName = CurrentDate.ToString("dd-MM-yy-hh-mm-ss") + ".bin";

        // serialize object to binary file

        MessageBox.Show(strFileName);

        Enquiry newEnquiry = new Enquiry();
        newEnquiry.Name = txtName.Text;
        newEnquiry.Comment = txtComment.Text;
        newEnquiry.DOB = dteDOB.Value;
        newEnquiry.WriteToFile(strFileName, newEnquiry);
    }
}

private void btnLoad_Click(object sender, EventArgs e)
{

}

private void btnPrevious_Click(object sender, EventArgs e)
{

}

and the class code:

[Serializable]
class Enquiry
{
    //stores the various values into the enquiry class.
    public string Name { get; set; }

    public DateTime DOB { get; set; }

    public string Comment { get; set; }

    //creates the file, if there isn't one, writes the file, and
    //disables sharing while the program is active. It also formats
    //the file into binary 
    public void WriteToFile(string strFileName, Enquiry newEnquiry)
    {
        IFormatter formatter = new BinaryFormatter();
        Stream stream = new FileStream(strFileName, FileMode.Create, FileAccess.Write, FileShare.None);
        formatter.Serialize(stream, newEnquiry);
        stream.Close();

    }
    // this loads the files and translates them to plain text

    public void ReadFromFile(string strFileName, Enquiry newEnquiry)
    {
        Stream stream = File.OpenRead(strFileName);
        BinaryFormatter formatter = new BinaryFormatter();
        newEnquiry = (Enquiry)formatter.Deserialize(stream);
        stream.Close();     
    }
1
  • Your question is not clear! Commented Aug 20, 2015 at 5:19

1 Answer 1

1

I don't know what you are asking, but if you need methods to serialize and deserialize, use these:

 public static void BinarySerializeObject(string path, object obj)
    {
      using (StreamWriter streamWriter = new StreamWriter(path))
      {
        BinaryFormatter binaryFormatter = new BinaryFormatter();
        try
        {
          binaryFormatter.Serialize(streamWriter.BaseStream, obj);
        }
        catch (SerializationException ex)
        {
          throw new SerializationException(((object) ex).ToString() + "\n" + ex.Source);
        }
      }
    }

    public static object BinaryDeserializeObject(string path)
    {
      using (StreamReader streamReader = new StreamReader(path))
      {
        BinaryFormatter binaryFormatter = new BinaryFormatter();
        object obj;
        try
        {
          obj = binaryFormatter.Deserialize(streamReader.BaseStream);
        }
        catch (SerializationException ex)
        {
          throw new SerializationException(((object) ex).ToString() + "\n" + ex.Source);
        }
        return obj;
      }
    }
Sign up to request clarification or add additional context in comments.

3 Comments

Link to existing similar answer would be much better that random code that you think may relate to question.
@Alexei Levenkov What? I'm trying to help someone who does not know how to describe the issue that they are having, don't knock me for trying to be helpful. What is your response for?
OP already have very similar code in the post - so not really clear how your code is better... There is not much value in answering posts that are unclear or likely endup as duplicate... But it is your time and effort - spend it whatever way you find useful.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.