VB.Net program to demonstrate the private methods inside the class

By Nidhi Last Updated : November 13, 2024

Private methods inside the class in VB.Net

Here, we will create a Student class that contains some private methods inside the class; the private methods will be called inside the public method.

Program/Source Code:

The source code to demonstrate the private methods inside the class is given below. The given program is compiled and executed successfully.

VB.Net code to implement private methods inside the class

'VB.Net program to demonstrate the private methods inside the class. Class Student Private roll_no As Integer Private name As String Private fees As Integer Private address As String Private Sub ReadStart() Console.WriteLine("Start reading from user") End Sub Private Sub ReadStop() Console.WriteLine("Stop reading") End Sub Sub ReadInfo() ReadStart() 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() ReadStop() 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) End Sub End Class Module Module1 Sub Main() Dim S As New Student S.ReadInfo() S.PrintInfo() End Sub End Module

Output:

Start reading from user
Enter roll number: 101
Enter student name: Alex carry
Enter student fees: 23457
Enter student address: USA
Stop reading
Student Information
        Roll Number: 101
        Roll Name  : Alex carry
        Fees       : 23457
        address    : USA
Press any key to continue . . .

Explanation:

In the above program, we created a module Module1 that contains the Main() function. Here, we created a Student class that contains 4 data members roll_no, name, fees, address. In the Student class we created two private method ReadStart() and ReadStop() and two public methods ReadInfo() and PrintInfo().

The private methods will be called inside the public methods because we cannot call private members outside the class.

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 Main() method is the entry point for the program, here we created the object of Student class then called ReadInfo() and PrintInfo() using object to read and print student information.

VB.Net Basic Programs »


Advertisement
Advertisement

Comments and Discussions!

Load comments ↻


Advertisement
Advertisement
Advertisement



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