Results 1 to 2 of 2

Thread: [RESOLVED] Issues serializing arraylists and strings

Threaded View

  1. #1

    Thread Starter
    KrisSiegel.com Kasracer's Avatar
    Join Date
    Jul 2003
    Location
    USA, Maryland
    Posts
    4,985

    Resolved [RESOLVED] Issues serializing arraylists and strings

    I created the PrefBox to be a holder of all the settings my application needs. I then created an instance of it (MyPrefs) in this same class so I have a way to access it from all of my windows. This same class also serializes the MyPref class and then reloads it later.

    It works great except for one part: I cannot serialize an ArrayList or an additional string.

    What I'm trying to do is keep a list of last used files so the user can see the last items they opened so they can open them again easier. Anyway, when I use an ArrayList, everything works except for the either the Serialization or the Deserialization part. I have tried using MessageBox.Show() to see what was being done and it looked like it was correct when it was going to be serialized but after deserialization, it's empty. Same if I use a string and basically keep the history ';' delimited. It has all the information at time of serialization but after deserialization, it's empty.

    What am I doing wrong? It has to be something simple
    Code:
    	public class cPref
    	{
    		[System.Serializable]
    		public struct PrefBox
    		{
    			//General
    			public bool agreed;
    			public bool WordWrap;
    			public bool StatusBar;
    			public bool TabsAsSpaces;
    			public int SpacesPerTab;
    			public bool AutoTab;
    			public string CPlusExt;
    			public bool checkUpdatesAtLaunch;
    			public int windowStateW;
    			public int windowStateH;
    			public System.Windows.Forms.FormWindowState state;
    			public string openAs;
    			public System.Collections.ArrayList History;
    		}
    		public static BinaryEdit.cPref.PrefBox MyPrefs = new BinaryEdit.cPref.PrefBox();
    		public static void Defaults()
    		{
    			//Default Settings Here
    		}
    		public static bool Load()
    		{
    			if (!DeserializePrefs())
    			{
    				Defaults();
    				return false;
    			}
    			else
    			{
    				return true;
    			}
    		}
    		public static bool SerializePrefs()
    		{
    			System.Windows.Forms.MessageBox.Show(MyPrefs.sHistory);
    			try
    			{
    				using ( System.IO.FileStream fout = new System.IO.FileStream("prefs.besb", System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.Write))
    				{
    					System.Runtime.Serialization.Formatters.Binary.BinaryFormatter bout = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
    					bout.Serialize(fout, MyPrefs);
    				}
    				return true;
    			}
    			catch
    			{
    				return false;
    			}
    		}
    		public static bool DeserializePrefs()
    		{
    			try
    			{
    				using ( System.IO.FileStream fin = new System.IO.FileStream("prefs.besb", System.IO.FileMode.Open, System.IO.FileAccess.Read))
    				{
    					System.Runtime.Serialization.Formatters.Binary.BinaryFormatter bout = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
    					MyPrefs = (BinaryEdit.cPref.PrefBox)bout.Deserialize(fin);
    				}
    				System.Windows.Forms.MessageBox.Show(MyPrefs.sHistory);
    				return true;
    			}
    			catch
    			{
    				return false;
    			}
    		}
    	}







    Added [RESOLVED] to thread title - Hack
    Last edited by Hack; Dec 12th, 2005 at 10:31 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width