Visual Basic Language Concepts |

Structure of a Visual Basic Program

A Visual Basic program has standard building blocks.

Visual Basic code is stored in project modules. Projects are composed of files, which are compiled into applications. When you start a project and open the code editor, you see some code already in place and in the correct order. Any code that you write should follow this sequence:

Option statements

Imports statements

The Main procedure

Class, Module, and Namespace, if applicable, statements

In addition, a program may contain conditional compilation statements. These can be placed anywhere in the module. Many programmers prefer to put them at the end.

If you enter statements in a different order, compilation errors may result.

Option Statements

Option statements establish ground rules for subsequent code, helping prevent syntax and logic errors. The Option Explicit statement ensures that all variables are declared and spelled correctly, which cuts back on time spent debugging later. The Option Strict statement helps prevent logic errors and data loss that can occur when you work between variables of different types. The Option Compare statement specifies the way strings are compared to each other, either by their Binary or Text arrangement.

Imports Statements

Imports statements allow you to name classes and other types defined within the imported namespace without having to qualify them.

Main Procedure

The Main procedure is the "starting point" for your application — the first procedure that is accessed when you run your code. Main is where you would put the code that needs to be accessed first. In Main, you can determine which form is loaded first when the program is started, find out if a copy of your application is already running on the system, establish a set of variables for your application, or open a database that the application requires. There are four varieties of Main:

· Sub Main()

· Sub Main(ByVal CmdArgs() As String)

· Function Main() As Integer

· Function Main(ByVal CmdArgs() As String) As Integer

The most common variety of this procedure is Sub Main( ). For more information, see The Visual Basic Version of Hello World!.

Class, Module, and Namespace Statements

Classes and modules make up most of the code in your source file. They contain most of the code you write — mainly Sub, Function, Method and Event statements — along with variable declarations and other code needed to make your application run.

Conditional Compilation Statements

Conditional compilation statements can appear anywhere within the module. They are set to execute if certain conditions are met during run time. You can also use them for debugging your application, since conditional code runs in debugging mode only.

See Also

Visual Basic Naming Conventions | Visual Basic Limitations | Option Compare Statement | Option Explicit Statement | Option Strict Statement | Imports Statement | Class Statement | Module Statement | Conditional Compilation Overview | Anatomy of the Visual Basic Code Behind Windows Forms | Sub Statement | The Visual Basic Version of Hello World!