VB.Net – Program Structure
A VB.Net program basically consists of the following parts:
- Namespace declaration
- A class or module
- One or more procedures
- The Main procedure
- Statements & Expressions
Let us look at a simple code that would print the words “Hello World”:
Imports System Module Module1 'This program will display Hello World Sub Main() Console.WriteLine("Hello World") Console.ReadKey() End Sub End Module
When the above code is compiled and executed, it produces the following result:
Let us look various parts of the above program:
- The first line of the program Imports System is used to include the System namespace in the program.
- The next line has a Module declaration, the module Module1. VB.Net is completely object oriented, so every program must contain a module of a class that contains the data and procedures that your program uses.
- Classes or Modules generally would contain more than one procedure. Procedures contain the executable code, or in other words, they define the behavior of the class. A procedure could be any of the following:
- The next line( ‘This program) will be ignored by the compiler and it has been put to add additional comments in the program.
- The next line defines the Main procedure, which is the entry point for all VB.Net programs. The Main procedure states what the module or class will do when executed.
- The Main procedure specifies its behavior with the statement
WriteLine is a method of the Console class defined in the System namespace. This statement causes the message “Hello, World!” to be displayed on the screen.
- The last line Console.ReadKey() is for the VS.NET Users. This will prevent the screen from running and closing quickly when the program is launched from Visual Studio .NET.