VB.Net program to create a simple class and object

Here, we are going to learn how to create a simple class and object in VB.Net?
Submitted by Nidhi, on December 21, 2020 [Last updated : March 06, 2023]

How to create a class in VB.Net?

Here, we will class that contains data members and member functions. Then create the object of the class and access members of the class using the object.

Program/Source Code:

The source code to create a simple class and object is given below. The given program is compiled and executed successfully.

VB.Net code to create a simple class and object

'VB.Net program to create a simple class and object.

Class Sample
    Private num1 As Integer
    Private num2 As Integer

    Public Sub SetValues(ByVal n1 As Integer, ByVal n2 As Integer)
        num1 = n1
        num2 = n2
    End Sub

    Public Sub PrintValues()
        Console.WriteLine("Num1: " & num1)
        Console.WriteLine("Num2: " & num2)
    End Sub
End Class

Module Module1
    Sub Main()
        Dim obj As New Sample()

        obj.SetValues(10, 20)
        obj.PrintValues()
    End Sub
End Module

Output:

Num1: 10
Num2: 20
Press any key to continue . . .

Explanation:

In the above program, we created a module Module1. Here, we created a class Sample that contains two data members num1 and num2. Here, we also created two member functions to SetValues() and PrintValues().

The SetValues() member function is used to set the values to data members. The PrintValues() member function is used to print the values of data members on the console screen.

VB.Net Basic Programs »






Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.