Home >>VB.Net Built-in Functions >VB.Net program to create an object of class inside another class
Here, we are going to create a Marks class and then create the Marks class object into a Student class.
Program :Below is the source code for creating a class object within another class. The program given is compiled and successfully executed.
'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
We created a Module1 module in the program above that includes the Main() function. Here, inside the Student Class, we created a Marks class and then created the Marks class object. The Student Class also includes 4 roll-no data members, name, fees, address. We built two ReadInfo() and PrintInfo() methods in the student class ().
To read the student information from the user, the ReadInfo() method is used. The PrintInfo() method is used on the console screen to print student details.
The Marks class includes two SetMarks() and PrintMarks() methods to set and print the values for marks.
The Main() method is the entry point for the program, where we created the User class object then called ReadInfo() and PrintInfo() using the student information object to read and print.