Program Structure and Compiler Options

3.1 Structure of a VB.NET File

Entry point: Sub Main(), Sub Main(args As String()), or Function Main(...) As Integer (exit code). WinForms can use the VB Application Framework (startup form).

VB has no top-level statements, and Async Sub Main isn't supported. Bridge with MainAsync().GetAwaiter().GetResult().

Option Strict On                      ' 1. Options (optional per file)
Imports System.Text                   ' 2. Imports
Imports Shop.Core.Models

Namespace Shop.Cli                    ' 3. Namespace (optional; root namespace is added automatically)
    Module Program                    ' 4. Types: Module / Class / Structure / Interface / Enum / Delegate
        Sub Main(args As String())
            Console.WriteLine("Hello, .NET")
        End Sub
    End Module
End Namespace

3.2 Lexical Rules

Case-insensitive identifiers and keywords. Statements end at the newline. Implicit line continuation after ,, (, operators, From, Where, etc. The explicit continuation is _.

Comments: ' and REM. XML doc comments use '''. Escaped identifiers use [Class], [Enum], [Error].

Type characters (%, &, @, !, #, $) and literal suffixes (L, D, F, R, S, UI, UL, US, c).

3.3 Compiler Options Explained

What it enforces Option Explicit On All variables must be declared Option Strict On

No implicit narrowing conversions, no late binding, no implicit Object typing Option Infer On

Local type inference (Dim x = 5 → Integer) Option Compare Binary

Ordinal, case-sensitive = string comparisons (Text = culture/case-insensitive)

Option Strict On
Dim d As Double = 3.7
' Dim i As Integer = d          ' compile error under Option Strict On (narrowing)
Dim i As Integer = CInt(d)      ' explicit: 4

3.4 Conditional Compilation

Predefined constants: DEBUG, TRACE, CONFIG, TARGET, PLATFORM, VBC_VER, and NET8_0_OR_GREATER, WINDOWS, etc. (from the SDK).

#Const TRACE_ENABLED = True
#If DEBUG Then
    Console.WriteLine("Debug build")
#ElseIf TRACE_ENABLED Then
    Console.WriteLine("Tracing")
#End If
#Region "Helpers"
' collapsible region
#End Region
#Disable Warning BC42024
Dim unused As Integer
#Enable Warning BC42024