WPF Application using a global variable
Solution 1
The proper way, especially if you ever want to move to XBAPP, is to store it in
Application.Current.Properties
which is a Dictionary object.
Solution 2
To avoid having to pass around values between windows and usercontrols, or creating a static class to duplicate existing functionality within WPF, you could use:
- setting:
App.Current.Properties["NameOfProperty"] = 5;
- getting:
string myProperty = App.Current.Properties["NameOfProperty"];
This was mentioned above, but the syntax was a little off.
This provides global variables within your application, accessible from any code running within it.
Solution 3
You can use a static property:
public static class ConfigClass()
{
public static int MyProperty { get; set; }
}
Edit:
The idea here is create a class that you holds all "common data", typically configurations. Of course, you can use any class but suggest you to use a static class. You can access this property like this:
Console.Write(ConfigClass.MyProperty)
Solution 4
As other people mentioned before either use App.Current.Properties
or create a static class.
I am here to provide an example for those who need more guidance with the static class.
- Add a new Class
Right-click your project name in your solution explorer
Add > New Item
chooseClass
give it a name (I usually name it GLOBALS)
- What that cs file should look like
using System;
namespace ProjectName
{
public static class GLOBALS
{
public static string Variable1 { get; set; }
public static int Variable2 { get; set; }
public static MyObject Variable3 { get; set; }
}
}
- Add references into .cs files where you intend to use those variables
using ProjectName
- We're done. Use it. Examples:
GLOBALS.Variable1 = "MyName"
Console.Write(GLOBALS.Variable1)
GLOBALS.Variable2 = 100;
GLOBALS.Variable2 += 20;
GLOBALS.Variable3 = new MyObject();
GLOBALS.Variable3.MyFunction();
A note about unit testing and static classes / static variables
It is generally considered bad practice to use a static class to hold global variables, one of the major reasons being that it can significantly hinder the ability to properly unit test your code.
If however you need code that is testable and you do still want to use a static global variable class, then at least consider using a singleton pattern for accessing your GLOBALS. [more details here: https://jonskeet.uk/csharp/singleton.html]
Below is a useful snippet of what I mean. Make GLOBALS abstract and remove the static, then add the following inside GLOBALS class:
private static GLOBALS instance = null;
/// <summary>
/// The configuration instance used by the application and unit tests
/// </summary>
public static GLOBALS Instance
{
get
{
if (instance == null)
{
//Create the default configuration provider
instance = new AppConfigConfiguration();
}
return instance;
}
set
{
instance = value;
}
}
AppConfigConfiguration (in the example above) is an application specific settings class which derives from GLOBALS. The singleton pattern allows other configurations to also be derived, and optionally set on the Instance property, a common occurrence being prior to unit tests running so that test specific settings can be assured.
Solution 5
There are two different things you can do here (among others; these are just the two that come to mind first).
You could make the variable static on Home.xaml.cs
public static string Foo = "";
You could just pass in the variable to all three forms.
I would go with #2, myself, and if necessary create a separate class that contains the data I need. Then each class would have access to the data.
Jake
Updated on September 28, 2021Comments
-
Jake over 2 years
I created a WPF application in c# with 3 different windows,
Home.xaml, Name.xaml, Config.xam
l. I want to declare a variable inHome.xaml.cs
that I can use in both the other forms. I tried doingpublic string wt = "";
but that didn't work.How can I make it usable by all three forms?
-
Jake almost 15 yearsBut I want to add a value to the variable in home and use that value in config. If I declare in all the forms the value gets erased. How do I declare it so I can use it continuously in all three forms.
-
David Mason about 13 yearsProperties isn't coming up as a member of Application.Current, is there something special to do in order to access it? EDIT: it just occurred to me that this could be because I'm making a Windows Phone application so I may have slightly different libraries.
-
Henk Holterman about 13 years@dr monk: Win Phone (7) means SilverLight, this answer was about WPF.
-
Mohammad Mahroz over 6 yearswhat if we need to save class objects? will it work the same way?
-
FAB over 5 yearsI had to add
.ToString()
to get the value from the property, but other than that, this works fine for me, thanks for sharing.string myProperty = App.Current.Properties["NameOfProperty"].ToString();
-
Pang over 3 yearsFirst code snippet doesn't seem to compile. Redundant parentheses on first line?