Bhaskar
Bhaskar

Reputation: 10691

WinForm App Data Persistance (C#)

I beleive the best ways of "variable short term" persistance in an ASP.NET application are:

  1. Sessions Variable (Session Scope)
  2. Application Variable (Application Scope)
  3. Page View (Page Scope)
  4. Application Settings (Application Scope)
  5. ???

What are the best ways of "variable short term" persistance in a windows form application for:

  1. Form Scope
  2. User Session Scope
  3. Application Global Scope

Thanks

Upvotes: 2

Views: 2265

Answers (5)

Lazarus
Lazarus

Reputation: 43114

I'm not absolutely sure what you are looking to persist beyond the boundaries of the object lifecycle but as Henk has stated, your Form has scope for the duration of time that it's loaded and you can add properties to the form that can be initialised by your code when the Form is instantiated and will last till the Form is unloaded. The next scope up is really the Application object (unless you wrap the forms in some kind of custom container class) where you could add properties for the life of the application (and essentially the Application object).

To persist beyond the scope of the Application then use the Properties class or store data in the registry (in an appropriately identified and named location).

It sounds like you are thinking somewhat procedurally and it sounds a bit like global variable (or at least larger than method or object scope) persistence. Rather than thinking in terms of variables, think in terms of objects and properties of those objects. If you've correctly designed your object model then the persistence of the appropriate properties should be a function of that.

Upvotes: 0

bohdan_trotsenko
bohdan_trotsenko

Reputation: 5367

Right-click the project, select properties->Settings. You can edit persistent fields (i.e. settings), specifying name, type and scope (user-wide or application-wide).

You can access them from the code by <Default Namespace>.Properties.Settings.Default.

The settings are persistent between application runs.

You should use these settings for the Form Scope too.

All these settings make sense for storing persistent values between application runs. Use regular (static) fields for storing data within one program instance.

Upvotes: 3

Matt Warren
Matt Warren

Reputation: 10291

You can specify whether a settings is for the current user or global when you create it. If you look in the projects properties in VS you see this

alt text http://img268.imageshack.us/img268/9186/projectsettings.png

Upvotes: 1

James
James

Reputation: 82136

For variables only accessible by the form I would just make them private fields. There isn't such as a thing as a "session" in a win forms application, however, you could use the CallContext to simulate a session as HttpContext and Session in a Web application are based around this class.

Anything global I would probably store in the Application object itself or the application configuration file.

Upvotes: 0

Henk Holterman
Henk Holterman

Reputation: 273784

Well, for "Form Scope" you can simply use fields or properties. For application settings and session settings you can use a (static) class, or anything else that is convenient.

Note that there really is no difference between Application and Session in a WinForms app, you're not on a Server anymore.

Upvotes: 3

Related Questions