HOWTO: Use application global variables in VB.NET

While some purists of programming theory will adamantly oppose the use of application global variables, they are usually not the people in the trenches that have a project that must be completed yesterday. While you do need to limit your use of application-wide global variables to the absolute minimum (otherwise you can end up with some horribly splintered code), sometimes you just have to use a global variable accessible to the whole application or you’re going to have a massive mess on your hands from passing data back and forth between functions. (That’s pretty much the decision point on when to use a global variable – is the code going to be more or less understandable, thus maintainable, if you go ahead and use a global variable.)

An instance where I generally find that the use of a global variable is an “acceptable risk” is in a Windows Form application with multiple forms that all need to access the same array. Without using the global, you’d be stuck with passing an entire array between forms. This would be a terrible waste of time that introduces unnecessary complexity to your project. Just go ahead and create your global variable and tell the purists to go talk to the hand.

But, how do you go about creating a global variable? Where does the declaration go? That is the tricky question, but with a relatively simple solution. All you do is add a separate class to your application and declare your globals in that class as “Public Shared”.

Here’s an example in Visual Basic.NET:

Create a new project. This will create an empty project with a blank Windows Form with the name Form1.
Click on Project|Add Class. Give the new class a name like clsGlobal. This is where your global variables will live.
Click on Project|Add Windows Form, go with the default name of Form2 for this example. We’ll use this to show that global data modified in Form1 can be accessed from Form2.

clsGlobal:

Open the code for clsGlobal in the code editor. Add the following line between Public Class clsGlobal and End Class:

    Public Shared test As String

Form1:

Open Form1 in the form designer. Add two buttons.

Double-Click Button1, add the following code to its event code:

clsGlobal.test = Now

Double-Click Button2, add the following code to its event code:

Dim frm2 As Form2
      frm2 = New Form2

frm2.Show()

Form2:

Open Form2 in the form designer. Add one button.

Double-click Button1, add the following code to its event code:

MessageBox.Show(clsGlobal.test)

You now have a working example. Run the code.

Click Button1 on Form1 to set the current time in the global variable named test.

Click Button2 on Form1 to show Form2.

Move Form2 so that you can see both Form1 and Form2 at the same time.

Click Button1 on Form2, a messagebox will display the time and date when Button1 on Form1 was clicked.

Click Button1 on Form1 again, then click Button1 on Form2 – you will see the time stored in the global variable has updated!

This is the basics on how to use a global variable in VB.NET. Enjoy!
Reply
Replies:
Comfortably Anonymous
3/24/2005 8:18:10 PM
Comfortably Anonymous
3/26/2005 1:17:38 PM
Comfortably Anonymous
2/23/2006 6:09:44 PM
Comfortably Anonymous
4/7/2006 3:30:40 PM
- RE: [Read 602]
Comfortably Anonymous
3/25/2005 6:05:04 PM
Comfortably Anonymous
3/28/2005 10:59:57 AM
Comfortably Anonymous
4/6/2005 2:35:40 PM
Comfortably Anonymous
4/13/2005 9:28:55 AM
Comfortably Anonymous
3/6/2006 9:09:35 PM
Comfortably Anonymous
3/8/2006 7:41:32 PM
Comfortably Anonymous
3/13/2006 8:36:26 AM
Comfortably Anonymous
3/13/2006 8:35:50 AM
- RE: [Read 565]
Comfortably Anonymous
4/22/2005 8:55:14 PM
This site contains copyrighted material the use of which has not always been specifically authorized by the copyright owner. We are making such material available in our efforts to advance understanding of environmental, political, human rights, economic, democracy, scientific, and social justice issues, etc. We believe this constitutes a 'fair use' of any such copyrighted material as provided for in section 107 of the US Copyright Law. In accordance with Title 17 U.S.C. Section 107, the material on this site is distributed without profit to those who have expressed a prior interest in receiving the included information for research and educational purposes. For more information go to: http://www.law.cornell.edu/uscode/17/107.shtml . If you wish to use copyrighted material from this site for purposes of your own that go beyond 'fair use', you must obtain permission from the copyright owner.