Local global variable as Local variable without changing the name of variable in VB.NET
This article is all about my study about variables in Vb.Net you can use your global variable as locally also in your application.
All Variables are different in its scope and types on the basis of scope of the variable the variable can be describe as local either global.
The global variables can be accessed from anywhere inside the program but the local variable should be same name.
The local variables can be accessed only locally like in the class , sub or in a particular function and you cannot access these variables outside there local scope where you defined that variables.
To explain that how can you use a global variable as locally with the same name we take a example in this first we create a variable globally and than using that variable within two loops with the same name and shows the output of both loops using that single variable.
EXAMPLE
Module Module1
Dim series As Integer
Sub cities()
For series = 5 To 150
Console.Write(series & " ")
Next
End Sub
Sub countries()
Dim series As Integer
For series = 0 To 5
Console.Write(series & " ")
Next
End Sub
Sub Main()
series = 5
Console.WriteLine("Starting series: " & series)
countries()
Console.WriteLine("series after countries: " & series)
cities()
Console.WriteLine("series after cities: " & series)
If (series > 150) Then
Dim series As Integer = 0
Console.WriteLine("series in If statement: " & series)
End If
Console.WriteLine("Ending series: " & series)
Console.ReadLine()
End Sub
End Module
OUTPUT
CONCLUSION
I hope this article helps you to understand the working with variables locally and globally at the same time with same in the same module.