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.
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:
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 establish ground rules for subsequent code, helping prevent syntax and logic errors. Theensures that all variables are declared and spelled correctly, which reduces debugging time. The helps to minimize logic errors and data loss that can occur when you work between variables of different data types. The specifies the way strings are compared to each other, based on either their Binary or Text values.
You can include anto 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 .
Namespaces help you organize and classify your programming elements for ease of grouping and accessing. You use theto classify the following statements within a particular namespace. For more information, see .
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.
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.
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.
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.
Data elements at procedure level are limited to local variables and constants.
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(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.