Link to home
Start Free TrialLog in
Avatar of JoergOas
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.Serialization;
using System.Reflection;
using System.CodeDom;
using System.ComponentModel.Design;
using System.ComponentModel.Design.Serialization;


namespace ClassLibrary1
{
      public class MyTest : System.ComponentModel.Component
      {
            private System.Collections.ArrayList myArrayList;

            public System.Collections.ArrayList MyArrayList
            {
                  get
                  {
                        return myArrayList;
                  }
                  set
                  {
                        myArrayList=value;
                  }
            }



            public MyTest()
            {
                  myArrayList = new System.Collections.ArrayList();
            }

            private void AddElement()
            {
                  myArrayList.Add(new Element(System.DateTime.Now.ToLongTimeString(),System.DateTime.Now.ToLongDateString()));
            }

            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;
                  }
            }
      }
}
Avatar of WinterMuteUK
WinterMuteUK
Flag of United Kingdom of Great Britain and Northern Ireland image

You need to mark 'Element' with [Serializable]

i.e.

[Serializable]
public class Element{
..
}

Cheers

Wint.
Avatar of JoergOas
JoergOas

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
Example:

ms-help://MS.MSDNQTR.2003APR.1033/cpref/html/frlrfSystemRuntimeSerializationFormattersBinaryBinaryFormatterClassTopic.htm

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
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.SerializeArrayList(myTest.MyArrayList);
                  Console.WriteLine("Press ENTER to Deserialize");
                  Console.ReadLine();

                  myTest = new MyTest();
                  myTest.MyArrayList = Serializer.DeSerializeArrayList();

                  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(ArrayList 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;
            }
      }
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!
ASKER CERTIFIED SOLUTION
Avatar of RomanPetrenko
RomanPetrenko

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
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
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