Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

VB.Net - Program structure


May 13, 2021 vb.net


Table of contents


Before we VB.Net the basic building blocks of a programming language, let's look at a minimal VB.Net program structure so that we can use it as a reference for future chapters.


VB.Net hello World example

A VB.Net consists mainly of the following parts:

  • The namespace declares Namespace declaration

  • A class or module, class or module

  • One or more programs, One or more procedures

  • Variables

  • The main procedure

  • Statements and Expressions Statements and Expressions

  • Note Comments


Let's look at a simple code that prints the word "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 results:

Hello, World!

Let's take a look at the various parts of the program above:

  • The first line of the program Image System is used to include a system namespace in the program. T he first line of the program Imports System is used to include the System namespace in the program.

  • The next line has a Module declaration, module Module1. V b. N et is completely object-oriented, so each program must contain a module of a class that contains the data and procedures that your program uses. s 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 typically contain multiple procedures. P rocedures contain executable code, or in other words, they define the behavior of the class. T he process can be any of the following:

    • Feature Function

    • Sub Sub

    • Operator Operator

    • Get Get

    • Group Set

    • AddHandler

    • RemoveHandler

    • ReiseEvent

  • The next line ('this program') is ignored by the compiler and additional comments have been added to the program.

  • The next line defines the Main procedure, which is the entry point VB.Net of all programs. T he Main procedure explains what a module or class will do when executed.

  • The Main procedure uses statements to specify its behavior

    Console.WriteLine ("Hello World")

    WriteLine is a method of the Console class defined in the System namespace. This statement causes the message "Hello, World!" /b18> Appears on the screen.

  • The last line, Console.ReadKey(), is used to VS.NET users. T his will block the screen from Visual Studio . N et runs quickly and shuts down when it starts.

Compile and execute VB.Net programs:

If you use visual Studio.Net IDE, follow these steps:

  • Start Visual Studio. Start Visual Studio.

  • In the menu bar, select file, new, project. On the menu bar, choose File, New, Project.

  • Select Visual Basic from the template. Choose Visual Basic from templates

  • Select the console application. C hoose Console Application.

  • Use the browse button to specify the name and location of the item, and then select the OK button. Specify a name and location for your project using the Browse button, and then choose the OK button.

  • The new project appears in Solution Explorer. The new project appears in Solution Explorer.

  • Write code in the code editor. Write code in the Code Editor.

  • Click the Run button or the F5 key to run the project. A command prompt window with the line Hello World appears. C lick the Run button or the F5 key to run the project. A Command Prompt window appears that contains the line Hello World.


You can compile the program using the command line instead VB.Net Studio IDE:

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

  • Save the file as helloworld .vb. Save the file as helloworld.vb

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

  • Type VBC helloworld .vb, and then compile the code by carriage return. Type vbc helloworld.vb and press enter to compile your code.

  • If you don't have an error command prompt in your code, you'll be taken to the next line and you'll .exe executable file that HelloWorld can use. 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.

  • Next, enter HelloWorld to execute your program. Next, type helloworld to execute your program.

  • You'll see the word "Hello World" on the screen. You will be able to see "Hello World" printed on the screen.