Program to Override the ToString() method of Object class in Child class using VB.net

Rajnilari2015
Posted by Rajnilari2015 under VB.NET category on | Points: 40 | Views : 342
Let us say we have a class by the name Employee with some properties and we want to override the ToString() method of Object class in the Employee class. The below program will do so.

Public Class Employee
Public Property EmployeeId() As Integer
Get
Return m_EmployeeId
End Get
Set
m_EmployeeId = Value
End Set
End Property
Private m_EmployeeId As Integer
Public Property EmployeeName() As String
Get
Return m_EmployeeName
End Get
Set
m_EmployeeName = Value
End Set
End Property
Private m_EmployeeName As String
Public Property Gender() As String
Get
Return m_Gender
End Get
Set
m_Gender = Value
End Set
End Property
Private m_Gender As String
Public Property PhoneNumber() As String
Get
Return m_PhoneNumber
End Get
Set
m_PhoneNumber = Value
End Set
End Property
Private m_PhoneNumber As String
Public Property Email() As String
Get
Return m_Email
End Get
Set
m_Email = Value
End Set
End Property
Private m_Email As String
Public Property Address() As String
Get
Return m_Address
End Get
Set
m_Address = Value
End Set
End Property
Private m_Address As String

Public Overrides Function ToString() As String
Dim strEmployeeRecords As String = "EmployeeId: {EmployeeId}"
strEmployeeRecords += ", EmployeeName: {EmployeeName}"
strEmployeeRecords += ", Gender: {Gender}"
strEmployeeRecords += ", PhoneNumber: {PhoneNumber}"
strEmployeeRecords += ", Email: {Email}"
strEmployeeRecords += ", Address: {Address}"
Return [String].Format(strEmployeeRecords)
End Function
End Class

Comments or Responses

Login to post response