VB.Net Program with Code Examples

โšก Smart Summary

VB.Net program structure organizes code into namespaces, modules, classes, and a Main procedure that runs first. Understanding these building blocks, along with structures for grouping data, lets beginners write clear, well-organized console and Windows Forms applications in Visual Studio.

  • ๐Ÿ”˜ Namespaces: The Imports statement pulls a namespace such as System into scope so its methods are callable directly.
  • โ˜‘๏ธ Modules and classes: Every program needs a module or class to hold its data, procedures, and executable logic.
  • โœ… Main procedure: Main marks the entry point where execution begins in each VB.Net application.
  • ๐Ÿงช Structures: A Structure packages several related values of different types into one user-defined value type.
  • ๐Ÿ› ๏ธ Project types: Visual Studio builds both Console and Windows Forms applications for VB.Net.
  • ๐Ÿค– AI assistance: Copilot and IntelliCode in Visual Studio 2026 scaffold VB.Net modules and classes from plain-English prompts.

VB.Net Program Structure

Modules in VB.Net

A VB.Net program consists of the following modules:

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

Hello World Program Example in VB.Net

Below is a simple Hello World program example in VB.Net:

Step 1) Create a new console application.

Step 2) Add the following code:

Imports System
Module Module1

    'Prints Hello Guru99 
    Sub Main()

        Console.WriteLine("Hello Guru99")
        Console.ReadKey()

    End Sub
End Module

Step 3) Click the Start button from the toolbar to run it. It should print the following on the console:

Hello World program

Let us discuss the various parts of the above program:

Hello World program

Explanation of Code:

  1. This is called the namespace declaration. What we are doing is that we are including a namespace with the name System into our programming structure. After that, we will be able to access all the methods that have been defined in that namespace without getting an error.
  2. This is called a module declaration. Here, we have declared a module named Module1. VB.Net is an object-oriented language. Hence we must have a class module in every program. It is inside this module that you will be able to define the data and methods to be used by your program.
  3. This is a comment. To mark it as a comment, we added a single quote (‘) to the beginning of the sentence. The VB.Net compiler will not process this part. The purpose of comments is to improve the readability of the code. Use them to explain the meaning of various statements in your code. Anyone reading through your code will find it easy to understand.
  4. A VB.Net module or class can have more than one procedure. It is inside procedures where you should define your executable code. This means that the procedure will define the class behavior. A procedure can be a Function, Sub, Get, Set, AddHandler, Operator, RemoveHandler, or RaiseEvent. In this line, we defined the Main sub-procedure. This marks the entry point in all VB.Net programs. It defines what the module will do when it is executed.
  5. This is where we have specified the behavior of the primary method. The WriteLine method belongs to the Console class, and it is defined inside the System namespace. Remember this was imported into the code. This statement makes the program print the text Hello Guru99 on the console when executed.
  6. This line will prevent the screen from closing or exiting soon after the program has been executed. The screen will pause and wait for the user to perform an action to close it.
  7. Closing the main sub-procedure.
  8. Ending the module.

With the Hello World program in place, the next core building block of a VB.Net program is the class.

Class in VB.Net

In VB.Net, we use classes to define a blueprint for a Data Type. It does not mean that a class definition is a data definition, but it describes what an object of that class will be made of and the operations that we can perform on such an object.

An object is an instance of a class. The class members are the methods and variables defined within the class.

To define a class, we use the Class keyword, which should be followed by the name of the class, the class body, and the End Class statement. This is described in the following syntax:

[ <attributelist> ] [ accessmodifier ] _
Class name 
   [ Inherits classname ]
   [ statements ]
End Class

Here,

  • The attributeList denotes a list of attributes that are to be applied to the class.
  • The accessModifier is the access level of the defined class. It is an optional parameter and can take values like Public, Protected, Protected Friend, Friend, and Private.
  • The Inherits denotes any parent class that it inherits.

VB.Net Class Example

Following is an example code to create a class in VB.Net:

Step 1) Create a new console application.

Step 2) Add the following code:

Imports System
Module Module1

    Class Figure
        Public length As Double

        Public breadth As Double
    End Class
    Sub Main()
        Dim Rectangle As Figure = New Figure()
        Dim area As Double = 0.0

        Rectangle.length = 8.0

        Rectangle.breadth = 7.0
        area = Rectangle.length * Rectangle.breadth
        Console.WriteLine("Area of Rectangle is : {0}", area)

        Console.ReadKey()
    End Sub
End Module

Step 3) Run the code by clicking the Start button from the toolbar. You should get the following window:

VB.Net Class Example

We have used the following code:

VB.Net Class Example

