Override ToString function : ToString « Class « VB.Net






Override ToString function

Override ToString function
Imports System

Public Class MainClass

    Shared Sub Main(ByVal args As String())
      Dim point As Point

      point = New Point(72, 115) ' instantiate Point object

      ' display point coordinates via X and Y properties
      Console.WriteLine( "X coordinate is " & point.X & _
         vbCrLf & "Y coordinate is " & point.Y )

      point.X = 10 ' set x-coordinate via X property
      point.Y = 10 ' set y-coordinate via Y property

      ' display new point value
      Console.WriteLine("The new location of point is " & point.ToString() )

    End Sub
End Class

' Point class represents an x-y coordinate pair.

Public Class Point
   ' implicitly Inherits Object

   Private mX, mY As Integer

   Public Sub New()
      ' implicit call to Object constructor occurs here
      X = 0
      Y = 0
   End Sub ' New

   ' constructor
   Public Sub New(ByVal xValue As Integer,ByVal yValue As Integer)
      ' implicit call to Object constructor occurs here
      X = xValue
      Y = yValue
   End Sub ' New

   ' property X
   Public Property X() As Integer
      Get
         Return mX
      End Get

      Set(ByVal xValue As Integer)
         mX = xValue ' no need for validation
      End Set

   End Property ' X

   ' property Y 
   Public Property Y() As Integer

      Get
         Return mY
      End Get

      Set(ByVal yValue As Integer)
         mY = yValue ' no need for validation
      End Set

   End Property ' Y

   ' return String representation of Point
   Public Overrides Function ToString() As String
      Return "[" & mX & ", " & mY & "]"
   End Function ' ToString

End Class 
           
       








Related examples in the same category

1.Override Object.ToStringOverride Object.ToString
2.Override ToString Method to provide meaningful informationOverride ToString Method to provide meaningful information