Visual Basic 2008 9.0 .NET Examples and Ebook

Procedures and Functions

Vorig Onderwerp

Arrays

|

Object Oriented Programming

Volgend Onderwerp

Local, Module and Static Variables

Vorig Onderwerp

Introduction to Procedures

|

Introduction to Functions

Volgend Onderwerp
Local Variables and Argument Variables

Local Variables and Argument Variables

Module Variabelen

Module Variabelen

Overlapping Scopes

Overlapping Scopes

Exercises

Exercises

Static Variables

Static Variables

Exercise Static Variables

Exercise Static Variables

Statementblock Variables

Statementblock Variables



So far, we've only used local variables and argument variables.


Local Variables and Argument Variables


Local variables are locally declared within a routine ( procedure ). Their scope ( or range of use ) starts at the declaration, and ends at the end of the routine in which they are declared.
Outside that routine this variables can not be used.

Argument variables are declared in the argument list ( between parentheses in the signature of the routine definition ), and can be accessed between Sub and End Sub of the declaring routine.

As long as an activation record for a routine exists on the calling sequence ( as long as a routine is on the callstack ), all local and argument variables of this routine are loaded into memory.


Klik hier om terug naar boven te gaan.  Up



Module Variabelen


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.


Klik hier om terug naar boven te gaan.  Up



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

Klik hier om terug naar boven te gaan.  Up


Static Variables


Static variables are declared within a routine, and can only be accessed from within that routine.

The variables are not removed from memory when the routine finishes. The lifespan of these variables equals the lifespan of the module ( in which they are declared ). Therefore static variables keep there values between different execution-instances of a routine.


Module Example3
    Sub Main()
        Test1()
        Test1()
        Test1()
        '
        Test2()
        Test2()
        Test2()
        '
        Console.ReadLine()
    End Sub
    Sub Test1()
        Static x As Integer
        x += 1
        Console.WriteLine(x)
    End Sub
    Sub Test2()
        Static x As Integer = 10
        x += 1
        Console.WriteLine(x)
    End Sub
End Module
Download Broncode

Output :

 1
 2
 3
 11
 12
 13

An initialization ( on the declaration line of a static variable ) is not repeated when the routine ( declaring the static variable ) is executed several times.

Static variables consume more ( or consume longer ) memory than local and argument variables, therefore limit the use of this type of variables.

Static variables are often used to let routines behave differently on different occasions.


Module Example4
    Sub Main()
        Test()
        Test()
        Test()
        '
        Console.ReadLine()
    End Sub
    Sub Test()
        Static firstExecution As Boolean = True
        If firstExecution Then
            Console.WriteLine("First execution of Test.")
            firstExecution = False
        Else
            Console.WriteLine("Not the first execution of Test.")
        End If
    End Sub
End Module
Download Broncode

Output :

 First execution of Test.
 Not the first execution of Test.
 Not the first execution of Test.

Klik hier om terug naar boven te gaan.  Up


Exercise Static Variables


Task :

What will be the output of module Exercise3Task ?


Module Exercise3Task
    Sub Main()
        Test1()
        Test1()
        '
        Console.ReadLine()
    End Sub
    Sub Test1()
        Static t1 As Integer = 10
        t1 += 1
        Test2(t1)
        Test2(t1)
    End Sub
    Sub Test2(ByVal t2 As Integer)
        Static t3 As Integer = 20
        t3 += t2
        Console.WriteLine(t3)
    End Sub
End Module
Download Broncode

Solution :


Output :

 31
 42
 54
 66

Klik hier om terug naar boven te gaan.  Up


Statementblock Variables


Local variables have scopes limited to the statementblock in which they are declared.

ifVariable can only be accessed between If and Else.
elseVariable can only be accessed between Else and End If.
caseVariable can only be accessed in Case 1.
doVariable can only be accessed between Do and Loop.
forVariable1 and forVariable2 can only be accessed between For and Next.
forEachVariabele1 and forEachVariabele2 can only be accessed between For Each and Next.


Module Example5
    Sub Main()
        Dim someCondition As Boolean
        If someCondition Then
            Dim ifVariable As Integer
            '...
        Else
            Dim elseVariable As Integer
            '...
        End If
        '
        Dim someExpression As Integer
        Select Case someExpression
            Case 1
                Dim caseVariable As Integer
        End Select
        '
        Do
            Dim doVariable As Integer
            '...
        Loop
        '
        For forVariable1 As Integer = 1 To 10
            Dim forVariable2 As Integer
            '...
        Next
        '
        Dim someCollection As Integer()
        For Each forEachVariable1 As Integer In someCollection
            Dim forEachVariable2 As Integer
            '...
        Next
    End Sub
End Module
Download Broncode

Although it seem like some declaration are repeated ( declarations of _-
doVariable, forVariable2 and forEachVariable2 ), they are declared only once. These variables keep there values for next executions of the


Module Example6
    Sub Main()
        For count As Integer = 1 To 5
            Dim forVariable As Integer
            Console.WriteLine(forVariable)
            forVariable += 1
        Next
        Console.WriteLine()
        '
        For count As Integer = 1 To 5
            Dim forVariable As Integer = 1
            Console.WriteLine(forVariable)
            forVariable += 1
        Next
        '
        Console.ReadLine()
    End Sub
End Module
Download Broncode

Output :

 0
 1
 2
 3
 4

 1
 1
 1
 1
 1




This version ( published on 2008-06-24 ) is printed from http://www.studyvb.com, visit the website for more recent information.

Updated On : 2008-01-29

Download Broncode

Published On : 2008-06-24

Local, Module and Static Variables

Vorig Onderwerp

Introduction to Procedures

|

Introduction to Functions

Volgend Onderwerp

Procedures and Functions

Vorig Onderwerp

Arrays

|

Object Oriented Programming

Volgend Onderwerp
Nederlands  Nederlands

Add to favorites (IE).