< Documentation Home < Languages < VB.NET

VB.NET Variables

Variables are fundamental to any programming language, and Visual Basic .NET (VB.NET) is no exception. A variable is a symbolic name given to a location in memory that stores a value. This value can be changed during the execution of a program.

Declaring Variables

Before you can use a variable, you must declare it. Variable declaration involves specifying the variable's name and its data type. The data type tells the compiler how much memory to allocate for the variable and what kind of data it can hold. The general syntax for declaring a variable is:

Dim variableName As DataType

Common Data Types

Data Type Description Example Size
Boolean True or False values. 1 byte
Byte Unsigned integer from 0 to 255. 1 byte
Char Single character. 2 bytes
Date Date and time values. 8 bytes
Decimal Large numbers with decimal points. 16 bytes
Double Floating-point numbers with large range and precision. 8 bytes
Integer Signed whole numbers. 4 bytes
Long Signed whole numbers with a larger range than Integer. 8 bytes
Short Signed whole numbers with a smaller range than Integer. 2 bytes
Single Floating-point numbers with smaller range and precision than Double. 4 bytes
String Sequence of characters. Variable (dependent on string length)
Object Can hold any type of data. 4 bytes plus overhead

Example Declarations

Dim age As Integer
Dim userName As String
Dim isActive As Boolean
Dim price As Decimal
Dim today As Date

Initializing Variables

You can assign a value to a variable when you declare it. This is called initialization. If you don't initialize a variable, it will have a default value based on its data type (e.g., 0 for numeric types, False for Boolean, Nothing for objects).

Dim count As Integer = 0
Dim message As String = "Hello, World!"
Dim isComplete As Boolean = True
Dim pi As Double = 3.14159

You can also declare and initialize multiple variables of the same type on a single line:

Dim x As Integer = 10, y As Integer = 20, z As Integer = 30

Assigning Values to Variables

After a variable has been declared, you can assign or reassign values to it using the assignment operator (=).

Dim score As Integer
score = 100  ' Assigning a value
score = score + 10 ' Reassigning with an updated value
Console.WriteLine("Current score: " & score) ' Output: Current score: 110
Note: VB.NET is strongly typed. This means that you cannot directly assign a value of one data type to a variable of an incompatible data type without explicit conversion. The compiler will often flag such assignments as errors.

Variable Scope

Variable scope refers to the region of the program where a variable is accessible. In VB.NET, variables can have different scopes:

Implicit and Explicit Conversions

VB.NET supports both implicit (widening) and explicit (narrowing) data type conversions.

Important: Implicit conversions are generally safe as they do not result in data loss. Explicit conversions, on the other hand, can lead to data loss or exceptions if not handled carefully.

Implicit Conversion Example:

Dim myInteger As Integer = 100
Dim myLong As Long = myInteger ' Implicitly converts Integer to Long

Explicit Conversion Example:

Dim myDouble As Double = 123.45
Dim myInteger As Integer
' Explicitly convert Double to Integer (data loss occurs here)
myInteger = CInt(myDouble)
Console.WriteLine(myInteger) ' Output: 123

Common conversion functions include CInt(), CLng(), CDbl(), CStr(), etc.