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

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

Return an object of structure from VB.Net function

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

Program/Source Code:

The source code to return an object of structure 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 structure from the function

'VB.Net program to return a structure 
'from a user defined function.

Module Module1
    Structure MyStruct
        Dim num1 As Integer
        Dim num2 As Integer
    End Structure

    Function GetStruct() As MyStruct
        Dim M As New MyStruct

        M.num1 = 10
        M.num2 = 20

        Return M
    End Function

    Sub Main()
        Dim str As New MyStruct

        str = GetStruct()

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

Output

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

Explanation

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

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

Here, we called the function GetStruct() that returns the object of the structure, and then printed the members of the structure on the console screen.

In the GetStruct() function, we created the object of structure 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.