NHQue
NHQue

Reputation: 73

C# - Variable and List accessible in whole project

Is it possible to make a variable or a List of items accessible to the whole project? My program selects an object in one view and a I want to have access/change it another one.

I know this not the best workaround and it would be better to use a MVVM-pattern for this, but it seems a big effort to implement this properly just for this simple usecase of one ot two variables/lists.

Upvotes: 1

Views: 667

Answers (1)

Andrei Dragotoniu
Andrei Dragotoniu

Reputation: 6335

Sharing data can be done in multiple ways.

One interesting way could be to cache the data, have a look at this for example : https://learn.microsoft.com/en-us/dotnet/desktop/wpf/advanced/walkthrough-caching-application-data-in-a-wpf-application?view=netframeworkdesktop-4.8

I would recommend against using any global variables, I would also recommend not using static variables either as you might open yourself up to sharing data between users for example.

In this example, when you need the data, you check if you have it in the cache, if not you load it from wherever ( db, file, api, whatever your source is) and then you simply read it from the cache wherever and whenever you require it.

If you need to update it, then you make sure you update it to whatever storage mechanism you have and then you reload the cache. This is a good way to keep things in sync when updates are needed without complicating the application, its testing and the maintenance.

Upvotes: 2

Related Questions