VB.Net/Class/ToString

Материал из VB Эксперт
Перейти к: навигация, поиск

Override Object.ToString

<source lang="vbnet"> Imports System Public Class MainClass

   Shared Sub Main()
        Dim i As Integer = 5
        Console.WriteLine("The value of i is: {0}", i.ToString(  ))
        Dim milo As New Dog(62)
        Console.WriteLine("My dog Milo weighs {0} pounds", milo.ToString(  ))
   End Sub

End Class

Public Class Dog
    Private weight As Integer
    Public Sub New(ByVal weight As Integer)
        Me.weight = weight
    End Sub "New
    Public Overrides Function ToString(  ) As String
        Return weight.ToString(  )
    End Function "ToString
End Class "Dog
          
      </source>


Override ToString function

<source lang="vbnet"> 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

      </source>


Override ToString Method to provide meaningful information

<source lang="vbnet"> Imports System Imports System.IO

Public Class MainClass

 Shared Sub Main()
       Dim e As New Employee("Joe", 10000)
       Console.WriteLine(e.TheName & " salary is " & e.Salary)
      
       Console.WriteLine(e)
 End Sub
 Public Class Employee
   Private m_Name As String
   Private m_Salary As Decimal
   Public Sub New(ByVal sName As String, ByVal curSalary As Decimal)
           If sName = String.Empty Then
               Console.WriteLine("no names")
           Else
               m_Name = sName
           End If
   End Sub
       Public Property TheName() As String
           Get
               Return m_Name
           End Get
           Set(ByVal Value As String)
               m_Name = Value
           End Set
       End Property
       Public ReadOnly Property Salary() As Decimal
           Get
               Return m_Salary
           End Get
       End Property
      
       Public Overrides Function ToString() As String
           Return (m_Name & " " & Me.GetType.ToString)
       End Function
   End Class

End Class


      </source>