VB.Net Program with Code Examples: Module, Class & 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 procedures. 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.

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 Exmaple

We have used the following code:

VB.Net Class Exmaple

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.

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.

Console 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 Form 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.

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.