VB.NET Syntax Overview

This section provides a comprehensive overview of the syntax rules and conventions used in Visual Basic .NET (VB.NET). Understanding these fundamentals is crucial for writing efficient and maintainable VB.NET code.

Basic Structure

A VB.NET program is typically composed of modules, classes, structures, interfaces, and other constructs. Statements are the building blocks of these constructs.

Statements in VB.NET are generally executed sequentially, one after another. However, control flow statements can alter this order.

Statements

Each statement in VB.NET usually occupies a single line. You can continue a statement onto the next line using the line continuation character (underscore `_`) preceded by a space.

Dim message As String = "This is a very long string" & _
                                    " that has been split across" & _
                                    " multiple lines for readability."

Comments

Comments are used to explain code and are ignored by the compiler. VB.NET supports single-line and multi-line comments.

' This is a single-line comment.
            Dim counter As Integer = 0

            '=<![CDATA[
            This is a multi-line comment.
            It can span across several lines
            to provide detailed explanations.
            ]]

Identifiers

Identifiers are names given to programming elements such as variables, procedures, classes, and constants. They must follow these rules:

Valid Identifiers:

MyVariable, _userCount, CalculateTotal, Max_Value_1

Invalid Identifiers:

1stValue (starts with a digit), For (reserved keyword), My-Variable (contains a hyphen)

Keywords

Keywords are predefined words that have special meaning to the compiler. They cannot be used as identifiers.

Public, Private, Dim, If, Else, While, For, Class, Sub, Function, End, New, Try, Catch, Finally

Literals

Literals are fixed values represented directly in the source code.

Operators

Operators perform operations on operands. Common operators include arithmetic, comparison, logical, assignment, and bitwise operators.

+, -, *, /, ^, =, >, <, And, Or, Not, =

Control Structures

VB.NET provides various control structures to manage the flow of execution.

Case Sensitivity

VB.NET is generally case-insensitive for identifiers and keywords. For example, myVariable, MyVariable, and MYVARIABLE all refer to the same variable. However, string literals are case-sensitive.

Whitespace

Whitespace characters (spaces, tabs, newlines) are generally ignored by the compiler, except when they separate elements or are part of string literals.

Example:

The following code snippet demonstrates various syntax elements:

' Declare a variable
                Dim userName As String = "Alice"
                Dim userAge As Integer = 30

                ' Check a condition
                If userAge >= 18 Then
                    Console.WriteLine($"Hello, {userName}! You are an adult.")
                Else
                    Console.WriteLine($"Hello, {userName}! You are a minor.")
                End If

                ' Loop through numbers
                For i As Integer = 1 To 5
                    Console.WriteLine($"Number: {i}")
                Next