Вы находитесь на странице: 1из 2

Global and Local Variables

Global Variables
A Global variable is a variable that is visible through the whole program and hose value may be
modified by any procedure in the program. So in simpler terms it is a variable that can assessed
throughout a program instead of being restricted to a function.

Local Variables
A Local variable is used and visible only within the body of a procedure or function, and which retains
its value only for the duration of each call to that procedure. Within different procedures, different local
variables may be declared with the same name, which eases the task of finding unique names, and
avoids a whole class of errors in which a global variable is inadvertently altered by the wrong
procedure.

The Form Note Saver

Throughout my program I declared number of different variables some local and some global. Below
are samples of code from my program that I created which shows sections that have local variables.

Public Class FrmNoteSaver


Dim TheDate As DateTime
Dim TheTime As DateTime
Dim theResponse As String

Variables declared within a separate module like this are limited to use within that
module. When a variable is declared within a class this limits the overall use you
have over that variable so you are restricted to most actions. This is what makes this
particular section local. Below is another example within the same program which
shows a different way of making a variable local.
Private Sub CmdSave1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles CmdSave1.Click

Dim SaveFile1 As New SaveFileDialog


SaveFile1.DefaultExt = "*.doc"
SaveFile1.Filter = "Btec Files| *.doc"

If (SaveFile1.ShowDialog = System.Windows.Forms.DialogResult.OK)
And (SaveFile1.FileName.Length) > 0 Then
Select Case DirectCast(sender, Button).Name

Case CmdSave1.Name
RTBNote1.SaveFile(SaveFile1.FileName,
RichTextBoxStreamType.PlainText)

End Select

End If

End Sub
The code above shows my declaring a variable local as it is still limited to one section you
cant use it anywhere throughout the program. So there are restrictions as to what you can
do with this variable. This is known as procedural variable

The UCAS Calculator


Public Class BND
Dim PassUnitPoints As Integer
Dim MeritUnitPoints As Integer
Dim DistinctionUnitPoints As Integer
Dim GradeBoundaryPoints As Integer
Dim UCASPass As Integer
Dim UCASMerit As Integer
Dim UCASDistinction As Integer
Dim UnitCounter As Integer

Above is another example of a local module variable however this was created in my
second program . As you can see all variables are declared so that they can be used with
the program. It is again limited to a particualr section which means there are restrictions
and regulations as to what you can do with the coding.

Scope of variables

When discussing the scope of a variable it is referring to the visibilty of the variable, so whereabouts
in the program it can be located and then used.

Dim SaveFile1 As New SaveFileDialog


SaveFile1.DefaultExt = "*.doc"
SaveFile1.Filter = "Btec Files| *.doc"
As you can see from the sample coding above a variable can only be used inside that event handler.
It does not exist outside of it which makes this a gloabl variable.

However a varibale declared inside the form class can be used in any of the event handler so it has a
wider scope. Mark variables such as public and private are only accessible from only within
(private) their class or from outside (public). These can be referred to as access modifiers.

Modules
Public Class BNC
Dim PassUnitPoints As Integer
Dim MeritUnitPoints As Integer

Variables delclared within a seperate module are limited to use within that module. The coding
above shows this as theese two integers will only be used for this particular module which means it
will be used for this particular program once created.

Вам также может понравиться