Freigeben über


Enum.CompareTo-Methode

Vergleicht diese Instanz mit einem angegebenen Objekt und gibt eine Angabe über das Verhältnis der entsprechenden Werte zurück.

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

Syntax

'Declaration
Public Function CompareTo ( _
    target As Object _
) As Integer
'Usage
Dim instance As Enum
Dim target As Object
Dim returnValue As Integer

returnValue = instance.CompareTo(target)
public int CompareTo (
    Object target
)
public:
virtual int CompareTo (
    Object^ target
) sealed
public final int CompareTo (
    Object target
)
public final function CompareTo (
    target : Object
) : int

Parameter

  • target
    Ein Vergleichsobjekt oder NULL (Nothing in Visual Basic).

Rückgabewert

Eine Zahl mit Vorzeichen, die das Verhältnis zwischen dem Wert dieser Instanz und target angibt.

Rückgabewert

Beschreibung

Kleiner als 0

Der Wert dieser Instanz ist kleiner als der Wert von target.

0

Der Wert dieser Instanz ist gleich dem Wert von target.

Größer als 0

Der Wert dieser Instanz ist größer als der Wert von target.

- oder -

target ist NULL (Nothing in Visual Basic).

Ausnahmen

Ausnahmetyp Bedingung

ArgumentException

target hat nicht denselben Typ wie diese Instanz.

InvalidOperationException

Diese Instanz ist nicht vom Typ SByte, Int16, Int32, Int64, Byte, UInt16, UInt32 oder UInt64.

Beispiel

Das folgende Codebeispiel veranschaulicht die Verwendung von CompareTo im Kontext von Enum.

Imports System

Public Class CompareToTest
    
    Enum VehicleDoors
        Motorbike = 0
        Sportscar = 2
        Sedan = 4
        Hatchback = 5
    End Enum
    
    Public Shared Sub Main()
        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"
        Console.WriteLine("Does a {0} have more doors than a {1}?", myVeh, yourVeh)
        Console.WriteLine("{0}{1}", output, Environment.NewLine)
        
        Console.WriteLine("Does a {0} have more doors than a {1}?", myVeh, otherVeh)
        If myVeh.CompareTo(otherVeh) > 0 Then output = "Yes" Else output = "No"
        Console.WriteLine("{0}", output)
    End Sub 'Main
End Class 'CompareToTest
using System;

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

    public static void Main() {
        VehicleDoors myVeh = VehicleDoors.Sportscar;
        VehicleDoors yourVeh = VehicleDoors.Motorbike;
        VehicleDoors otherVeh = VehicleDoors.Sedan;

        Console.WriteLine("Does a {0} have more doors than a {1}?", myVeh, yourVeh);
        Console.WriteLine( "{0}{1}", myVeh.CompareTo(yourVeh) > 0 ? "Yes" : "No", Environment.NewLine );

        Console.WriteLine("Does a {0} have more doors than a {1}?", myVeh, otherVeh);
        Console.WriteLine( "{0}", myVeh.CompareTo(otherVeh) > 0 ? "Yes" : "No" );
    }
}
using namespace System;
enum class VehicleDoors
{
   Motorbike = 0,
   Sportscar = 2,
   Sedan = 4,
   Hatchback = 5
};

int main()
{
   VehicleDoors myVeh = VehicleDoors::Sportscar;
   VehicleDoors yourVeh = VehicleDoors::Motorbike;
   VehicleDoors otherVeh = VehicleDoors::Sedan;
   Console::WriteLine(  "Does a {0} have more doors than a {1}?", myVeh, yourVeh );
   Int32 iRes = myVeh.CompareTo( yourVeh );
   Console::WriteLine(  "{0}{1}", iRes > 0 ? (String^)"Yes" : "No", Environment::NewLine );
   Console::WriteLine(  "Does a {0} have more doors than a {1}?", myVeh, otherVeh );
   iRes = myVeh.CompareTo( otherVeh );
   Console::WriteLine(  "{0}", iRes > 0 ? (String^)"Yes" : "No" );
}
import System.*;

public class CompareToTest
{
    static class VehicleDoors
    {
        private int member;

        VehicleDoors(int n)
        {
            member = n;
        }//VehicleDoors

        public String get_Member()
        {
            if (member == 0) {
                return "MotorBike";
            }
            else {
                if (member == 2) {
                    return "Sportscar";
                }
                else {
                    if (member == 4) {
                        return "Sedan";
                    }
                    else {
                        if (member == 5) {
                            return "Hatchback";
                        }
                        else {
                            return "";
                        }
                    }
                }
            }
        }//get_Member

        public int get_Number()
        {
            return member;
        }//get_Number

        public static int Motorbike = 0;
        public static int Sportscar = 2;
        public static int Sedan = 4;
        public static int Hatchback = 5;
    } //VehicleDoors

    public static void main(String[] args)
    {
        VehicleDoors myVeh = new VehicleDoors(VehicleDoors.Sportscar);
        VehicleDoors yourVeh = new VehicleDoors(VehicleDoors.Motorbike);
        VehicleDoors otherVeh = new VehicleDoors(VehicleDoors.Sedan);

        Console.WriteLine("Does a {0} have more doors than a {1}?", 
            myVeh.get_Member(), yourVeh.get_Member());
        Console.WriteLine("{0}{1}", 
            (String.Compare(System.Convert.ToString(myVeh.get_Number()), 
            System.Convert.ToString(yourVeh.get_Number())) > 0) ? "Yes" : "No", 
            Environment.get_NewLine());

        Console.WriteLine("Does a {0} have more doors than a {1}?", 
            myVeh.get_Member(), otherVeh.get_Member());
        Console.WriteLine("{0}", (
            String.Compare(System.Convert.ToString(myVeh.get_Number()), 
            System.Convert.ToString(otherVeh.get_Number())) > 0) ? "Yes" : "No");
    } //main
} //CompareToTest

Plattformen

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile für Pocket PC, Windows Mobile für Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

.NET Framework unterstützt nicht alle Versionen sämtlicher Plattformen. Eine Liste der unterstützten Versionen finden Sie unter Systemanforderungen.

Versionsinformationen

.NET Framework

Unterstützt in: 2.0, 1.1, 1.0

.NET Compact Framework

Unterstützt in: 2.0

Siehe auch

Referenz

Enum-Struktur
Enum-Member
System-Namespace
Equals