Every built-in type in VB.NET environment has a ToString method which returns a textual representation of the value. ToString method is declared to System.Object as
Public Overridable Function ToString() As String
and very class that inherits from System.Object inherits ToString method too. Since ToString method is declared as Overridable, inherited classes typically override this base method. Besides inheriting ToString method it is also overloaded with method that accepts a format string as a parameter. More information about format strings for ToString method can be found in MSDN. Here's a link to Int64.ToString method with format string. The same MSDN page contains also examples for formatting numbers, dates and time. Examples are provided to both VB.NET and C#.
When you write your own classes in VB.NET, there's nothing to prevent that you write your own ToString method too. Here's a simple PersonName class that implements ToString method.
Option Explicit On Option Strict On Public Class PersonName Private _FirstName As String Private _LastName As String Public Sub New() ' Initialize class _FirstName = "" _LastName = "" End Sub Public Sub New(ByVal FirstName As String, ByVal LastName As String) ' Initialize class _FirstName = FirstName _LastName = LastName End Sub Public Property FirstName() As String ' Get Return _FirstName End Get Set(ByVal value As String) _FirstName = value End Set End Property Public Property LastName() As String Get Return _LastName End Get Set(ByVal value As String) _LastName = value End Set End Property Public Overrides Function ToString() As String ' Return name as a string Dim TempStr As String TempStr = "" If _FirstName.Length > 0 Then TempStr = _FirstName End If If _LastName.Length > 0 Then If TempStr.Length > 0 Then ' Add space between names TempStr = TempStr & " " & _LastName Else TempStr = _LastName End If End If Return TempStr End Function Public Overloads Function ToString(ByVal Format As String) As String ' Return name as a string ' Format="f", "l", "fl", "lf", "f,l", "l,f" Dim TempStr As String TempStr = "" Select Case Format Case "f" TempStr = _FirstName Case "l" TempStr = _LastName Case "fl" If _FirstName.Length > 0 Then TempStr = _FirstName End If If _LastName.Length > 0 Then If TempStr.Length > 0 Then ' Add space between names TempStr = TempStr & " " & _LastName Else TempStr = _LastName End If End If Case "lf" If _LastName.Length > 0 Then TempStr = _LastName End If If _FirstName.Length > 0 Then If TempStr.Length > 0 Then ' Add space between names TempStr = TempStr & " " & _FirstName Else TempStr = _FirstName End If End If Case "f,l" If _FirstName.Length > 0 Then TempStr = _FirstName End If If _LastName.Length > 0 Then If TempStr.Length > 0 Then ' Add space between names TempStr = TempStr & ", " & _LastName Else TempStr = _LastName End If End If Case "l,f" If _LastName.Length > 0 Then TempStr = _LastName End If If _FirstName.Length > 0 Then If TempStr.Length > 0 Then ' Add space between names TempStr = TempStr & ", " & _FirstName Else TempStr = _FirstName End If End If End Select Return TempStr End Function End Class
The first ToString method has to be declared as Overrides, since the class is inherited from System.Object and the method overrides the method from the base class.
The second ToString method accepts a format string argument and it has to be declared as Overloads, since it overloads our first method. Accepted format strings are "f", "l", "fl", "lf", "f,l" and "l,f" and they affect if either first or the last name is outputted first and how they are separated.
The following example shows, how to test ToString methods and how the output from the our custom ToString method is formatted.
Dim aPersonName As New PersonName("John", "Doe") MessageBox.Show(aPersonName.ToString, _ "Name", MessageBoxButtons.OK, MessageBoxIcon.Information) MessageBox.Show(aPersonName.ToString("f"), _ "Name", MessageBoxButtons.OK, MessageBoxIcon.Information) MessageBox.Show(aPersonName.ToString("l"), _ "Name", MessageBoxButtons.OK, MessageBoxIcon.Information) MessageBox.Show(aPersonName.ToString("fl"), _ "Name", MessageBoxButtons.OK, MessageBoxIcon.Information) MessageBox.Show(aPersonName.ToString("lf"), _ "Name", MessageBoxButtons.OK, MessageBoxIcon.Information) MessageBox.Show(aPersonName.ToString("f,l"), _ "Name", MessageBoxButtons.OK, MessageBoxIcon.Information) MessageBox.Show(aPersonName.ToString("l,f"), _ "Name", MessageBoxButtons.OK, MessageBoxIcon.Information)
And the resulting output is
John Doe
John
Doe
John Doe
Doe John
John, Doe
Doe, John
No comments:
Post a Comment