' Visual Basic 2008 9.0 .NET Examples - Local, Module and Static Variables - Procedures and Functions : 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 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 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 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 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 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 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 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 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 ' Visit www.studyvb.com for more examples. Copyright 2003-2008 De Wolf.