Enum.CompareTo Method
[ This article is for Windows Phone 8 developers. If you’re developing for Windows 10, see the latest documentation. ]
Compares this instance to a specified object and returns an indication of their relative values.
Assembly: mscorlib (in mscorlib.dll)
Parameters
- target
- Type: System.Object
An object to compare, or Nothing.
Return Value
Type: System.Int32A signed number indicating the relationship of this instance to target.
Return Value | Description |
|---|---|
Less than zero | The value of this instance is less than the value of target. |
Zero | The value of this instance is equal to the value of target. |
Greater than zero | The value of this instance is greater than the value of target. -or- target is Nothing. |
Implements
IComparable.CompareTo(Object)| Exception | Condition |
|---|---|
| ArgumentException | target and this instance are not the same type. |
| InvalidOperationException | This instance is not type SByte, Int16, Int32, Int64, Byte, UInt16, UInt32, or UInt64. |
| NullReferenceException | This instance is Nothing. |
The following example illustrates the use of CompareTo in the context of Enum.
Public Class Example Enum VehicleDoors Motorbike = 0 Sportscar = 2 Sedan = 4 Hatchback = 5 End Enum Public Shared Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock) Dim myVeh As VehicleDoors = VehicleDoors.Sportscar Dim yourVeh As VehicleDoors = VehicleDoors.Motorbike Dim otherVeh As VehicleDoors = VehicleDoors.Sedan Dim output As String If myVeh.CompareTo(yourVeh) > 0 Then output = "Yes" Else output = "No" outputBlock.Text &= String.Format("Does a {0} have more doors than a {1}?", myVeh, yourVeh) & vbCrLf outputBlock.Text &= String.Format("{0}{1}", output, vbCrLf) & vbCrLf outputBlock.Text &= String.Format("Does a {0} have more doors than a {1}?", myVeh, otherVeh) & vbCrLf If myVeh.CompareTo(otherVeh) > 0 Then output = "Yes" Else output = "No" outputBlock.Text &= String.Format("{0}", output) & vbCrLf End Sub 'Main End Class 'CompareToTest