VB.Net Tutorial/Language Basics/Variable Scope

Материал из VB Эксперт
Перейти к: навигация, поиск

Block scope

Option Strict On
Public Class BlockScope
   Public Shared Sub Main()
   
      For outerLoop As Integer = 0 to 10000
         For innerLoop As Integer = 0 to 10
            Dim blockVar As Integer
            blockVar += 1
            If blockVar Mod 1000 = 0 Then 
                Console.WriteLine(blockVar)
            End If
         Next
      Next
   
   End Sub
End Class
1000
2000
3000
4000
5000
6000
7000
8000
9000
10000
11000
12000
13000
14000
15000
16000
17000
18000
19000
20000
21000
22000
23000
24000
25000
26000
27000
28000
29000
30000
31000
32000
33000
34000
35000
36000
37000
...

Define variable inside If statement

public class Test
   public Shared Sub Main
        Dim manager As Boolean = True
        If manager Then Dim txt As String = "M" : Console.WriteLine(txt) Else _
            Dim txt As String = "E" : Console.WriteLine(txt)
   End Sub
End class
M

Demonstrates scope rules and instance variables

Public Class Tester
   " instance variable can be used anywhere in class
   Dim Shared value As Integer = 1
   " demonstrates class scope and block scope
   Public Shared Sub Main
      Dim value As Integer = 5
      Console.WriteLine("local variable value in" & _
         " FrmScoping_Load is " & value )
      MethodA() " MethodA has automatic local value
      MethodB() " MethodB uses instance variable value
      MethodA() " MethodA creates new automatic local value
      MethodB() " instance variable value retains its value
      Console.WriteLine("local variable " & _
         "value in FrmScoping_Load is " & value )
   End Sub 
   " automatic local variable value hides instance variable
   Shared Sub  MethodA()
      Dim value As Integer = 25 " initialized after each call
      Console.WriteLine("local variable " & _
         "value in MethodA is " & value & " after entering MethodA" )
      value += 1
      Console.WriteLine("local variable " & _
         "value in MethodA is " & value & " before exiting MethodA" )
   End Sub 
   " uses instance variable value
   Shared Sub  MethodB()
      Console.WriteLine("instance variable" & _
         " value is " & value & " after entering MethodB" )
      value *= 10
      Console.WriteLine("instance variable " & _
         "value is " & value & " before exiting MethodB" )
   End Sub
End Class
local variable value in FrmScoping_Load is 5
local variable value in MethodA is 25 after entering MethodA
local variable value in MethodA is 26 before exiting MethodA
instance variable value is 1 after entering MethodB
instance variable value is 10 before exiting MethodB
local variable value in MethodA is 25 after entering MethodA
local variable value in MethodA is 26 before exiting MethodA
instance variable value is 10 after entering MethodB
instance variable value is 100 before exiting MethodB
local variable value in FrmScoping_Load is 5

Function local variables

Module Module1
    Sub F()
        Dim Name As String = "www.vbex.ru"
        Dim Price As Double = 17.45
        Dim I As Integer = 1001
        Console.WriteLine("In F")
        Console.WriteLine("Name: " & Name)
        Console.WriteLine("Price: " & Price)
        Console.WriteLine("I: " & I)
    End Sub
    Sub FF()
        Dim Name As String = "string"
        Dim Price As Double = 49.99
        Dim I As Integer = 0
        Console.WriteLine("In FF")
        Console.WriteLine("Name: " & Name)
        Console.WriteLine("Price: " & Price)
        Console.WriteLine("I: " & I)
    End Sub
    Sub Main()
        F()
        Console.WriteLine()
        FF()
    End Sub
End Module
In F
Name: www.vbex.ru
Price: 17.45
I: 1001
In FF
Name: string
Price: 49.99
I: 0

Local variable shadows global variable with the same name

Module Module1
    Dim Counter As Integer
    Sub BigLoop()
        For Counter = 1000 To 1005      " Use global Counter
            Console.Write(Counter & " ")
        Next
    End Sub
    Sub LittleLoop()
        Dim Counter As Integer
        For Counter = 0 To 5      " Use local Counter
            Console.Write(Counter & " ")
        Next
    End Sub
    Sub Main()
        Counter = 100
        Console.WriteLine("Starting Counter: " & Counter)
        BigLoop()
        Console.WriteLine("Counter after BigLoop: " & Counter)
        LittleLoop()
        Console.WriteLine("Counter after LittleLoop: " & Counter)
        If (Counter > 1000) Then
            Dim Counter As Integer = 0
            Console.WriteLine("Counter in If statement: " & Counter)
        End If
        Console.WriteLine("Ending Counter: " & Counter)
    End Sub
End Module
Starting Counter: 100
1000 1001 1002 1003 1004 1005 Counter after BigLoop: 1006
0 1 2 3 4 5 Counter after LittleLoop: 1006
Counter in If statement: 0
Ending Counter: 1006

Module global variable

Module Module1
    Sub Main()
        For intLoopIndex As Integer = 0 To 5
            System.Console.WriteLine(Tracker())
        Next intLoopIndex
    End Sub
    Dim intCount As Integer
    Function Tracker() As Integer
        intCount += 1
        Return intCount
    End Function
End Module
1
2
3
4
5
6

Sub scope

public class Test
   public Shared Sub Main
        DisplayHowardsName()
        DisplayStephsName()
   End Sub
   Shared Sub DisplayStephsName()
        Dim myName As String
        myName = "A"
        Console.WriteLine(myName)
    End Sub
   Shared Sub DisplayHowardsName()
        Dim myName As String
        myName = "B"
        Console.WriteLine(myName)
    End Sub
  
End class
B
A

Variable block scope

public class Test
   public Shared Sub Main
        For i As Integer = 1 To 5
            Dim j As Integer = 3
            If i = j Then
                Dim M As Integer = i + j
                Console.WriteLine("M: " & M)
            Else
                Dim N As Integer = i * j
                Console.WriteLine("N: " & N)
            End If
            Dim k As Integer = 123
            Console.WriteLine("k: " & k)
        Next i
   End Sub
End class
N: 3
k: 123
N: 6
k: 123
M: 6
k: 123
N: 12
k: 123
N: 15
k: 123

Variable scope in try catch statement

public class Test
   public Shared Sub Main

        Try
            Dim i As Integer = CInt("bad value")
        Catch ex As InvalidCastException
            Dim txt As String = "InvalidCastException"
            Console.WriteLine(txt)
        Catch ex As Exception
            Dim txt As String = "Exception"
            Console.WriteLine(txt)
        End Try
   End Sub
End class
InvalidCastException