×

VB.Net Programs

VB.Net program to pass an object of class into function

By Nidhi Last Updated : November 15, 2024

Pass an object to VB.Net function

Here, we will create a class that contains two members and then create a user define a function that will accept an object of the class as an argument. After we will print the value of class members on the console screen.

Program/Source Code:

The source code to pass an object of class into the function is given below. The given program is compiled and executed successfully.

VB.Net code to demonstrate the example of passing an object to the function

'VB.Net program to pass an object of class into function.

Module Module1
    Class Cls
        Public num1 As Integer
        Public num2 As Integer
    End Class

    Sub Print(ByVal obj As Cls)
        Console.WriteLine("Num1 : {0}", obj.num1)
        Console.WriteLine("Num2 : {0}", obj.num2)
    End Sub

    Sub Main()
        Dim obj As New Cls

        obj.num1 = 10
        obj.num2 = 20

        Print(obj)
    End Sub
    
End Module

Output

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

Explanation

In the above program, we created a module Module1 that contain a structure and a user define sub-routine Print().

The Main() subroutine is the entry point for the program. Here, we created the object of a class Cls and assigned the values to members of the class.

The Print() sub-routine accepts the object of class as an argument. Here we printed the values of members on the console screen.

VB.Net User-defined Functions Programs »


Comments and Discussions!

Load comments ↻





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