How to use Class in VB.NET

A class defines a collection of encapsulated instance variables, methods, functions, optionally including implementations of those types and a constructor and destructor.
  • 3794

What is a Class

A class defines a collection of encapsulated instance variables and methods, functions, optionally including implementations of those types and a constructor and destructor.

In this article, we will see how to use Visual Studio to add and work with classes using VB.NET.

Steps

For adding a new class to a project, you have to follow this steps:

You can add class by two ways, "directly go to the project menu and select Add New Item or Go the solution explorer Right click on project and chose Add New Item option", in Your Opened Project.

1.gif

Figure 1

2.gif

Figure 2

Choose Visual Basic Class From Solution items and Click on Add button.

Untitled-3.gif

Figure 3

When you add a class to a project, Visual Basic Automatically generates the Class and End Class Statement so you can enter the code for the class between them.

4.gif

Figure 4

As for displaying simple string in a console application like ("Welcome User"), you have to write this code between the newly added class body.

Public Class test
    Sub disp()
        Console.WriteLine("welcome user")
        Console.ReadLine()

     End Sub
End Class

And write this code in existing Module body .

Imports System.Console
Module Module1
    Sub Main()
        Dim obj As New test()
        obj.disp()
        Read()

    End Sub

End Module
After writing this code, build or debug the application. The output screen looks like following.

5.gif

Figure 5

Summery

In this simple article, you can learned how to add a new class to an existing project using Visual Studio and how to call this class from a project module.

© 2020 DotNetHeaven. All rights reserved.