Академический Документы
Профессиональный Документы
Культура Документы
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.
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.
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
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
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.
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.