Structure overrides ToString method : Structure « Language Basics « VB.Net






Structure overrides ToString method

 
Imports System

Public Class MainClass
    
    Shared Sub Main()
        Dim loc1 As Location  
        loc1.XVal = 75
        loc1.YVal = 225

        ' display the values in the structure
        Console.WriteLine("Loc1 location: {0}", loc1)
    End Sub
End Class

      Public Structure Location
         Private myXVal As Integer
         Private myYVal As Integer

         Public Sub New( _
            ByVal xCoordinate As Integer, ByVal yCoordinate As Integer)
             myXVal = xCoordinate
             myYVal = yCoordinate
         End Sub 'New

         Public Property XVal(  ) As Integer
             Get
                 Return myXVal
             End Get
             Set(ByVal Value As Integer)
                 myXVal = Value
             End Set
         End Property

         Public Property YVal(  ) As Integer
             Get
                 Return myYVal
             End Get
             Set(ByVal Value As Integer)
                 myYVal = Value
             End Set
         End Property

         Public Overrides Function ToString(  ) As String
             Return String.Format("{0}, {1}", XVal, YVal)
         End Function 
     End Structure 
           
         
  








Related examples in the same category

1.Structure Variable assignment
2.ToString Method for Structure data typeToString Method for Structure data type
3.Structure with Constructor
4.Store Structure into a Collection
5.Compare Structure and ClassCompare Structure and Class
6.Pass Structure into a FunctionPass Structure into a Function
7.Simple Structure DemoSimple Structure Demo
8.ValueType.Equals Method Indicates whether this instance and a specified object are equal.