|
|
 |
|
|
|
|
|
|
|
|
Visual Basic 2008 9.0 .NET Examples and Ebook
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| Variables can be declared between Module and End Module ( but outside any routine ), these are called ( here ) module variables.
These variables can be accessed from within any routine contained by that module. |
| Module Example1
Dim sum As Integer
Sub Main()
Dim value1 As Integer = 5
Dim value2 As Integer = 10
MakeSum(value1, value2)
Console.WriteLine(sum)
Console.ReadLine()
End Sub
Sub MakeSum(ByVal value1 As Integer, ByVal value2 As Integer)
sum = value1 + value2
End Sub
End Module Download Broncode |
| Variable sum declared on module level, can be accessed from the Main and MakeSum procedures.
The lifespan of these variables equals the lifespan of the declaring module. They consume more memory ( or consume the memory longer ) than local or argument variables.
Less assignments need to be made with this type of variable, assignment that would occur when values are passed on from one routine to ( a argument of ) another routine.
Still it is better to avoid the use of this variables ( more or longer memory consumption ) unless it is really necessary or useful. For instance in situations where ( almost ) all routines need access to this variables. them. |
Overlapping Scopes
| Next examples uses three different x variables. Procedure Main declares a local variable x (2), accessible within Main. Procedure Test2 declares a argument variable x (6), accessible within Test2. Module Example2 declares a module variable x(1), accessible everywhere within the module.
The scope of the module variable x overlaps the scope of the other two variables x.
When x is addressed in a routine, there first will be searched for a local x ( local or argument variable ), before there is searched on a higher level. |
| Module Example2
Dim x As Integer = 1
Sub Main()
Test1()
Dim x As Integer = 2
Console.WriteLine(x)
Test2(x)
Console.ReadLine()
End Sub
Sub Test1()
Console.WriteLine(x)
End Sub
Sub Test2(ByVal x As Integer)
x += 1
Console.WriteLine(x)
End Sub
End Module Download Broncode |
| Line (3) and (4) use the local variable x, declared on line (2).
Line (7) and (8) use the argument variable x, declared on line (6).
Line (5) use the module variable x, declared on line (1). |
Up
Exercises
| Task :
What will be the output of module Exercise1Task ? |
| Module Exercise1Task
Dim test As Integer
Sub Main()
Test1()
Test2()
Console.ReadLine()
End Sub
Sub Test1()
Dim test As Integer = 1
Test3(test)
End Sub
Sub Test2()
test = 2
Test3(test)
End Sub
Sub Test3(ByVal test As Integer)
test += test
Console.WriteLine(test)
End Sub
End Module Download Broncode |
| What will be the output of module Exercise2Task ? |
| Module Exercise2Task
Dim a As Integer = 1
Sub Main()
Dim a As Integer
Dim b As Integer
Dim c As Integer
Dim d As Integer
Dim e As Integer
a += 2
Test(a, b + e, c + 1, d)
Console.WriteLine(a)
Console.WriteLine(b)
Console.WriteLine(c)
Console.WriteLine(d)
Console.WriteLine(e)
Console.ReadLine()
End Sub
Sub Test(ByVal b As Integer, ByRef c As Integer, _
ByVal d As Integer, ByRef e As Integer)
b += a
c += b
d += c
e += d
End Sub
End Module Download Broncode |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|