Windows Dev Center

Decimal.Equals Method (Object)

Returns a value indicating whether this instance and a specified Object represent the same type and value.

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)

'Declaration
Public Overrides Function Equals ( _
	value As Object _
) As Boolean

Parameters

value
Type: System.Object
An Object.

Return Value

Type: System.Boolean
true if value is a Decimal and equal to this instance; otherwise, false.

The following code example compares several Decimal and other objects to a reference Decimal value using the Equals method.


' Example of the Decimal.CompareTo and Decimal.Equals instance methods.

Module Example

   ' Get the exception type name; remove the namespace prefix.
   Function GetExceptionType(ByVal ex As Exception) As String

      Dim exceptionType As String = ex.GetType().ToString()
      Return exceptionType.Substring( _
          exceptionType.LastIndexOf("."c) + 1)
   End Function

   ' Compare the Decimal to the Object parameters, 
   ' and display the Object parameters with the results.
   Sub CompDecimalToObject(ByVal outputBlock As System.Windows.Controls.TextBlock, ByVal Left As Decimal, ByVal Right As Object, _
       ByVal RightText As String)

      outputBlock.Text &= String.Format("{0,-46}{1}", "Object: " & RightText, _
          Right) & vbCrLf
      outputBlock.Text &= String.Format("{0,-46}{1}", "Left.Equals( Object ) & vbCrLf", _
          Left.Equals(Right))
      outputBlock.Text &= String.Format("{0,-46}", "Left.CompareTo( Object )")

      ' Catch the exception if CompareTo( ) throws one.
      Try
         outputBlock.Text &= String.Format("{0}" & vbCrLf, _
             Left.CompareTo(Right)) & vbCrLf
      Catch ex As Exception
         outputBlock.Text &= String.Format("{0}" & vbCrLf, _
             GetExceptionType(ex)) & vbCrLf
      End Try
   End Sub

   Public Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)
      outputBlock.Text &= String.Format( _
          "This example of the Decimal.Equals( Object ) " & _
          "and " & vbCrLf & "Decimal.CompareTo( Object ) " & _
          "methods generates the " & vbCrLf & _
          "following output. It creates several different " & _
          "Decimal " & vbCrLf & "values and compares them " & _
          "with the following reference value." & vbCrLf) & vbCrLf

      ' Create a reference Decimal value.
      Dim Left As New Decimal(987.654)

      outputBlock.Text &= String.Format("{0,-46}{1}" & vbCrLf, _
          "Left: Decimal( 987.654 )", Left) & vbCrLf

      ' Create objects to compare with the reference.
      CompDecimalToObject(outputBlock, Left, New Decimal(987.654), _
          "Decimal( 9.8765400E+2 )")
      CompDecimalToObject(outputBlock, Left, 987.6541D, "987.6541D")
      CompDecimalToObject(outputBlock, Left, 987.6539D, "987.6539D")
      CompDecimalToObject(outputBlock, Left, _
          New Decimal(987654000, 0, 0, False, 6), _
          "Decimal( 987654000, 0, 0, false, 6 )")
      CompDecimalToObject(outputBlock, Left, 987.654, _
          "Double 9.8765400E+2")
      CompDecimalToObject(outputBlock, Left, "987.654", _
          "String ""987.654""")
   End Sub
End Module

' This example of the Decimal.Equals( Object ) and
' Decimal.CompareTo( Object ) methods generates the
' following output. It creates several different Decimal
' values and compares them with the following reference value.
' 
' Left: Decimal( 987.654 )                      987.654
' 
' Object: Decimal( 9.8765400E+2 )               987.654
' Left.Equals( Object )                         True
' Left.CompareTo( Object )                      0
' 
' Object: 987.6541D                             987.6541
' Left.Equals( Object )                         False
' Left.CompareTo( Object )                      -1
' 
' Object: 987.6539D                             987.6539
' Left.Equals( Object )                         False
' Left.CompareTo( Object )                      1
' 
' Object: Decimal( 987654000, 0, 0, false, 6 )  987.654000
' Left.Equals( Object )                         True
' Left.CompareTo( Object )                      0
' 
' Object: Double 9.8765400E+2                   987.654
' Left.Equals( Object )                         False
' Left.CompareTo( Object )                      ArgumentException
' 
' Object: String "987.654"                      987.654
' Left.Equals( Object )                         False
' Left.CompareTo( Object )                      ArgumentException


Windows Phone OS

Supported in: 8.1, 8.0, 7.1, 7.0

Windows Phone

Show:
© 2015 Microsoft