UPDATE: Use application global variables in VB.
Posted: 3/28/2005 10:59:57 AM
By: Comfortably Anonymous
Times Read: 3,031
0 Dislikes: 0
Topic: Programming: .NET Framework
Parent Message
A minor update to the HOWTO on global variables:

This isn't absolutely needed, but it's still a good idea. Generally, a class is just a definition of how to 'instantiate' an object based on the class. More of a blueprint of an object than something you should use directly. Good programming practice says that you should 'instantiate' a class into an actual object before using it.

In truth, a non-instantiated class shouldn't work, but it seems to work just fine in VB.NET, which is actually what is happening in the original version of this HOWTO.

It's a bit mysterious what is going on by using the public shared variables in the non-instantiated class clsGlobals. (Without instantiating the class, we are basically using a 'ghost' object.) To do it correctly, with instantiated objects requires a small change to the example.

Let's just start over from the beginning and do it right:

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:

Right-click on Form1 in the Solution Explorer, select View Code. Right between "Windows Form Designer Generated Code" and "End Class", put the following:

Dim Globals As New clsGlobal 'Instantiate the object

Now, open Form1 in the form designer. Add two buttons.

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

Globals.test = Now ' Note use of instantiated object name rather than class name.

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

Dim frm2 As Form2
frm2 = New Form2

frm2.Show()

Form2:

Select Form2 in the Solution Explorer. Right-click and select View Code. Right between "Windows Form Designer Generated Code" and "End Class", put the following:

Dim Globals As New clsGlobal 'Instantiate the object

Now we have two separate objects, one instantiated as part of Form1, the other instantiated as part of Form2. Now we refer to the variable 'test' using the object name (Globals, aka: Form1.Globals.test and Form2.Globals.test) rather than the class name (clsGlobals). This is where the keyword "Shared" kicks in - it causes all instances of a class to share the same data. We have two separate instantiations of the same class, one called Form1.Globals, the other Form2.Globals. Without the word shared, each of the instantiated objects would contain a variable called 'test' with completely unrelated data in each. Changing the Form1.Globals.test would have no effect on Form2.Globals.test. However, since the keyword 'shared' is used in the class definition of the variable named 'test', all instances refer to the same data, thus we can use it as a global variable just like in the first HOWTO example, but with proper class instantiation this time.

Open Form2 in the form designer. Add one button.

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

MessageBox.Show(Globals.test) ' Note use of object name rather than class name

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!

Note that this works exactly the same as the first example. Just that we are not using the mysterious 'ghost object' clsGlobal, but are instantiated actual objects based on that class definition. If anyone knows any info about why the 'ghost object' even works at all, please post here. It's really got me intrigued, but I cannot find any explanation of it.
Rating: (You must be logged in to vote)