ARTICLE

How to use Local global variable as Local variable without changing the name of variable in Vb.Net

Posted by Manish Tewatia Articles | Visual Basic Language July 26, 2010
This article is all about my study about variables in Vb.Net you can use your global variable as locally also in your application.
 
Reader Level:

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

1.gif

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.

Login to add your contents and source code to this article
share this article :
post comment
 
Team Foundation Server Hosting
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor