Variables, Data Types and Constants

4.1 Declaring Variables

Unassigned variables get default values: 0, False, Nothing (for reference types), #1/1/0001# for Date.

Dim age As Integer = 30
Dim name As String = "Ana"
Dim price As Decimal = 19.99D
Dim isActive As Boolean = True
Dim count = 10                    ' inferred as Integer (Option Infer On)
Dim a, b, c As Integer            ' all three are Integer
Dim x As Integer, y As String     ' different types
Static callCount As Integer       ' keeps its value between procedure calls

4.2 Data Types

-32,768 to 32,767 0 to 4.29 billion 0 to 1.8 × 10¹⁹ ~7 digits precision

28–29 significant digits (use for money) One UTF-16 character Any type (base of everything)

4.3 Literals and Type Characters

Legacy type characters: % Integer, & Long, ! Single, # Double, @ Decimal, $ String (Dim n% = 5). Avoid them in new code.

Dim i = 42                ' Integer
Dim l = 42L               ' Long
Dim s As Short = 42S
Dim ui = 42UI, ul = 42UL, us = 42US
Dim d = 3.14              ' Double
Dim f = 3.14F             ' Single
Dim m = 3.14D             ' Decimal
Dim r = 3.14R             ' Double (explicit)
Dim hex = &HFF            ' 255
Dim oct = &O17
Dim bin = &B1010_1010     ' binary literal + digit separator (VB 15)
Dim million = 1_000_000
Dim ch = "A"c             ' Char literal
Dim dt = #10/24/2025#     ' Date literal (M/d/yyyy format)
Dim dtt = #2025-10-24 14:30:00#
Dim str = "He said ""hi"""    ' escape quotes by doubling them
Dim multi = "Line 1
Line 2"                   ' multi-line string literal (VB 14)

4.4 Constants and ReadOnly

Const Pi As Double = 3.14159
Const AppName = "MyApp"
Private Const MaxUsers As Integer = 100
Public ReadOnly StartTime As Date = Date.Now    ' class-level, set at runtime (or in a constructor)

4.5 Type Conversion

CBool, CByte, CChar, CDate, CDbl, CDec, CInt, CLng, CObj, CSByte, CShort, CSng, CStr, CUInt, CULng, CUShort The named type CType(value, Type) Any type (uses conversion operators) DirectCast(obj, Type)

Reference/unboxing cast (no conversion, faster; throws if incompatible) TryCast(obj, Type)

Returns Nothing on failure (reference types only) Integer.Parse("42"), Integer.TryParse(s, result).NET Convert class x.ToString(), CStr(x)

Widening conversions (Integer → Long) are implicit. Narrowing conversions (Double → Integer) require explicit conversion when Option Strict On.

Dim s = "123"
Dim n As Integer = CInt(s)
Dim ok As Boolean = Integer.TryParse("abc", n)    ' False, n = 0
Dim d As Double = 7.6
Dim rounded = CInt(d)                             ' 8 (banker's rounding: CInt(2.5) = 2)
Dim truncated = Fix(d)                            ' 7
Dim o As Object = "text"
Dim str1 = DirectCast(o, String)
Dim maybe = TryCast(o, Exception)                 ' Nothing

4.6 Scope and Lifetime

Dim inside a block (If, For) Dim inside a procedure Private at class/module level That class/module The same assembly (project) Static (local)

Procedure scope, lifetime of the program/object