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.
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
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
Open Form2 in the form designer. Add one button.
Double-click Button1, add the following code to its event code:
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!
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.