Variables and Types

Visual Basic .NET Fundamentals

Welcome to the section on Variables and Types in Visual Basic .NET. Understanding how to declare and use variables, and the various data types available, is fundamental to building any application.

What are Variables?

A variable is a symbolic name given to a memory location that stores a value. This value can be changed during the execution of a program. Variables are essential for holding data that your program needs to process.

Declaring Variables

You declare a variable in Visual Basic .NET using the Dim keyword, followed by the variable name and optionally its data type.

Syntax:

Dim variableName [As dataType]

Example:

' Declaring an integer variable
Dim count As Integer

' Declaring a string variable
Dim message As String

' Declaring a variable without specifying a type (implicitly typed)
Dim price = 19.99

If you don't specify a data type, Visual Basic .NET will infer it from the value assigned. However, it's generally good practice to explicitly declare the data type for clarity and to prevent potential errors.

Data Types

Data types define the kind of data a variable can hold and the operations that can be performed on it. Choosing the correct data type can improve performance and reduce memory usage.

Common Data Types:

Data Type Description Example
Boolean Stores either True or False. Dim isActive As Boolean = True
Byte Stores an unsigned integer from 0 to 255. Dim statusFlag As Byte = 128
Char Stores a single character (Unicode). Dim initial As Char = "J"c
Date Stores date and time values. Dim appointment As Date = #12/25/2023 14:30:00#
Decimal Stores precise decimal numbers, suitable for financial calculations. Dim accountBalance As Decimal = 1234.56D
Double Stores double-precision floating-point numbers. Dim pi As Double = 3.1415926535
Integer Stores signed 32-bit integers (from -2,147,483,648 to 2,147,483,647). Dim quantity As Integer = 100
Long Stores signed 64-bit integers. Dim largeNumber As Long = 9223372036854775807L
Short Stores signed 16-bit integers (from -32,768 to 32,767). Dim smallValue As Short = 50
Single Stores single-precision floating-point numbers. Dim temperature As Single = 25.5F
String Stores a sequence of characters (text). Dim userName As String = "Alice"
Object Can store a value of any type. Use sparingly. Dim anything As Object = 10

Type Suffix Characters:

You can use type suffix characters to denote a literal's data type:

Variable Scope

The scope of a variable determines where in your code it can be accessed. Common scopes include:

Example of Local Variable Scope:

Sub MyProcedure()
    Dim localVar As Integer = 10 ' localVar is local to MyProcedure
    Console.WriteLine(localVar)
End Sub

Sub AnotherProcedure()
    ' Console.WriteLine(localVar) ' This would cause a compile-time error
End Sub

Implicit vs. Explicit Typing

Explicit Typing requires you to declare the data type of a variable:

Dim age As Integer = 30

Implicit Typing (Option Infer) allows the compiler to infer the data type from the assigned value. You must have Option Infer On at the top of your code file for this to work.

Option Infer On

' The compiler infers that 'name' is a String
Dim name = "Bob"

' The compiler infers that 'score' is an Integer
Dim score = 95

Recommendation: While implicit typing can make code shorter, explicit typing often leads to more readable and maintainable code, especially in larger projects. It also helps catch type-related errors earlier.

Constants

Constants are variables whose values cannot be changed after they are assigned. They are declared using the Const keyword.

Syntax:

Const constantName As dataType = value

Example:

Const PI As Double = 3.14159
Const MAX_USERS As Integer = 100

Important: Always use constants for values that are fixed and will not change throughout your program. This improves code readability and makes it easier to update values if needed.

Mastering variables and data types is a critical step in your journey with Visual Basic .NET. Experiment with different types and declarations to solidify your understanding.

Last Updated: October 26, 2023

Content provided by Microsoft Developer Network.