Hi,
just looking at some sample code for building a structure.
Can someone explain to me how this code works that follows.
I can see here that the tostring method is overridden and overloaded but how is this being called by just calling the structure. One of the last lines of code below shows console.writeline(p) which outputs to contents of the structure to a string using the tostring function. However I am not sure how this is automatically called, shouldn't it be console.writeline(p.tostring) to get that sort of result. Thanks in advance.
just looking at some sample code for building a structure.
Can someone explain to me how this code works that follows.
I can see here that the tostring method is overridden and overloaded but how is this being called by just calling the structure. One of the last lines of code below shows console.writeline(p) which outputs to contents of the structure to a string using the tostring function. However I am not sure how this is automatically called, shouldn't it be console.writeline(p.tostring) to get that sort of result. Thanks in advance.
Code:
"
Public Overloads Overrides Function ToString() As String
Return FirstName + " " + LastName + ", age " + Age.ToString
End Function
Code:
Module Module1
Structure Person
Public FirstName As String
Public LastName As String
Public Age As Integer
Public Sub New(ByVal _firstname As String, ByVal _lastname As String, ByVal _age As Integer)
FirstName = _firstname
LastName = _lastname
Age = _age
End Sub
Public Overloads Overrides Function ToString() As String
Return FirstName + " " + LastName + ", age " + Age.ToString
End Function
End Structure
Sub Main()
Dim p As Person = New Person("Dan", "Farnell", 28)
Console.WriteLine(p)
Console.ReadLine()
End Sub
End Module