Decimal.CompareTo Method (Object)
Microsoft Silverlight will reach end of support after October 2021. Learn more.
Namespace: System
Assembly: mscorlib (in mscorlib.dll)
Syntax
'Declaration
<SecuritySafeCriticalAttribute> _
Public Function CompareTo ( _
value As Object _
) As Integer
[SecuritySafeCriticalAttribute]
public int CompareTo(
Object value
)
Parameters
- value
Type: System.Object
An Object or nulla null reference (Nothing in Visual Basic).
Return Value
Type: System.Int32
A signed number indicating the relationship between 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 nulla null reference (Nothing in Visual Basic). |
Implements
Remarks
Any instance of Decimal, regardless of its value, is considered greater than nulla null reference (Nothing in Visual Basic).
Parameter value must be nulla null reference (Nothing in Visual Basic) or an instance of Decimal; otherwise, an exception is thrown.
Examples
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.
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
// Example of the decimal.CompareTo and decimal.Equals instance
// methods.
using System;
class Example
{
// Get the exception type name; remove the namespace prefix.
public static string GetExceptionType(Exception ex)
{
string exceptionType = ex.GetType().ToString();
return exceptionType.Substring(
exceptionType.LastIndexOf('.') + 1);
}
// Compare the decimal to the object parameters,
// and display the object parameters with the results.
public static void CompDecimalToObject(System.Windows.Controls.TextBlock outputBlock, decimal Left,
object Right, string RightText)
{
outputBlock.Text += String.Format("{0,-46}{1}", "object: " + RightText,
Right) + "\n";
outputBlock.Text += String.Format("{0,-46}{1}", "Left.Equals( object )",
Left.Equals(Right)) + "\n";
outputBlock.Text += String.Format("{0,-46}", "Left.CompareTo( object )");
try
{
// Catch the exception if CompareTo( ) throws one.
outputBlock.Text += String.Format("{0}\n", Left.CompareTo(Right)) + "\n";
}
catch (Exception ex)
{
outputBlock.Text += String.Format("{0}\n", GetExceptionType(ex)) + "\n";
}
}
public static void Demo(System.Windows.Controls.TextBlock outputBlock)
{
outputBlock.Text += String.Format(
"This example of the decimal.Equals( object ) and \n" +
"decimal.CompareTo( object ) methods generates the \n" +
"following output. It creates several different " +
"decimal \nvalues and compares them with the following " +
"reference value.\n") + "\n";
// Create a reference decimal value.
decimal Left = new decimal(987.654);
outputBlock.Text += String.Format("{0,-46}{1}\n",
"Left: decimal( 987.654 )", Left) + "\n";
// Create objects to compare with the reference.
CompDecimalToObject(outputBlock, Left, new decimal(9.8765400E+2),
"decimal( 9.8765400E+2 )");
CompDecimalToObject(outputBlock, Left, 987.6541M, "987.6541D");
CompDecimalToObject(outputBlock, Left, 987.6539M, "987.6539D");
CompDecimalToObject(outputBlock, Left,
new decimal(987654000, 0, 0, false, 6),
"decimal( 987654000, 0, 0, false, 6 )");
CompDecimalToObject(outputBlock, Left, 9.8765400E+2,
"Double 9.8765400E+2");
CompDecimalToObject(outputBlock, Left, "987.654", "String \"987.654\"");
}
}
/*
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
*/
Version Information
Silverlight
Supported in: 5, 4, 3
Silverlight for Windows Phone
Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0
XNA Framework
Supported in: Xbox 360, Windows Phone OS 7.0
Platforms
For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.