Visual Basic 2008 9.0 .NET Examples and Ebook
  Home

Procedures and Functions

Vorig Onderwerp

Arrays

|

Object Oriented Programming

Volgend Onderwerp

Module Variables

Vorig Onderwerp

Local Variables and Argument Variables

|

Static Variables

Volgend Onderwerp
Overlapping Scopes

Overlapping Scopes

Exercises

Exercises



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                                                     ' (1)
    Sub Main()
        Dim value1 As Integer = 5
        Dim value2 As Integer = 10
        '
        MakeSum(value1, value2)
        '
        Console.WriteLine(sum)                                             ' (2)
        '
        Console.ReadLine()
    End Sub
    Sub MakeSum(ByVal value1 As Integer, ByVal value2 As Integer)
        sum = value1 + value2                                              ' (3)
    End Sub
End Module
Download Broncode

Output :

 15

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                                                  ' (1)
    Sub Main()
        Test1()
        '
        Dim x As Integer = 2                                              ' (2)
        Console.WriteLine(x)                                              ' (3)
        '
        Test2(x)                                                          ' (4)
        '
        Console.ReadLine()
    End Sub
    Sub Test1()
        Console.WriteLine(x)                                              ' (5)
    End Sub
    Sub Test2(ByVal x As Integer)                                         ' (6)
        x += 1                                                            ' (7)
        Console.WriteLine(x)                                              ' (8)
    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).


Output :

 1
 2
 3

Klik hier om terug naar boven te gaan.  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

Solution :


Output :

 2
 4

Task :


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

Solution :


Output :

 2
 0
 0
 4
 0

Updated On : 2008-11-06

Download Broncode

Published On : 2008-11-06

Module Variables

Vorig Onderwerp

Local Variables and Argument Variables

|

Static Variables

Volgend Onderwerp

Procedures and Functions

Vorig Onderwerp

Arrays

|

Object Oriented Programming

Volgend Onderwerp
  Home  
Nederlands
Nederlands

Add to favorites (IE).


No printable version available.