Explanation of Code:

  1. Creating a module named Module1.
  2. Creating a class named Figure.
  3. Creating a class member named length of type Double. Its access level has been set to public meaning that it will be accessed publicly.
  4. Creating a class member named breadth of type Double. Its access level has been set to public meaning that it will be accessed publicly.
  5. Ending the class.
  6. Creating the main sub-procedure.
  7. Creating an object named Rectangle. This object will be of type figure, meaning that it will be capable of accessing all the members defined inside the Figure class.
  8. Defining a variable named area of type Double and initializing its value to 0.0.
  9. Accessing the length property defined in the Figure class and initializing its value to 8.0.
  10. Accessing the breadth property defined in the Figure class and initialize its value to 7.0.
  11. Calculating the area of the rectangle by multiplying the values of length and breadth. The result of this calculation will be assigned to the area variable.
  12. Printing some text and the area of the rectangle on the console.
  13. Pausing the console waiting for a user to take action to close it.
  14. Ending the sub-procedure.
  15. Ending the class.

Alongside classes, VB.Net offers structures as a lightweight way to package related values together.

Structure in VB.Net

A structure is a user-defined data type. Structures provide us with a way of packaging data of different types together. A structure is declared using the structure keyword.

VB.Net Structure Example

Here is an example to create a structure in VB.Net:

Step 1) Create a new console application.

Step 2) Add the following code:

Module Module1
    Structure Struct
        Public x As Integer
        Public y As Integer
    End Structure
    Sub Main()
        Dim st As New Struct
        st.x = 10
        st.y = 20
        Dim sum As Integer = st.x + st.y
        Console.WriteLine("The result is {0}", sum)
        Console.ReadKey()

    End Sub 
End Module

Step 3) Run the code by clicking the Start button from the toolbar. You should get the following window:

Structure in VB.Net

We have used the following code:

Structure in VB.Net

Explanation of Code:

  1. Creating a module named Module1.
  2. Creating a structure named Struct.
  3. Creating a variable x of type Integer. Its access level has been set to Public to make it publicly accessible.
  4. Creating a variable y of type Integer. Its access level has been set to Public to make it publicly accessible.
  5. End of the structure.
  6. Creating the main sub-procedure.
  7. Creating an object named st of type Struct. This means that it will be capable of accessing all the properties defined within the structure named Struct.
  8. Accessing the variable x defined within the structure Struct and initializing its value to 10.
  9. Accessing the variable y defined within the structure Struct and initializing its value to 20.
  10. Defining the variable sum and initializing its value to the sum of the values of the above two variables.
  11. Printing some text and the result of the above operation on the console.
  12. Pausing the console window waiting for a user to take action to close it.
  13. End of the main sub-procedure.
  14. End of the module.

How to Create a New Project in Microsoft Visual Studio

IDE stands for Integrated Development Environment. It is where we write our code. Microsoft Visual Studio forms the most common type of IDE for VB.Net programming.

To install Visual Studio use this guide.

To write your code, you need to create a new project.

Following are the steps to create a new project in Visual Studio:

Step 1) Go to File Menu in Visual Studio

Open Visual Studio, click on the File menu, and choose New->Project from the toolbar

Console Application Project in Visual Studio

Step 2) Select Windows Forms Application

On the new window, click Visual Basic from the left vertical navigation pane, and Choose Windows Forms Application.

Windows Forms Application project in Visual Studio

Step 3) Give a name to your project

Give it a name and click the OK button. The project will be created.

You will have created a Windows Forms Application project. This type of project will allow you to create a graphical user interface by dragging and dropping elements.

How to Create a Console Application Project in Visual Studio

You may need to create an application that runs on the console. This requires you to create a Console Application project. The following steps can help you achieve this:

Step 1) Open Visual Studio, and click the File menu, Choose New then Project from the toolbar.

Console Application Project in Visual Studio

Step 2) On the new window, click Visual Basic from the left vertical navigation pane. Choose Console Application.

Console Application Project in Visual Studio

Step 3) Give it a name and click the OK button. The project will be created.

FAQs

A module has one Shared, non-inheritable instance, so it suits helper code and console entry points. A class is a blueprint you instantiate as objects and can inherit for reusable, object-oriented designs.

The Main procedure is the entry point of every VB.Net program; the .NET runtime calls it after loading the app. Inside a module Main needs no Shared keyword, but inside a class it must be Shared.

A namespace is the top-level container that groups related classes and modules and prevents naming conflicts. The Imports statement brings it into scope, so you can call members like Console.WriteLine directly.

Use a Structure for small, short-lived data because it is a value type copied on assignment. Choose a Class for larger objects needing inheritance, references, or shared state, as classes are heap-managed reference types.

VB.Net is object-oriented, so all executable code must sit inside a module or class. This container holds the variables, procedures, and Main entry point the compiler needs to organize the program.

Yes. GitHub Copilot in Visual Studio 2026 can generate complete VB.Net structures, turning a plain-English comment into a Module, Class, or Main procedure. Always review the generated code before using it.

Yes. Visual Studio 2026 uses AI through GitHub Copilot and IntelliCode to speed up VB.Net coding, offering context-aware completions that learn your patterns and surface the most relevant members first.

A Console Application runs in a text-only command window and is ideal for learning core VB.Net syntax. A Windows Forms Application adds a graphical interface built by dragging and dropping controls for desktop apps.

Summarize this post with: