How to define Global Variable in Windows Forms with C#

Thursday, March 25, 2010

Create one Static class and declare static member/property into that class.

Now you can access that variable from any form in your windows application. Also you can able to set and get variable values.

GlobalModule Class:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

static class GlobalModule

private static string _GlobalVar = "";
public static string GlobalVar
get { return _GlobalVar; }
set { _GlobalVar = value; }

public static string FirstName = "Rajesh";

How to use global variables in application:

//Set the value to static variable
GlobalModule.GlobalVar = "VB.Net C#";

//Get the value from static variable
string CheckVal = GlobalModule.GlobalVar;
string FirstName = GlobalModule.FirstName;

Hope this post will help you,
if yes please put comment below of this page,
Rajesh Singh,
Asp.Net Developer
Indianic Infotech Ltd (India)


No comments :