Tuesday, July 6, 2010

How to define global variable in c# windows form

It is easy to use global like variable in web based application but quite hard to use it in windows form. Here the solution:

Add a new class.cs file to the project and then
static class GlobalClass
{
private string m_globalVar = "";
public static string GlobalVar
{
get { return m_globalVar; }
set { m_globalVar = value; }
}
}

Use it anywhere like this
GlobalClass.GlobalVar = "the_value_you_waant_to_put";
Access it anywhere like this
myFormLabel.Text = GlobalClass.GlobalVar;

Enjoy C#

No comments:

Post a Comment