?
Solved

Serializing an ArrayList inside a control - URGENT - THANKS !!!

Posted on 2005-04-01
10
Medium Priority
?
237 Views
Last Modified: 2010-04-16
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;
                  }
            }
      }
}
0
Comment
Question by:JoergOas
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
10 Comments
 
LVL 9

Expert Comment

by:WinterMuteUK
ID: 13679197
You need to mark 'Element' with [Serializable]

i.e.

[Serializable]
public class Element{
..
}

Cheers

Wint.
0
 

Author Comment

by:JoergOas
ID: 13679534
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
0
 
LVL 6

Expert Comment

by:melodiesoflife
ID: 13679750
Example:

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

0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 9

Expert Comment

by:WinterMuteUK
ID: 13679835
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
0
 
LVL 9

Expert Comment

by:WinterMuteUK
ID: 13679890
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;
            }
      }
0
 

Author Comment

by:JoergOas
ID: 13682795
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!
0
 
LVL 8

Accepted Solution

by:
RomanPetrenko earned 2000 total points
ID: 13688120
I've made such DesignTime support for Custom TabPage:
look here:
http://www.experts-exchange.com/Programming/Programming_Languages/C_Sharp/Q_20861404.html#10251275

In general: you have to create custom designer for your component. It will control serialization into the code. Also you can control serialization from/into resource with TypeConverter
look here:
http://www.divil.co.uk/net/articles/designers/collectioncontrols.asp
0
 
LVL 9

Expert Comment

by:WinterMuteUK
ID: 13695737
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.
0
 

Author Comment

by:JoergOas
ID: 13695824
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
0
 

Author Comment

by:JoergOas
ID: 13695831
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
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question