VB.NET Basics

Welcome to the fundamental concepts of Visual Basic .NET (VB.NET). This section covers the essential building blocks you need to start writing VB.NET code.

Comments

Comments are crucial for explaining your code and making it more readable. VB.NET supports single-line comments and multi-line comments.

Single-line Comments

Use an apostrophe (') to start a single-line comment. Anything after the apostrophe on that line is ignored by the compiler.

' This is a single-line comment
Dim message As String = "Hello, World!" ' Assign a value

Multi-line Comments

Use '= to start a multi-line comment block and =' to end it. Alternatively, you can use REM followed by a space.

'=
This is a multi-line comment.
It spans across several lines
and is useful for detailed explanations.
='
Console.WriteLine("This line will execute.")

Statements

A statement is a complete unit of execution in VB.NET. Statements typically end with a line break, but can be continued onto the next line using the line continuation character (_).

Dim longVariableName As Integer _
    = 100 * 5 - 20

Keywords

Keywords are reserved words that have special meaning to the VB.NET compiler. You cannot use keywords as identifiers (variable names, function names, etc.). Examples include Dim, If, Then, For, Next, Sub, End, etc.

Identifiers

Identifiers are names given to program elements such as variables, functions, classes, and modules. An identifier must start with a letter or an underscore, followed by letters, numbers, or underscores.

Valid Identifiers:

  • myVariable
  • _userName
  • counter123
  • CalculateTotal

Invalid Identifiers:

  • 1stPlace (starts with a number)
  • my-variable (contains a hyphen)
  • class (is a keyword)

Literals

Literals are fixed values directly represented in source code. The common types of literals include:

Whitespace

Whitespace (spaces, tabs, newlines) is generally ignored by the compiler, except when it separates tokens or within string literals. Proper use of whitespace improves code readability.

Example of Whitespace Use:

The following two lines of code are functionally identical:

Dim x As Integer=5
Dim x As Integer = 5

The second version is much easier to read.

Understanding these basic elements is the first step to mastering VB.NET. In the next sections, we will delve into variables, operators, and control flow.

Previous: Installation Next: Variables & Data Types