Export (0) Print
Expand All

String.CompareTo Method (Object)

.NET Framework 1.1

Compares this instance with a specified Object.

[Visual Basic]
Overloads Public Overridable Function CompareTo( _
   ByVal value As Object _
) As Integer Implements IComparable.CompareTo
[C#]
public virtual int CompareTo(
 object value
);
[C++]
public: virtual int CompareTo(
 Object* value
);
[JScript]
public function CompareTo(
   value : Object
) : int;

Parameters

value
An Object that evaluates to a String.

Return Value

A 32-bit signed integer indicating the lexical relationship between the two comparands.

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

Implements

IComparable.CompareTo

Exceptions

Exception Type Condition
ArgumentException value is not a String.

Remarks

value must be a String object.

This method performs a word (case-sensitive and culture-sensitive) comparison using the current culture. For more information about word, string, and ordinal sorts, see System.Globalization.CompareOptions.

For more information about the behavior of this method, see the Remarks section of the Compare method.

Example

The following code example demonstrates how you can use the CompareTo method with an Object.

[Visual Basic] 
Imports System

Public Class [MyClass]
End Class '[MyClass]

Public Class ArgExceptionExample
   
   Public Shared Sub Main()
      Dim my As New [MyClass]()
      Dim s As String = "sometext"
      Try
         Dim i As Integer = s.CompareTo(my)
      Catch e As Exception
         Console.WriteLine("Error: {0}", e.ToString())
      End Try
   End Sub 'Main
End Class 'ArgExceptionExample

[C#] 
using System;

public class MyClass {}
public class ArgExceptionExample 
   {
   public static void Main()
      {
      MyClass my = new MyClass();
      string s = "sometext";
      try 
         {
         int i = s.CompareTo(my);
         }
            catch (Exception e) 
            {
            Console.WriteLine("Error: {0}",e.ToString());
            }
      }
}

[C++] 
#using <mscorlib.dll>

using namespace System;

public __gc class MyClass {};

int main() {
   MyClass* my = new MyClass();
   String* s = S"sometext";
   try {
      int i = s->CompareTo(my);
   } catch (Exception* e) {
      Console::WriteLine(S"Error: {0}", e->ToString());
   }
}

[JScript] 
import System;

public class MyClass {}
public class ArgExceptionExample 
   {
   public static function Main()
      {
      var my : MyClass = new MyClass();
      var s : String = "sometext";
      try 
         {
         var i : int = s.CompareTo(my);
         }
     catch (e : Exception) 
         {
         Console.WriteLine("Error: {0}",e.ToString());
         }
      }
}

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework, Common Language Infrastructure (CLI) Standard

See Also

String Class | String Members | System Namespace | String.CompareTo Overload List | Compare | CompareOrdinal

Show:
© 2015 Microsoft