Skip to content
Visual Basic

Variables and Types

Declare variables with Dim and type inference.

By EZ4Code Team
variablestypes

Code

' Explicit type
Dim age As Integer = 30
Dim name As String = "Alice"
Dim height As Double = 1.75
Dim isActive As Boolean = True

' Type inference (Option Infer On)
Dim count = 10        ' Integer
Dim message = "Hello" ' String

' Constants
Const PI As Double = 3.14159

' Nullable
Dim score As Integer? = Nothing
If score.HasValue Then
    Console.WriteLine(score.Value)
End If

' Object (late binding, Option Strict Off)
Dim obj As Object = "text"
Dim len As Integer = obj.Length  ' late-bound

Explanation

VB uses Dim for declarations. Option Infer On enables type inference (like var in C#). Option Strict On enforces type safety (recommended). Nullable types (Of T?) wrap value types to support Nothing. Use Object only for late binding with COM/interop.

More Visual Basic Snippets