Home >>VB.Net Built-in Functions >VB.Net program to create a global variable

VB.Net program to create a global variable

Write a VB.Net program to create a global variable

Here, we are going to use two global variables and then use them in the function Main().

Program :

Below is the source code for creating a global variable. The program given is compiled and successfully executed.

'VB.Net program to create a global variable.


Module Module1
    Dim num1 As Integer = 20
    Dim num2 As Integer = 30
    Sub Main()
        Dim num3 As Integer = 0
        num3 = num1 + num2
        Console.WriteLine("Sum: " & num3)
    End Sub
End Module
	
Output:
Sum: 50
Explanation:

We created a Module1 module in the program above. Here, we created two num1, num2 global variables. These are initialized with 20, 30. We created a local variable num3 in the Main() function, which is initialized by 0, then added num1 and num2 and assigned the sum to the variable num3. We printed the sum on the console screen after that.


No Sidebar ads