VB.Net program to create a constructor within the structure

Here, we are going to learn how to create a constructor within the structure in VB.Net?
Submitted by Nidhi, on December 20, 2020

Here, we will create a parameterized constructor within the structure and create a function to print student information on the console screen.

Program/Source Code:

The source code to create a constructor within the structure is given below. The given program is compiled and executed successfully.

'VB.Net program to create a constructor within the structure.

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

        Sub New(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 stu1 As New Student(101, "Rohit", 5000)
        Dim stu2 As New Student(102, "Virat", 7000)

        Console.WriteLine("Student1: ")
        stu1.PrintStudent()

        Console.WriteLine(vbCrLf & "Student2: ")
        stu2.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 a parameterized constructor using the New keyword and we also created a PrintStudent() function to print Student Information.

In the Main() function, we created two objects of Student objects that are initialized using the parameterized constructor. After that, we printed the Student information on the console screen.






Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.