VB.Net program to return an object of the class from the user-defined function

Here, we are going to learn how to return an object of the class from the user-defined function in VB.Net?
Submitted by Nidhi, on December 14, 2020 [Last updated : March 07, 2023]

Return an object of the class from from VB.Net function

Here, we will create a user-defined function that will return an object of the class to the calling function.

Program/Source Code:

The source code to return an object of the class from the user-defined function is given below. The given program is compiled and executed successfully.

VB.Net code to demonstrate the example of returning an object of the class from the function

'VB.Net program to return an object of the class 
'from a user defined function.

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


    Function GetObject() As Cls
        Dim M As New Cls

        M.num1 = 10
        M.num2 = 20

        Return M
    End Function

    Sub Main()
        Dim str As New Cls

        str = GetObject()

        Console.WriteLine("Member of class are:")
        Console.WriteLine("Num1: {0}", str.num1)
        Console.WriteLine("Num2: {0}", str.num2)
    End Sub

End Module

Output

Member of class are:
Num1: 10
Num2: 20
Press any key to continue . . .

Explanation

In the above program, we created a module Module1 that contain a user-defined function GetClass() and Main() subroutine.

The Main() subroutine is the entry point for the program. Here we created a reference of class.

Here we called the function GetObject() that returns an object of class Cls, and then printed the members of the class on the console screen.

In the GetObject() function, we created the object of the class and assigned the values to the members, and return the object to the calling function.

VB.Net User-defined Functions Programs »





Comments and Discussions!

Load comments ↻





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