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.

In order for you to test your VB.net Code provided in this lesson, you can use our VB.net Online Compiler.

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.”

Learn Excel VBA Macro - Quick Lesso...
Learn Excel VBA Macro - Quick Lesson - 1 Minute

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

Hello, World!

Let us look 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 VB.net Program?

Before we start compiling and execute vb.net program, makes sure that you have already installed Microsoft Visual Studio in 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 new project

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

  • Step 3: Choose console app

    Next, select 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 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 procedures in a VB.net program.


Leave a Comment