Dela via


Structure of a Visual Basic Program

A Visual Basic program is built up from standard building blocks. A solution comprises one or more projects. A project in turn can contain one or more assemblies. Each assembly is compiled from one or more source files. A source file provides the definition and implementation of classes, structures, modules, and interfaces, which ultimately contain all your code.

For more information about these building blocks of a Visual Basic program, see Introduction to Solutions, Projects, and Items and Assemblies.

File-Level Programming Elements

When you start a project or file and open the code editor, you see some code already in place and in the correct order. Any code that you write should follow the following sequence:

  1. Option statements

  2. Imports statements

  3. Namespace statements and namespace-level elements

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

A program can also contain conditional compilation statements. You can intersperse these in the source file among the statements of the preceding sequence.

Option Statements

Option statements establish ground rules for subsequent code, helping prevent syntax and logic errors. The Option Explicit Statement (Visual Basic) ensures that all variables are declared and spelled correctly, which reduces debugging time. The Option Strict Statement helps to minimize logic errors and data loss that can occur when you work between variables of different data types. The Option Compare Statement specifies the way strings are compared to each other, based on either their Binary or Text values.

Imports Statements

You can include an Imports Statement (.NET Namespace and Type) to import names defined outside your project. An Imports statement allows your code to refer to classes and other types defined within the imported namespace, without having to qualify them. You can use as many Imports statements as appropriate. For more information, see References and the Imports Statement.

Namespace Statements

Namespaces help you organize and classify your programming elements for ease of grouping and accessing. You use the Namespace Statement to classify the following statements within a particular namespace. For more information, see Namespaces in Visual Basic.

Conditional Compilation Statements

Conditional compilation statements can appear almost anywhere in your source file. They cause parts of your code to be included or excluded at compile time depending on certain conditions. You can also use them for debugging your application, because conditional code runs in debugging mode only. For more information, see Conditional Compilation Overview.

Namespace-Level Programming Elements

Classes, structures, and modules contain all the code in your source file. They are namespace-level elements, which can appear within a namespace or at the source file level. They hold the declarations of all other programming elements. Interfaces, which define element signatures but provide no implementation, also appear at module level. For more information on the module-level elements, see the following:

Data elements at namespace level are enumerations and delegates.

Module-Level Programming Elements

Procedures, operators, properties, and events are the only programming elements that can hold executable code (statements that perform actions at run time). They are the module-level elements of your program. For more information on the procedure-level elements, see the following:

Data elements at module level are variables, constants, enumerations, and delegates.

Procedure-Level Programming Elements

Most of the contents of procedure-level elements are executable statements, which constitute the run-time code of your program. All executable code must be in some procedure (Function, Sub, Operator, Get, Set, AddHandler, RemoveHandler, RaiseEvent). For more information, see Executable Statements.

Data elements at procedure level are limited to local variables and constants.

The Main Procedure

The Main procedure is the first code to run when your application has been loaded. Main serves as the starting point and overall control for your application. 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 Main Procedure in Visual Basic.

See Also

Concepts

Visual Basic Version of Hello, World

Main Procedure in Visual Basic

Visual Basic Naming Conventions

Visual Basic Limitations