VB.net Program Structure Example

Structure of a Visual Basic Program

A VB.net Program Structure is constructed using standard components. One or more projects make up a solution.

One or more assemblies can be found within a project. Each assembly is made up of several source files.

A source file contains all of your code and contains the definition and implementation of classes, structures, modules, and interfaces.

Let’s look at a bare minimum VB.net program structure before we look at the basic building elements of the VB.Net programming language, so we can use it as a reference in the next chapters.

VB.net Hello World Example

A VB.net program is made up of the following components:

  • Namespace declaration
  • A class or module
  • One or more procedures
  • Variables
  • The Main procedure
  • Statements & Expressions
  • Comments

Consider the following code, which prints the words “Hello World.”

Public Module Program
	Public Sub Main(args() As string)
		Console.WriteLine("Hello, World!")
	End Sub
End Module

You can test the above example here! ➡ VB.net Online Compiler

When the preceding code is compiled and run, the following is the result:

Hello, World!

Let us look at various parts of the above program.

  • The first line has a Module declaration, the module 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:
    • Function
    • Sub
    • Operator
    • Get
    • Set
    • AddHandler
    • RemoveHandler
    • RaiseEvent
  • 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 Console.WriteLine(“Hello, World!”) 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.

How to Compile & Execute the VB.net Program?

Before we start compiling and executing the vb.net program, make sure that you have already installed Microsoft Visual Studio on your computer.

Time needed: 5 minutes

Compile & Execute VB.net Program

  • Step 1: Open Microsoft Visual Studio

    First, open the Microsoft Visual Studio IDE.
    vb.net program open visual studio

  • Step 2: Create a new project

    Next, On the menu bar, choose FileNewProject.
    vb.net program create new project

  • Step 3: Choose the console app

    Next, select the console app and click next.
    vb.net program create new console project

  • Step 4: Name the console project

    Next, Specify a name and location for your project using the Browse button, and then choose the OK button.vb.net program name the console project

  • Step 5: The new project appears on the solution explorer

    Next, as you can see the console project appears on the solution explorer.
    console project appears on solution explorer

  • Step 6: Write code in the Code Editor

    Next, as you can see there’s already code in the code editor area.
    vb.net program writing code

  • Step 7: Click run

    Next, click the run button to execute the program.
    vb.net execute the program

  • Step 8: Output

    Last, the console will display the output of the executed program.
    vb.net program display output

Execute VB.net Program using Command Line

You can compile a VB.net program by using the command line instead of the Visual Studio IDE

1. Open Text Editor

Open a text editor and add the above-mentioned code.

2. Save the File

Save the file as helloworld.vb.

3. Open CMD

Open the command prompt tool and go to the directory where you saved the file.

4. Compile your Code

Type vbc helloworld.vb and press enter to compile your code.

5. Generate Executable File

If there are no errors in your code the command prompt will take you to the next line and would generate helloworld.exe executable file.

6. Execute Program

Next, type helloworld to execute your program.

7. Display Output

You will be able to see “Hello,World!” printed on the screen.

Summary

A VB.net program is composed of various parts. After importing a namespace into a program, it becomes possible for us to use all the methods and functions that have been defined in that module.

Every VB.net program must have a module.
The VB.net compiler ignores comments. We can have more than one procedure in a VB.net program.


Leave a Comment