This blog
defines the ToString() method in VB.NET.
ToString() method
The
ToString
method should provide the string representation of an object.
We can override
ToString() method for
Rectangle.
This function changes the result to string
format. It is needed when you want to have your output as a string format, like
when you want to pass is to a label text property, and your original output is
for example , Date/Time format. or pass it to a database field with varchar
datatype.
For example
C# code
using
System;
class
Rectangle
{
public int
Width;
public int
Height;
public string
ToString()
{
return "Rectangle: "
+ Width + " by " + Height;
}
}
class
Program
{
static void
Main(string[] args)
{
Rectangle r = new
Rectangle();
r.Width = 4;
r.Height = 5;
Console.WriteLine(r);
}
}
VB.NET code
Using convertor
Class
Rectangle
Public Width As
Integer
Public Height As
Integer
Public Overloads
Function ToString() As
String
Return "Rectangle: "
& Width & " by " & Height
End Function
End
Class
Class
Program
Private Shared
Sub Main(ByVal
args As String())
Dim r As
New Rectangle()
r.Width = 4
r.Height = 5
Console.WriteLine(r)
End Sub
End
Class