Global Variables in Visual Basic .NET
Everything you need to know about Global Variable for VB.NET in this single Post.
Everything you need to know about Global Variable for VB.NET in this single Post. First of all, I am not here to debate whatever you should use or not Global Variables. In fact, no one cares and neither than me. Many have their reasons on why not to use them while other says that we should use them because we need them.
Anyway, if you are here, is because you want to learn how to use them or wish to have an opinion on global variables. Please take a look on the project sample I made especially for this post.
I suggest you to download the project sample; it would be easier to understand. Because I am not good in English and my background is not computer science. Sorry if I express myself differently that you.
One thing I want to say and we have to know is that a global variable is available everywhere. Unfortunately, global variable doesn�t exist that way in Visual Basic .NET. (Same for C#). Only to make this post easier to read, I will always use the term global variable even if in VB is not considered a Global Variable.
Global Variable, say it! Global Variable. Say it again Global Variable.
Two ways to make a global variable
With a Module like Module1.vb, the variable iOne :
Module Module1 Public iOne As Integer End Module |
This is a second way, using a class named Class1, the other global variable is iTwo. If want want to make iTwo act like a global variable, we need to use the word shared. In C#, the equivalent is static.
Public Class Class1 Public Shared iTwo As Integer End Class |
Do not mix the word static in C# with Static in other programming language. It might not be the same thing. Always stay in .NET (VB or C#).
I made a previous post about shared functions. I haven�t spent a lot of time explaining everything. If you read it, you might have an idea on what is a shared function or shared variable.
I finally made a form1 with 2 Labels and 2 buttons.
Public Class Form1 Private WithEvents oTimer As Timer 'I cheat: don't use this to refresh your form Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load Me.Text = "Form1 from GlobalVariable" oTimer = New Timer 'I cheat: don't use this to refresh your form With oTimer .Interval = 1000 .Start() End With End Sub Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click Module1.iOne = Module1.iOne + 1 Label1.Text = Module1.iOne End Sub Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click Class1.iTwo = Class1.iTwo + 1 Label2.Text = Class1.iTwo End Sub Public Shared Sub add_Class1() ' there is a difference between public and public shared ' public is available but limited to current project ' public shared is extended to the solution (many projects) Class1.iTwo = Class1.iTwo + 1 End Sub Public Shared Sub add_Module1() ' there is a difference between public and public shared ' public is available but limited to current project ' public shared is extended to the solution (many projects) Module1.iOne = Module1.iOne + 1 End Sub #Region "oTimer Events" Private Sub refreshMe() Handles oTimer.Tick Label1.Text = Module1.iOne 'I cheat: don't use this to refresh your form Label2.Text = Class1.iTwo 'I cheat: don't use this to refresh your form Me.Refresh() End Sub #End Region End Class |
Believe me, if you make a single project with Module1, Form1 and Class1. You will not see the difference between the two types of global variable. They will acts like Global variable and you have the right to say it.
Global Variable, say it! Global Variable. Say it again Global Variable.
If you run the program, I won�t need to explain how the code works; you will notice how what the buttons do.
The limits of a global variable.
The real thing is when you have 2 projects or more. I have project GlobalVariables and project SecondProject.
Under SecondProject, I have another Form1 (form1.vb). Here is the code:
Public Class Form1 Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load Me.Text = "Form1 from SecondProject" Button1.Text = "display Form 1 from GlobaVariable" Button2.Text = "+1 to Module1.iOne from GlobaVariable" Button3.Text = "+1 to Class1.iTwo from GlobaVariable" 'GlobalVariable.Class1.iTwo = 100 End Sub Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click Dim oForm1 As GlobalVariable.Form1 Try oForm1 = New GlobalVariable.Form1 oForm1.Show() Catch ex As Exception End Try End Sub Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click GlobalVariable.Form1.add_Module1() End Sub Private Sub Button3_Click(sender As System.Object, e As System.EventArgs) Handles Button3.Click GlobalVariable.Form1.add_Class1() End Sub End Class |
When you run the code and display the Form1 from project SecondProject, here is what you will get:
If you press Button1 or Button2 , it will add 1 to each global variable from the project GlobalVariable. If you press any of the 2 other button from Form1 of the second project you will see that the counter add 1 to each global variables.
Only when a variable (or function) is shared, it can be seen elsewhere.
Please note that the only way to access my global variable in my 1st project from my 2nd project is with shared function or shared variable.
Now that you read my post, I hope you liked it. I hope you know or remember the range of your global variables.
Global Variable, say it! Global Variable. Say it again Global Variable.
I am not here to tell you to use them or not. I haven�t written this post to debate if global variable are good or not. Neither if we could call them global or not.
I searched the web before writing this post, I haven�t seen any post like mine. Truly, many ask the question on the difference on public or global variable, or any between static of global but all the time, the information is incomplete or not good because some people are mixing C++, VB6 and newer language like C# and VB.NET.
If you liked this post or have a comment, write something or help me completed it. Share your impression. (I will moderate it anyway).
If you like this post, leave a comment or share it.
Download the sample project: GlobalVariable.zip
Use the same Visual Studio than mine: Visual Studio 2010 Professional
Get the latest version available : Visual Studio Professional with MSDN 2012
Get the latest version available : Visual Studio Professional with MSDN 2012
Visit my web site : Check Technologies Official Web Site
Reference: other post I found weak, incomplete or inspired me:
C/C++ related:
.NET related:
Update Contact :
No Wa/Telepon (puat) : 085267792168
No Wa/Telepon (fajar) : 085369237896
Email : Fajarudinsidik@gmail.com
No Wa/Telepon (puat) : 085267792168
No Wa/Telepon (fajar) : 085369237896
Email: Fajarudinsidik@gmail.com
atau Kirimkan Private messanger melalui email dengan klik tombol order dibawah ini :