开发者

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 na开发者_如何学编程me, 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.

  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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