JoergOas
asked on
Serializing an ArrayList inside a control - URGENT - THANKS !!!
Hi,
I have a control that contains an ArrayList. During design time I add elements to the ArrayList. But the elements I created during design time don't get serialized.
Can anybody help ... THANKS a lot in advance ...
At the end of this message you get sample code. Here I create a element of the array list every time the change me property changes its value.
---- SOURCE ----
using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;
using System.Drawing.Design;
using System.Diagnostics;
using System.Runtime.Serializati on;
using System.Reflection;
using System.CodeDom;
using System.ComponentModel.Desi gn;
using System.ComponentModel.Desi gn.Seriali zation;
namespace ClassLibrary1
{
public class MyTest : System.ComponentModel.Comp onent
{
private System.Collections.ArrayLi st myArrayList;
public System.Collections.ArrayLi st MyArrayList
{
get
{
return myArrayList;
}
set
{
myArrayList=value;
}
}
public MyTest()
{
myArrayList = new System.Collections.ArrayLi st();
}
private void AddElement()
{
myArrayList.Add(new Element(System.DateTime.No w.ToLongTi meString() ,System.Da teTime.Now .ToLongDat eString()) );
}
private bool changeMe;
public bool ChangeMe
{
get
{
return changeMe;
}
set
{
changeMe=value;
this.AddElement();
}
}
}
public class Element
{
public Element(string timeIn, string dateIn)
{
Time=timeIn;
Date=dateIn;
}
private string time;
public string Time
{
get
{
return time;
}
set
{
time=value;
}
}
private string date;
public string Date
{
get
{
return date;
}
set
{
date=value;
}
}
}
}
I have a control that contains an ArrayList. During design time I add elements to the ArrayList. But the elements I created during design time don't get serialized.
Can anybody help ... THANKS a lot in advance ...
At the end of this message you get sample code. Here I create a element of the array list every time the change me property changes its value.
---- SOURCE ----
using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;
using System.Drawing.Design;
using System.Diagnostics;
using System.Runtime.Serializati
using System.Reflection;
using System.CodeDom;
using System.ComponentModel.Desi
using System.ComponentModel.Desi
namespace ClassLibrary1
{
public class MyTest : System.ComponentModel.Comp
{
private System.Collections.ArrayLi
public System.Collections.ArrayLi
{
get
{
return myArrayList;
}
set
{
myArrayList=value;
}
}
public MyTest()
{
myArrayList = new System.Collections.ArrayLi
}
private void AddElement()
{
myArrayList.Add(new Element(System.DateTime.No
}
private bool changeMe;
public bool ChangeMe
{
get
{
return changeMe;
}
set
{
changeMe=value;
this.AddElement();
}
}
}
public class Element
{
public Element(string timeIn, string dateIn)
{
Time=timeIn;
Date=dateIn;
}
private string time;
public string Time
{
get
{
return time;
}
set
{
time=value;
}
}
private string date;
public string Date
{
get
{
return date;
}
set
{
date=value;
}
}
}
}
ASKER
I changed it, but it doesn't work ...
Do I maybe need a CollectionTypeConverter ? How does this work ? Does anybody have a sample code for it ?
Thanks
bye
Joerg
Do I maybe need a CollectionTypeConverter ? How does this work ? Does anybody have a sample code for it ?
Thanks
bye
Joerg
Example:
ms-help://MS.MSDNQTR.2003A PR.1033/cp ref/html/f rlrfSystem RuntimeSer ialization Formatters BinaryBina ryFormatte rClassTopi c.htm
ms-help://MS.MSDNQTR.2003A
Where are you serialising to? I just ran a test serialising an ArrayList to a file and back again containing Elements (as you've defined + [Serializable] tag) and it worked.
Cheers Wint
Cheers Wint
This is the code I added to test it:
Runner - just runs the class as a console app.
Serializer - Serialise / Deserialise the arraylist.
Wint!
========================== ========== ========== ==
public class Runner
{
public static void Main()
{
MyTest myTest = new MyTest();
myTest.ChangeMe = true;
Thread.Sleep(1000); //Just to get two different times.
myTest.ChangeMe = true;
foreach (Element element in myTest.MyArrayList)
Console.WriteLine(element. ToString() );
Console.Write("Press ENTER to Serialize");
Console.ReadLine();
Serializer.SerializeArrayL ist(myTest .MyArrayLi st);
Console.WriteLine("Press ENTER to Deserialize");
Console.ReadLine();
myTest = new MyTest();
myTest.MyArrayList = Serializer.DeSerializeArra yList();
if(myTest.MyArrayList == null)
{
Console.WriteLine("Null returned.");
Console.WriteLine("Press ENTER to exit.");
Console.ReadLine();
return;
}
foreach (Element element in myTest.MyArrayList)
Console.WriteLine(element. ToString() );
Console.WriteLine("Press ENTER to exit.");
Console.ReadLine();
}
}
public class Serializer
{
public static void SerializeArrayList(ArrayLi st arrList)
{
FileStream fs = new FileStream("testing.bin", FileMode.Create);
BinaryFormatter bf = new BinaryFormatter();
bf.Serialize(fs, arrList);
fs.Close();
}
public static ArrayList DeSerializeArrayList()
{
FileStream fs = new FileStream("testing.bin", FileMode.Open);
BinaryFormatter bf = new BinaryFormatter();
ArrayList al = (ArrayList) bf.Deserialize(fs);
fs.Close();
return al;
}
}
Runner - just runs the class as a console app.
Serializer - Serialise / Deserialise the arraylist.
Wint!
==========================
public class Runner
{
public static void Main()
{
MyTest myTest = new MyTest();
myTest.ChangeMe = true;
Thread.Sleep(1000); //Just to get two different times.
myTest.ChangeMe = true;
foreach (Element element in myTest.MyArrayList)
Console.WriteLine(element.
Console.Write("Press ENTER to Serialize");
Console.ReadLine();
Serializer.SerializeArrayL
Console.WriteLine("Press ENTER to Deserialize");
Console.ReadLine();
myTest = new MyTest();
myTest.MyArrayList = Serializer.DeSerializeArra
if(myTest.MyArrayList == null)
{
Console.WriteLine("Null returned.");
Console.WriteLine("Press ENTER to exit.");
Console.ReadLine();
return;
}
foreach (Element element in myTest.MyArrayList)
Console.WriteLine(element.
Console.WriteLine("Press ENTER to exit.");
Console.ReadLine();
}
}
public class Serializer
{
public static void SerializeArrayList(ArrayLi
{
FileStream fs = new FileStream("testing.bin", FileMode.Create);
BinaryFormatter bf = new BinaryFormatter();
bf.Serialize(fs, arrList);
fs.Close();
}
public static ArrayList DeSerializeArrayList()
{
FileStream fs = new FileStream("testing.bin", FileMode.Open);
BinaryFormatter bf = new BinaryFormatter();
ArrayList al = (ArrayList) bf.Deserialize(fs);
fs.Close();
return al;
}
}
ASKER
Looks like I explained my problem pretty bad ... SORRY for that ... I try it again:
I add during design time elements in the attribute list (with a property editor). The elements I added during design time must serialize inside the code, i.e. in the method "InitializeComponent();". So adding an element to the attribute list is like changing a property. So its all about serializing to code, adding elements to the array during design time and having them available during runtime.
i.e. think of making a "font selection combobox", where you want to set in a property dialog during design time which fonts are available for selection during runtime.
I think I need to see the creating of the elements and the adding to the arraylist in the InitializeComponent section ... otherwise it wont work ... OR ?
I hopy you can help ... I am getting crazy with this problem ... thanks in advance!
I add during design time elements in the attribute list (with a property editor). The elements I added during design time must serialize inside the code, i.e. in the method "InitializeComponent();". So adding an element to the attribute list is like changing a property. So its all about serializing to code, adding elements to the array during design time and having them available during runtime.
i.e. think of making a "font selection combobox", where you want to set in a property dialog during design time which fonts are available for selection during runtime.
I think I need to see the creating of the elements and the adding to the arraylist in the InitializeComponent section ... otherwise it wont work ... OR ?
I hopy you can help ... I am getting crazy with this problem ... thanks in advance!
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Hi Joerg,
So just to get this clear in my head, you have say, err 'x' "Element's" that you want to create on initialization and store into the ArrayList in "MyTest"?
So the ArrayList starts with 'x' amount of Elements already there?
1. Are these Elements set to a specific Time/Date?
2. Does it have to be done in InitializeComponent, - Could it not be the next method call after InitializeComponent in the constructor?
Thanks!
Wint.
So just to get this clear in my head, you have say, err 'x' "Element's" that you want to create on initialization and store into the ArrayList in "MyTest"?
So the ArrayList starts with 'x' amount of Elements already there?
1. Are these Elements set to a specific Time/Date?
2. Does it have to be done in InitializeComponent, - Could it not be the next method call after InitializeComponent in the constructor?
Thanks!
Wint.
ASKER
Hi Wint,
On Creation of the control the array list is empty. The user can later on enter a property dialog and create items. The items that are created at design time must be stored in the code, e.g. like creating a tree control. You move it on your form, it has 0 elements, than you create elements during design time, the elements you create are stored in the source file.
Did I answer your question ?
thanks
bye
Joerg
On Creation of the control the array list is empty. The user can later on enter a property dialog and create items. The items that are created at design time must be stored in the code, e.g. like creating a tree control. You move it on your form, it has 0 elements, than you create elements during design time, the elements you create are stored in the source file.
Did I answer your question ?
thanks
bye
Joerg
ASKER
Hi Roman,
thanks so far for your answer ... I have to work through your example and the article. At the moment I implented a work around. Please give me another 2 days to get to it.
Thanks so far!
bye
Joerg
thanks so far for your answer ... I have to work through your example and the article. At the moment I implented a work around. Please give me another 2 days to get to it.
Thanks so far!
bye
Joerg
i.e.
[Serializable]
public class Element{
..
}
Cheers
Wint.