I created a WPF application in c# with 3 different windows, Home.xaml, Name.xaml, Config.xaml. I want to declare a variable in Home.xaml.cs that I can use in both the other forms. I tried doing public string wt = ""; but that didn't work. How can I make it usable by all three forms?
+1
A:
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)
Eduardo Cobuci
2009-07-21 20:05:48
I'm not sure I understand...can you explain please. Thank you.
Jake
2009-07-21 20:10:09
A:
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.
Ari Roth
2009-07-21 20:09:02
But 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.
Jake
2009-07-21 20:19:25