VB.Net program to pass a structure into function

Here, we are going to learn how to pass a structure into function in VB.Net?
Submitted by Nidhi, on December 13, 2020 [Last updated : March 07, 2023]

Pass a structure to VB.Net function

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

Program/Source Code:

The source code to pass a structure into a function is given below. The given program is compiled and executed successfully.

VB.Net code to demonstrate the example of passing a structure to the function

'VB.Net program to pass a structure into function.

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

    Sub PrintStructure(ByVal str As MyStruct)
        Console.WriteLine("Num1 : {0}", str.num1)
        Console.WriteLine("Num2 : {0}", str.num2)
    End Sub

    Sub Main()
        Dim str As New MyStruct

        str.num1 = 10
        str.num2 = 20

        PrintStructure(str)
    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 contains a structure and a user define sub-routine PrintStructure().

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

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

VB.Net User-defined Functions Programs »





Comments and Discussions!

Load comments ↻





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