|
|
 |
|
|
|
|
|
|
|
|
Visual Basic 2008 9.0 .NET Examples and Ebook
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 Example1
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 Example2
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 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|