Serializing classes created with Singleton pattern - c#

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

Related

pass data from one activity to another in Xamarin.Android

I wanted to pass a Class Object from one activity to another in Xamarin.Android app.
I can pass the simple strings using Intent.PutExtra method.
Does anybody know about it. anyhelp is appreciated :)
Just adding in case someone else comes across this. The nice thing about Xamarin/.NET is how easy it is to use JSON. You can Serialize your data to a string and pass that through the Extras.
JSON.NET is a nice library (that you can find on the Xamarin component store) for this and there is also some built in JSON classes in .NET. An example using JSON.NET would be like this.
Intent i = new Intent(Application.Context, typeof(SecondActivity));
i.PutExtra("key", JsonConvert.SerializeObject(myObject));
StartActivity(i);
And in the other Activity you can deserialize it.
var obj = JsonConvert.DeserializeObject<OBJ_TYPE>(Intent.GetStringExtra("key"));
This is better than using a static reference in my opinion.
The concept is the same as with a standard (non-Xamarin) application.
You can use Intent#putExtra(String, Parcelable) to pass any object that implements the Parcelable interface as an extra.
The Parcelable interface is a little bit complex, so be sure to read the documentation to ensure that your class conforms to the requirements. You may also want to check out this SO question for more information on creating a Parcelable class.
You cannot pass an object reference via an Intent. This is because Activities are designed to work completely independently of each other. Users can throw your Activity in the background while performing other tasks, so it is entirely possible (and very likely) that your Activity's variables will be garbage collected. When the user later comes back to your Activity, it should be able to recreate its state.
If you really need to pass a reference to an object directly, you can do so by making that object a static variable. While this is a quick and dirty way to solve the problem of getting data from one Activity to another, it does not solve the problem of the variable potentially being garbage collected at some point, and is generally a poor design choice.

polymorphism with Windows Forms

I'm making a project to get myself more familiar with Windows Forms and Graphic User Interfaces.
I have created this program for the Department of Motor Vehicles that uses polymorphism in CONSOLE. So when I input a taxi, it will call the base class of an industrial vehicle rather than a personal vehicle.
The program works fine in console.
But I'm wondering if that's implementable through a Graphical Interface. I know I can just have buttons with the types of vehicles, then have a new form open up to input that data for that specific type of vehicle. But that wouldn't be polymorphism....
Is this a type of project that could be done with polymorphism? and GUI's or no?
I think you would get more bang for the buck if only one form was created which handled the base class as mentioned. But it would turn on/off or make visible items as required by the derived classes. The GUI doesn't have to be polymorphic, it just needs to handle the polymorphism of the data. HTH
You'll have to be more specific about what you want to achieve. Polymorphism can be applied to most problems, if you like. Whether or not it's a good technique varies, and depends very much on how you use it. You seem to be forming ideas about how your object hierarchy will work early on, whereas I would suggest that you don't start there - instead specify what your application should do and how it should do it, and design your object model around that. It may turn out that your idea of how to represent (given your example) a taxi actually isn't useful.
There is no reason why you can't benefit from polymorphism in any object-oriented application, regardless of what user interface you elect to use. In your scenario, it may make sense to use only references to the base class in your list view, and then open up the appropriate details view suited to the specific type of the object.
Also, I recommend WPF for what it's worth. There's no use learning Windows Forms now unless you have a very good reason.
Perhaps what you are looking for is a way to dynamically build your GUI according to the type of (polymorphic) object you are passing? This can be done by using reflection, asking the object passed to the Form which attributes or properties it has and generate automatically input fields, text boxes etc. for each attribute.
For some examples, read this SO post:
Dynamic options dialog (using reflection)

Silverlight: Where to store data used across pages

I have a Silverlight application that consists of many pages that uses Navigation Framework. What is the ideal place to store data that should be accessed across all pages (XAMLs) and throughout
the lifetime of the application.
EDIT: Forgot to mention that I am currently doing it as a static class
Static members are generally a bad idea. You have no control over lifespan or ability to easily substitute another set of data (and don't get me started on the inability to do proper unit testing). You want to use some type of shared View Model/Data model.
If you are not going the whole PRISM route (we always use PRISM now for Silverlight and WPF), or Unity, or even just MVVM, then use simple singleton accessors on your data object.
There are lots of discussions over the best patterns for C# singletons, but you can learn a lot here http://www.yoda.arachsys.com/csharp/singleton.html
Hope this helps.
I like to create a class called Session, with a static property like this public static Session Default {get {return App.Current.Resources["Session"] as Session;}}, then create a new instance of it in app.xaml like this <classes:Session x:Name="Session"/>, now you can access it in code behind with Session.Default... and you can bind to it with Source binding and it will always be the same instance. I have expanded this pattern to a more complex and flexible pattern with base classes etc but that should suffice for your purposes. I wrote this code in this web window, it might not compile, if you need more help just let me know

Where's the best place to store a commonly-used variable?

What are the best way to store variables in a silverlight application?
Need to transfer store a customer ID throught the application but im not sure what is the best way
Disclaimer: This is a purely subjective answer. Others might object or have better suggestions.
I work mostly in VB.NET and over there, we've got the My.Application namespace where we can keep global variables. VB.NET users also have the option of using a Module for such purposes.
A Module, if I remember right, is equivalent to a static sealed class in C# so you can essentially do something of that sort.
To replicate VB.NET's functionality when I work in C#, I create a static class, with access level set to internal so its members are accessible from within the entire application.
Thus, when I assign a value to a member of the static class, it is accessible from all other classes in the application.
Hope this helps
Store the variable in a place where those things that need to get to it, can; and those things that don't need to get to it, can't. Can't say anything more specific without more information.
If you were following an MVVM pattern then I would have said as a property of the Customer model, with an instance of the customer model being accessed via the ViewModel.
Even if you aren't I would say within the application code and use binding where its needed in the UI. Otherwise you run the risk of changes to your UI causing the loss of customer ID storage at somepoint in the future.
If needed in more than one place then just create a repository that stores all of your data and have that accessed as needed (this way you can decouple your UIs from each other even if they use the same data source.
You may look at using InitParams, without knowing the situation I can't say much more.
http://msdn.microsoft.com/en-us/library/cc838255(VS.95).aspx

Where to maintain common information, that could be accessed by all forms

This is my first winform app in .NET... I have made a couple of ASP.NET app...
In this app, I have some common variables, like current user name, his selected process etc.. etc.. This should be made accessible from all forms in the app at any time... How could I do this... Is there any place like "Session" in ASP.NET here...
Further How do coders generally pass information from one form to another... Here I want to pass on the info I acquired in the first form to the subsequent forms... I use constructor overloading and pass the values as parameters... I'm pretty sure there has to be a better way to do it...
Thanks for your time...
You might want to implement a Singleton object
Implementing the Singleton Pattern in C#
This is typically used for thread safe code, but will also allow you to access the same instance from multiple forms, allowing you to use the same data without having to pass the object around from form to form.
There are quite a number of ways.
Static objects - These are shared accross applications so you can access them from any form or class. This is not advised by many and singleton classes are preferred, but I dont find any problem with these in winform applications.
Public Properties - These are form specific rather than global. Pretty much similar to ASP.NET usage.
Project Settings collection - This can be used to store data that might not change during the application lifecycle. All the forms can access this.
I'd use singleton in this case, checkout this generic singleton implementation.

Categories

Resources