0

I'm trying to serialize an array of objects and a string. This is the serialization code:

FileStream s;
s = new FileStream(savefile.FileName, FileMode.Create, FileAccess.Write);
BinaryFormatter bf = new BinaryFormatter();
bf.Serialize(s, ch.chaps);
bf.Serialize(s, txtPassword.Text);
s.Close();

This is the deserialization code:

FileStream s;
s = new FileStream(openfile.FileName, FileMode.Open, FileAccess.Read);
BinaryFormatter bf = new BinaryFormatter();
string password = (string)bf.Deserialize(s);
ch.chaps = (Chapter[])bf.Deserialize(s);
s.Close();
int i;

if (password == txtPassword.Text)
{
    for (i = 0; i <= 1000; i++)
    {
        try
        {
            combChapSelect.Items.Add(ch.chaps[i].chapName);         
        }
        catch
        {
             i = 1000;
        }
    }
}

This is the code and visual studio says there are no errors but the openfiledialog doesn't close when I select a file and nothing happens. Have I done something wrong or is there another way to serialize different object types?

1
  • Is Chapter implementing ISerializable? You say you are trying to "serialize" but in your code you call "deserialize". So are you trying to serialize or deserialize? Commented Jan 9, 2014 at 23:55

1 Answer 1

1

You're doing this backwards. You have to deserialize in the same order that you serialize.

In your serialization, its chaps-then-password. In your deserialization, its password-then-chaps.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! That worked. I didn't know that the order mattered.

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.