In my program, All of my classes are using singleton pattern, except the one which is the main window. Because of that, all the singleton connections are maintained by one singleton class, “Manager”. GUI access methods in other classes via the public methods in this class.
Now, I am trying to save my work (serialize) and I am always getting the error “Form1 not serialized”. That is the GUI class. So, I marked that as serialized, knowing that is not a good idea. Now it is saying “System.windows.forms not serialized”. Why is this? Is it unable to serialize a class with singleton access? Please help.
Note: I am a Java developer learning “Head First C#”. This is my first attempt to make one of their “Lab” problems in my own preferred way.
Without seeing code it’s hard to know what is wrong. Principally one can serialize a form (I just did so using the sample
Test<T>(T obj)method from MSDN which uses DataContractSerializer).Having said that… it is unwise to serialize the form itself in order to save the form state. Instead, you should keep your data in a separate class (commonly referred to as a Model class, see MVC) and serialize the data instead. Use data binding or the MVC pattern to connect your form (the view) to the data (the model).
If this is WinForms (as I presume), one can use MVC with WinForms
https://stackoverflow.com/questions/2406/looking-for-a-mvc-sample-for-winforms
For information about data binding with WinForms, see
http://msdn.microsoft.com/en-us/library/ef2xyb33.aspx