Variables in Visual Basic .NET
Our first program will demonstrate how to declare and use variables in Visual Basic .NET. Here’s the full source code:
In Visual Basic .NET, variables are explicitly declared and used by the compiler to check type-correctness of function calls, among other things.
Dim
declares one or more variables. You can declare multiple variables at once.
Visual Basic .NET will infer the type of initialized variables when you use the Dim
keyword without specifying a type.
Variables declared without a corresponding initialization are default-valued. For example, the default value for an Integer
is 0
.
In Visual Basic .NET, you can use the shorthand assignment operator to declare and initialize a variable in one line, similar to the :=
syntax in some other languages.
To run the program, save it as Variables.vb
and use the Visual Basic .NET compiler:
This example demonstrates basic variable declaration and usage in Visual Basic .NET, including type inference, multiple variable declaration, and default values.