Reputation: 101
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?
Upvotes: 0
Views: 260
Reputation: 4624
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.
Upvotes: 1