Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All

Decimal.CompareTo Method (Object)

Compares this instance to a specified object and returns a comparison of their relative values.

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

'Declaration
Public Function CompareTo ( _
	value As Object _
) As Integer

Parameters

value
Type: System.Object

The object to compare with this instance, or Nothing.

Return Value

Type: System.Int32
A signed number indicating the relative values of this instance and value.

Return value

Meaning

Less than zero

This instance is less than value.

Zero

This instance is equal to value.

Greater than zero

This instance is greater than value.

-or-

value is Nothing.

Implements

IComparable.CompareTo(Object)

ExceptionCondition
ArgumentException

value is not a Decimal.

Any instance of Decimal, regardless of its value, is considered greater than Nothing.

Parameter value must be Nothing or an instance of Decimal; otherwise, an exception is thrown.

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

' Example of the Decimal.CompareTo and Decimal.Equals instance methods. 
Imports System
Imports Microsoft.VisualBasic

Module DecCompToEqualsObjDemo

    ' Get the exception type name; remove the namespace prefix. 
    Function GetExceptionType( 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( Left as Decimal, Right as Object, _
        RightText as String )

        Console.WriteLine( "{0,-46}{1}", "Object: " & RightText, _
            Right )
        Console.WriteLine( "{0,-46}{1}", "Left.Equals( Object )", _
            Left.Equals( Right ) )
        Console.Write( "{0,-46}", "Left.CompareTo( Object )" )

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

    Sub Main( )
        Console.WriteLine( _
            "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 )

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

        Console.WriteLine( "{0,-46}{1}" & vbCrLf, _
            "Left: Decimal( 987.654 )", Left )

        ' Create objects to compare with the reference.
        CompDecimalToObject( Left, New Decimal( 9.8765400E+2 ), _
            "Decimal( 9.8765400E+2 )" )
        CompDecimalToObject( Left, 987.6541D, "987.6541D" )
        CompDecimalToObject( Left, 987.6539D, "987.6539D" )
        CompDecimalToObject( Left, _
            New Decimal( 987654000, 0, 0, false, 6 ), _
            "Decimal( 987654000, 0, 0, false, 6 )" )
        CompDecimalToObject( Left, 9.8765400E+2, _
            "Double 9.8765400E+2" )
        CompDecimalToObject( 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

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0, 1.1

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

XNA Framework

Supported in: 3.0, 2.0, 1.0

.NET for Windows Phone apps

Supported in: Windows Phone 8.1, Windows Phone Silverlight 8.1, Windows Phone Silverlight 8
Show:
© 2015 Microsoft