VB.Net program to the array of structures

Here, we are going to demonstrate the array of structures in VB.Net.
Submitted by Nidhi, on December 20, 2020 [Last updated : March 08, 2023]

Array of structures in VB.Net

Here, we will create a Student structure that contains three data members' id, name, and fees. Here, we created the array of objects of structure and then set and print student information.

Program/Source Code:

The source code to the array of the structures is given below. The given program is compiled and executed successfully.

VB.Net code to create array of structures

'VB.Net program to the array of structure.

Module Module1
    Structure Student
        Private id As Integer
        Private name As String
        Private fee As Integer

        Sub SetStudent(ByVal i As Integer, ByVal n As String, ByVal f As Integer)
            id = i
            name = n
            fee = f
        End Sub

        Sub PrintStudent()
            Console.WriteLine("Student Id  : {0}", id)
            Console.WriteLine("Student Name: {0}", name)
            Console.WriteLine("Student Fees: {0}", fee)
        End Sub
    End Structure

    Sub Main()
        Dim stu() As Student = New Student(2) {}

        stu(0).SetStudent(101, "Rohit", 5000)
        stu(1).SetStudent(102, "Virat", 7000)

        Console.WriteLine("Student1: ")
        stu(0).PrintStudent()

        Console.WriteLine(vbCrLf & "Student2: ")
        stu(1).PrintStudent()
    End Sub
End Module

Output

Student1:
Student Id  : 101
Student Name: Rohit
Student Fees: 5000

Student2:
Student Id  : 102
Student Name: Virat
Student Fees: 7000
Press any key to continue . . .

Explanation

In the above program, we created a module Module1. Here, we created a Structure Student that contains data members' id, name, and fees. In the Student structure, we created two member functions to Set and print Student Information.

In the Main() function, we created the array of objects of Student structure and then Set the values of objects and print student information on the console screen.

VB.Net Structure Programs »






Comments and Discussions!

Load comments ↻






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