다음을 통해 공유


Enum.CompareTo Method

Microsoft Silverlight will reach end of support after October 2021. Learn more.

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

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

Syntax

'Declaration
<SecuritySafeCriticalAttribute> _
Public Function CompareTo ( _
    target As Object _
) As Integer
[SecuritySafeCriticalAttribute]
public int CompareTo(
    Object target
)

Parameters

  • target
    Type: System.Object
    An object to compare, or nulla null reference (Nothing in Visual Basic).

Return Value

Type: System.Int32
A 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 nulla null reference (Nothing in Visual Basic).

Implements

IComparable.CompareTo(Object)

Exceptions

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 nulla null reference (Nothing in Visual Basic).

Examples

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
using System;

public class Example
{
   enum VehicleDoors { Motorbike = 0, Sportscar = 2, Sedan = 4, Hatchback = 5 };

   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      VehicleDoors myVeh = VehicleDoors.Sportscar;
      VehicleDoors yourVeh = VehicleDoors.Motorbike;
      VehicleDoors otherVeh = VehicleDoors.Sedan;

      outputBlock.Text += String.Format("Does a {0} have more doors than a {1}?", myVeh, yourVeh) + "\n";
      outputBlock.Text += String.Format("{0}{1}", myVeh.CompareTo(yourVeh) > 0 ? "Yes" : "No", "\n") + "\n";

      outputBlock.Text += String.Format("Does a {0} have more doors than a {1}?", myVeh, otherVeh) + "\n";
      outputBlock.Text += String.Format("{0}", myVeh.CompareTo(otherVeh) > 0 ? "Yes" : "No") + "\n";
   }
}

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.

See Also

Reference