VB.Net program to create an object of class inside another class

Here, we are going to learn how to create an object of class inside another class in VB.Net?
Submitted by Nidhi, on December 23, 2020 [Last updated : March 06, 2023]

Creating an object of class inside another class in VB.Net

Here, we will create a Marks class and then create the object of Marks class into a Student class.

Program/Source Code:

The source code to create an object of class inside another class is given below. The given program is compiled and executed successfully.

VB.Net code to create an object of class inside another class

'VB.Net program to create an object of class inside another class.

Class Marks
    Private marks As Integer

    Sub SetMarks(ByVal m As Integer)
        marks = m
    End Sub

    Sub PrintMarks()
        Console.WriteLine(vbTab & "marks    : " & marks)
    End Sub
End Class

Class Student
    Private roll_no As Integer
    Private name As String
    Private fees As Integer
    Private address As String
    Public mark As New Marks
    Sub ReadInfo()
        Console.Write("Enter roll number: ")
        roll_no = Integer.Parse(Console.ReadLine())

        Console.Write("Enter student name: ")
        name = Console.ReadLine()

        Console.Write("Enter student fees: ")
        fees = Integer.Parse(Console.ReadLine())

        Console.Write("Enter student address: ")
        address = Console.ReadLine()

        Console.Write("Enter student marks: ")
        mark.SetMarks(Integer.Parse(Console.ReadLine()))
    End Sub

    Sub PrintInfo()
        Console.WriteLine("Student Information")
        Console.WriteLine(vbTab & "Roll Number: " & roll_no)
        Console.WriteLine(vbTab & "Roll Name  : " & name)
        Console.WriteLine(vbTab & "Fees       : " & fees)
        Console.WriteLine(vbTab & "address    : " & address)
        mark.PrintMarks()
    End Sub
End Class

Module Module1
    Sub Main()
        Dim S As New Student

        S.ReadInfo()
        S.PrintInfo()
    End Sub
End Module

Output:

Enter roll number: 101
Enter student name: Amit Gupta
Enter student fees: 12456
Enter student address: Katni
Enter student marks: 455
Student Information
        Roll Number: 101
        Roll Name  : Amit Gupta
        Fees       : 12456
        address    : Katni
        marks    : 455
Press any key to continue . . .

Explanation:

In the above program, we created a module Module1 that contains the Main() function. Here, we created a Marks class and then create the object of Marks class inside the Student class. The Student class also contains 4 data members roll_no, name, fees, address. In the Student class we created two methods ReadInfo() and PrintInfo().

The ReadInfo() method is used to read the student information from the user. The PrintInfo() method is used to print the student detail on the console screen.

The Marks class contains two Methods SetMarks() and PrintMarks(), to set and print marks values.

The Main() method is the entry point for the program, here we created the object of Student class then called ReadInfo() and PrintInfo() using the object to read and print student information.

VB.Net Basic Programs »






Comments and Discussions!

Load comments ↻






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