seinc
asked on
How to serialize a custom collection class
I am trying to serialize a custom collection that I wrote (Class is shown below). It serializes fine, but I get nothing back out when I deserialize.
Can someone show me what I need to do to get my collection of objects back?
//my collection
[Serializable()]
public class PlayerCollection:Collectio nBase, ISerializable
{
public PlayerCollection()
{
}
public PlayerCollection(Serializa tionInfo info, StreamingContext context)
{
}
public virtual void Add(CrowCoder.Player NewPlayer)
{
this.List.Add(NewPlayer);
}
public virtual Player this[int Index]
{
get
{
//return the Widget at IList[Index]
return (Player)this.List[Index];
}
}
#region ISerializable Members
public void GetObjectData(Serializatio nInfo info, StreamingContext context)
{
info.AddValue("Players", this.List);
}
#endregion
public void SerializeMe()
{
try
{
Stream stream = File.Open("PlayerList.plr" , FileMode.OpenOrCreate);
BinaryFormatter bformatter = new BinaryFormatter();
bformatter.Serialize(strea m, this);
stream.Close();
}
catch(Exception ex)
{
}
}
}
//Code to use the collection
private void button1_Click(object sender, System.EventArgs e)
{
CrowCoder.Player p1 = new Player("Joe", "Smith");
CrowCoder.Player p2 = new Player("Mary", "Smith");
CrowCoder.Player p3 = new Player("John", "Smith");
CrowCoder.Player p4 = new Player("Jane", "Smith");
CrowCoder.PlayerCollection pc = new PlayerCollection();
pc.Add(p1);
pc.Add(p2);
pc.Add(p3);
pc.Add(p4);
pc.SerializeMe();
}
//Deserializing here, but collection comes up empty...........
private void button2_Click(object sender, System.EventArgs e)
{
CrowCoder.PlayerCollection mypc = new PlayerCollection();
Stream myStream;
BinaryFormatter bformatter = new BinaryFormatter();
myStream = File.Open("PlayerList.plr" , FileMode.Open);
bformatter = new BinaryFormatter();
mypc = (CrowCoder.PlayerCollectio n)bformatt er.Deseria lize(myStr eam);
myStream.Close();
}
Can someone show me what I need to do to get my collection of objects back?
//my collection
[Serializable()]
public class PlayerCollection:Collectio
{
public PlayerCollection()
{
}
public PlayerCollection(Serializa
{
}
public virtual void Add(CrowCoder.Player NewPlayer)
{
this.List.Add(NewPlayer);
}
public virtual Player this[int Index]
{
get
{
//return the Widget at IList[Index]
return (Player)this.List[Index];
}
}
#region ISerializable Members
public void GetObjectData(Serializatio
{
info.AddValue("Players", this.List);
}
#endregion
public void SerializeMe()
{
try
{
Stream stream = File.Open("PlayerList.plr"
BinaryFormatter bformatter = new BinaryFormatter();
bformatter.Serialize(strea
stream.Close();
}
catch(Exception ex)
{
}
}
}
//Code to use the collection
private void button1_Click(object sender, System.EventArgs e)
{
CrowCoder.Player p1 = new Player("Joe", "Smith");
CrowCoder.Player p2 = new Player("Mary", "Smith");
CrowCoder.Player p3 = new Player("John", "Smith");
CrowCoder.Player p4 = new Player("Jane", "Smith");
CrowCoder.PlayerCollection
pc.Add(p1);
pc.Add(p2);
pc.Add(p3);
pc.Add(p4);
pc.SerializeMe();
}
//Deserializing here, but collection comes up empty...........
private void button2_Click(object sender, System.EventArgs e)
{
CrowCoder.PlayerCollection
Stream myStream;
BinaryFormatter bformatter = new BinaryFormatter();
myStream = File.Open("PlayerList.plr"
bformatter = new BinaryFormatter();
mypc = (CrowCoder.PlayerCollectio
myStream.Close();
}
ASKER
This is what my PlayerList.plr has in it:
ÿÿÿÿ HMemoryGame, Version=1.0.2156.33581, Culture=neutral, PublicKeyToken=null CrowCoder.PlayerCollectio n PlayersCrowCoder.Player Collection
It doesn't look like there is any data in it, but I wasn't sure since I'm looking at binary data in Notepad.
ÿÿÿÿ HMemoryGame, Version=1.0.2156.33581, Culture=neutral, PublicKeyToken=null CrowCoder.PlayerCollectio
It doesn't look like there is any data in it, but I wasn't sure since I'm looking at binary data in Notepad.
it doesnt look like your CrowCoder.Player is serializaable only your PlayerCollection.
Regards,
Ready1
Regards,
Ready1
ASKER
I can serialize and deserialize the Player class.
I think I'm missing something here:
public PlayerCollection(Serializa tionInfo info, StreamingContext context)
{
}
but I don't know what. I tried setting the collection's List but it is read only.
I think I'm missing something here:
public PlayerCollection(Serializa
{
}
but I don't know what. I tried setting the collection's List but it is read only.
SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
Is the Player class serializable as well?
ASKER
Yes, the player class is serializable and I have tested to make sure I can serialize and deserialize it.
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
ASKER
Thanks dstanley9, I haven't had time to test this yet, but it looks like what I was missing. I'll get back soon.
ASKER
Well, that doesn't work either. I made it a little furthur with the code below, using the collection's inner list:
public PlayerCollection(Serializa tionInfo info, StreamingContext context)
{
ArrayList myList = (ArrayList)info.GetValue(" inner", typeof(ArrayList));
System.Collections.IEnumer ator myEnumerator = myList.GetEnumerator();
while(myEnumerator.MoveNex t())
{
this.InnerList.Add(myEnume rator.Curr ent);
}
}
I'm giving up for now. I will just store my player objects separately. I found a reference to DesignerSerializationVisib ilityAttri bute but I can't apply it to my code.
public PlayerCollection(Serializa
{
ArrayList myList = (ArrayList)info.GetValue("
System.Collections.IEnumer
while(myEnumerator.MoveNex
{
this.InnerList.Add(myEnume
}
}
I'm giving up for now. I will just store my player objects separately. I found a reference to DesignerSerializationVisib
public void SerializeMe()
{
Stream stream = null;
try
{
stream = File.Open("PlayerList.plr"
BinaryFormatter bformatter = new BinaryFormatter();
bformatter.Serialize(strea
}
catch(Exception ex)
{
}
finally
{
stream.Close();
}
}
Does your "PlayerList.plr" have data in it?
Do you know that no exception was thrown during serialization?
REgards,
Ready1