I've been following a guide on Binary Serialization (this one here: http://www.codeproject.com/Articles/1789/Object-Serialization-using-C) and I think I finally almost have it working. When I save the file is created, but when I try to load, nothing is loaded. I feel like I'm close to getting this working. Any advice would be appreciated. Here's the code:
Save/Load class
[Serializable()]
public class SaveLoad : ISerializable
{
public int GameDay = Date.GameDay;
public List<Adventurer> Adventurers = FormMain.AdventurerManager.AdventurerList;
public SaveLoad()
{
GameDay = 0;
Adventurers = null;
}
public SaveLoad(SerializationInfo info, StreamingContext ctxt)
{
GameDay = (int)info.GetValue("Date", typeof(int));
Adventurers = (List<Adventurer>)info.GetValue("Adventurers", typeof(List<Adventurer>));
}
public void GetObjectData(SerializationInfo info, StreamingContext ctxt)
{
info.AddValue("Date", GameDay);
info.AddValue("Adventurers", Adventurers);
}
}
Save/Load methods:
void btnSaveGame_Click(object sender, EventArgs e)
{
SaveLoad save = new SaveLoad();
Stream stream = File.Open("SaveGame.osl", FileMode.Create);
BinaryFormatter bformatter = new BinaryFormatter();
bformatter.Serialize(stream, save);
stream.Close();
}
void btnLoadGame_Click(object sender, EventArgs e)
{
SaveLoad load = new SaveLoad();
Stream stream = File.Open("SaveGame.osl", FileMode.Open);
BinaryFormatter bformatter = new BinaryFormatter();
load = (SaveLoad)bformatter.Deserialize(stream);
stream.Close();
Date.CalculateDate();
this.Visible = false;
((FormMain)(this.ParentForm)).ControlMainScreen.Visible = true;
}
I think you may just have an initialization timing issue.
Try either moving you initialization of the GameDay and Adventurers to the constructor or get rid of the nulling them out in the constructor. Once I did the following, the code pretty much works:
public SaveLoad()
{
//GameDay = null;
//Adventurers = null;
}
Note I had to also make sure that the Adventurer class had the Serialization attribute.
Here is the code with the serialization that works for me (I had to create my own Adventurer class and I replaced the date with a string since I couldn't figure out what you were doing with it or where it was coming from. I also populated the adventurers list with some dummy data and commented out anything to do with the form stuff that I also didn't have information on.
[Serializable()]
public class SaveLoad : ISerializable
{
public string GameDay = null;
public List<Adventurer> Adventurers = null;
//FormMain.AdventurerManager.AdventurerList;
public SaveLoad()
{
GameDay = "Date";
Adventurers = new List<Adventurer>() { new Adventurer { Name = "a1", Type = "t1" }, new Adventurer { Name = "a1", Type = "t1" } }; ;
}
public SaveLoad(SerializationInfo info, StreamingContext ctxt)
{
GameDay = (string)info.GetValue("Date", typeof(string));
Adventurers = (List<Adventurer>)info.GetValue("Adventurers", typeof(List<Adventurer>));
}
public void GetObjectData(SerializationInfo info, StreamingContext ctxt)
{
info.AddValue("Date", GameDay);
info.AddValue("Adventurers", Adventurers);
}
}
[Serializable()]
public class Adventurer
{
public string Name { get; set; }
public string Type { get; set; }
}
private void btnLoadGame_Click(object sender, EventArgs e)
{
SaveLoad sl = new SaveLoad();
Stream stream = File.Open("SaveGame.osl", FileMode.Open);
BinaryFormatter bformatter = new BinaryFormatter();
sl = (SaveLoad)bformatter.Deserialize(stream);
stream.Close();
MessageBox.Show(sl.Adventurers.Count.ToString());
//Date.CalculateDate();
//this.Visible = false;
//((Form1)(this.ParentForm)).ControlMainScreen.Visible = true;
}
private void btnSaveGame_Click(object sender, EventArgs e)
{
SaveLoad sl = new SaveLoad();
Stream stream = File.Open("SaveGame.osl", FileMode.Create);
BinaryFormatter bformatter = new BinaryFormatter();
bformatter.Serialize(stream, sl);
stream.Close();
}