Scope rules and instance variables in VB.NET

In this article I shows you how to use instance variable and what is scope in Vb.Net application.
  • 7254

Scope of a variable means that a variable is in the particular scope and you can use that variable within the the scope if the variable is out of scope you will not able to access the variable. In Vb.Net there are three types of scope.

  • Global Scope
    Global Scope means your variable have global scope in your application and you can use that variables in anywhere in your application
  • Module Scope
    Module Scope means that you can use variables antwhere within the module.
  • Local Scope
    Local Scope means you can use your declared variable only within the procedure where you declared the variable.

Instance variables are frequently used  for passing the reference values within the application like in given below example we create a shared variable name as value.

Example to demonstrate the working with instance variable with the scope

    Public Class Tester
        Shared value As Integer = 2
        Public Shared Sub Main()
            Dim value As Integer = 10
            Console.WriteLine("value in" & " FrmScoping_Load is " & value)
            Method()
            Method()
        End Sub

        Shared Sub Method()
            Dim value As Integer = 50
            Console.WriteLine("value in Method is " & value & " after entering Method")
            value += 5
            Console.WriteLine("value in Method is " & value & " before quitting from Method")
            Console.ReadLine()
        End Sub
    End
Class

OUTPUT

6.gif
 

CONCLUSION

In this article you can see how I use a shared variable in hole class and go through this code you can understand the scope of class.

Categories

More Articles

© 2020 DotNetHeaven. All rights reserved.