tags:

views:

65

answers:

3

Hi All,

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...

+1  A: 

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.

astander
Thanks Astander... And How will it react, If I run two instance of same Application in the same machines... Like running two notepads... Will this object be common for both... If that being the case, Is there anyway to avoid this...
The King
No, this will not be the case, it will create its own version per process, so that should be fine.
astander
Thanks.. Just one more Question... Why cant we use Static Class, and static members, for this... I presume they wont be shared too...
The King
It should be fine as long as this does not require thread safe access, then you should be able to use that.
astander
Thank a lot ...
The King
+1  A: 

There are quite a number of ways.

  1. 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.
  2. Public Properties - These are form specific rather than global. Pretty much similar to ASP.NET usage.
  3. Project Settings collection - This can be used to store data that might not change during the application lifecycle. All the forms can access this.
Aseem Gautam
A: 

I'd use singleton in this case, checkout this generic singleton implementation.

KMan
Topic is about winform application. There is no session object in windows applications.
Aseem Gautam
@Aseem: Thanks for pointing out.
KMan