Home >>VB.Net Built-in Functions >VB.Net program to create multiple objects of the class

VB.Net program to create multiple objects of the class

Write a VB.Net program to create multiple objects of the class

Here, we create a Sample class and then create multiple objects in the Sample class, then use objects to call the class methods.

Program :

Below is the source code for the creation of multiple objects in the class. The program given is compiled and successfully executed.

'VB.Net program to create multiple objects of the class.


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 obj1 As New Sample()
        Dim obj2 As New Sample()
        Console.WriteLine("Object1 data:")
        obj1.SetValues(20, 30)
        obj1.PrintValues()
        Console.WriteLine(vbCrLf & "Object2 data:")
        obj2.SetValues(40, 50)
        obj2.PrintValues()
    End Sub
End Module
	
Output:
Object1 data:
Num1: 20
Num2: 30
Object2 data:
Num1: 40
Num2: 50
Explanation:

We created a Module1 module in the program above. Here, we created a sample class that includes two num1 and num2 integer data members. We have developed two SetValues() and PrintValues() methods here ().

We created two objects in the Main() method, Obj1 and Obj2. Then the methods are called from all objects and the data members set and print value.