I created a WPF application in c# with 3 different windows, Home.xaml, Name.xaml, Config.xam
l. I want to declare a variable in Home.xaml.cs
that I
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.
Right-click your project name in your solution explorer
Add > New Item
chooseClass
give it a name (I usually name it GLOBALS)
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; }
}
}
using ProjectName
GLOBALS.Variable1 = "MyName"
Console.Write(GLOBALS.Variable1)
GLOBALS.Variable2 = 100;
GLOBALS.Variable2 += 20;
GLOBALS.Variable3 = new MyObject();
GLOBALS.Variable3.MyFunction();
On a side note, do notice that using the static class as global variables in c# is considered a bad practice (that's why there is no official implementation for globals), but I consider it a shortcut for when I am too lazy haha. It should not be used in the professional environment.