ADVERTISEMENT
ADVERTISEMENT

VB.Net program to create multiple objects of the class

Here, we are going to learn how to create multiple objects of the class in VB.Net?
Submitted by Nidhi, on December 21, 2020

Here, we will create a Sample class and then create the multiple objects of the Sample class and then call methods of the class using objects.

Program/Source Code:

The source code to create multiple objects of the class is given below. The given program is compiled and executed successfully.

'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(10, 20)
        obj1.PrintValues()

        Console.WriteLine(vbCrLf & "Object2 data:")
        obj2.SetValues(30, 40)
        obj2.PrintValues()
    End Sub
End Module

Output:

Object1 data:
Num1: 10
Num2: 20

Object2 data:
Num1: 30
Num2: 40
Press any key to continue . . .

Explanation:

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

In the Main() method, we created the two objects Obj1 and Obj2. Then called methods from both objects and set and print value of data members.

VB.Net Basic Programs »



ADVERTISEMENT




ADVERTISEMENT



Comments and Discussions!

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.