Export (0) Print
Expand All

CompareInfo.GetCompareInfo Method

Initializes a new instance of the CompareInfo class.

Overload List

Initializes a new instance of the CompareInfo class that is associated with the culture having the specified identifier.

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Shared Function GetCompareInfo(Integer) As CompareInfo
[C#] public static CompareInfo GetCompareInfo(int);
[C++] public: static CompareInfo* GetCompareInfo(int);
[JScript] public static function GetCompareInfo(int) : CompareInfo;

Initializes a new instance of the CompareInfo class that is associated with the culture having the specified name.

[Visual Basic] Overloads Public Shared Function GetCompareInfo(String) As CompareInfo
[C#] public static CompareInfo GetCompareInfo(string);
[C++] public: static CompareInfo* GetCompareInfo(String*);
[JScript] public static function GetCompareInfo(String) : CompareInfo;

Initializes a new instance of the CompareInfo class that is associated with the culture having the specified identifier and uses string comparison methods in the specified Assembly.

[Visual Basic] Overloads Public Shared Function GetCompareInfo(Integer, Assembly) As CompareInfo
[C#] public static CompareInfo GetCompareInfo(int, Assembly);
[C++] public: static CompareInfo* GetCompareInfo(int, Assembly*);
[JScript] public static function GetCompareInfo(int, Assembly) : CompareInfo;

Initializes a new instance of the CompareInfo class that is associated with the culture having the specified name and uses string comparison methods in the specified Assembly.

[Visual Basic] Overloads Public Shared Function GetCompareInfo(String, Assembly) As CompareInfo
[C#] public static CompareInfo GetCompareInfo(string, Assembly);
[C++] public: static CompareInfo* GetCompareInfo(String*, Assembly*);
[JScript] public static function GetCompareInfo(String, Assembly) : CompareInfo;

Example

[Visual Basic, C#, C++] The following code example compares two strings using the different CompareInfo instances: a CompareInfo instance associated with the "Spanish - Spain" culture with international sort, a CompareInfo instance associated with the "Spanish - Spain" culture with traditional sort, and a CompareInfo instance associated with the InvariantCulture.

[Visual Basic, C#, C++] Note   This example shows how to use one of the overloaded versions of GetCompareInfo. For other examples that might be available, see the individual overload topics.
[Visual Basic] 
Imports System
Imports System.Globalization

Public Class SamplesCompareInfo

   Public Shared Sub Main()

      ' Defines the strings to compare.
      Dim myStr1 As [String] = "calle"
      Dim myStr2 As [String] = "calor"

      ' Uses GetCompareInfo to create the CompareInfo that uses the "es-ES" culture with international sort.
      Dim myCompIntl As CompareInfo = CompareInfo.GetCompareInfo("es-ES")

      ' Uses GetCompareInfo to create the CompareInfo that uses the "es-ES" culture with traditional sort.
      Dim myCompTrad As CompareInfo = CompareInfo.GetCompareInfo(&H40A)

      ' Uses the CompareInfo property of the InvariantCulture.
      Dim myCompInva As CompareInfo = CultureInfo.InvariantCulture.CompareInfo

      ' Compares two strings using myCompIntl.
      Console.WriteLine("Comparing ""{0}"" and ""{1}""", myStr1, myStr2)
      Console.WriteLine("   With myCompIntl.Compare: {0}", myCompIntl.Compare(myStr1, myStr2))
      Console.WriteLine("   With myCompTrad.Compare: {0}", myCompTrad.Compare(myStr1, myStr2))
      Console.WriteLine("   With myCompInva.Compare: {0}", myCompInva.Compare(myStr1, myStr2))

   End Sub 'Main 

End Class 'SamplesCompareInfo


'This code produces the following output.
'
'Comparing "calle" and "calor"
'   With myCompIntl.Compare: -1
'   With myCompTrad.Compare: 1
'   With myCompInva.Compare: -1


[C#] 
using System;
using System.Globalization;

public class SamplesCompareInfo  {

   public static void Main()  {

      // Defines the strings to compare.
      String myStr1 = "calle";
      String myStr2 = "calor";

      // Uses GetCompareInfo to create the CompareInfo that uses the "es-ES" culture with international sort.
      CompareInfo myCompIntl = CompareInfo.GetCompareInfo( "es-ES" );
      
      // Uses GetCompareInfo to create the CompareInfo that uses the "es-ES" culture with traditional sort.
      CompareInfo myCompTrad = CompareInfo.GetCompareInfo( 0x040A );

      // Uses the CompareInfo property of the InvariantCulture.
      CompareInfo myCompInva = CultureInfo.InvariantCulture.CompareInfo;

      // Compares two strings using myCompIntl.
      Console.WriteLine( "Comparing \"{0}\" and \"{1}\"", myStr1, myStr2 );
      Console.WriteLine( "   With myCompIntl.Compare: {0}", myCompIntl.Compare( myStr1, myStr2 ) );
      Console.WriteLine( "   With myCompTrad.Compare: {0}", myCompTrad.Compare( myStr1, myStr2 ) );
      Console.WriteLine( "   With myCompInva.Compare: {0}", myCompInva.Compare( myStr1, myStr2 ) );

   }

}


/*
This code produces the following output.

Comparing "calle" and "calor"
   With myCompIntl.Compare: -1
   With myCompTrad.Compare: 1
   With myCompInva.Compare: -1

*/

[C++] 
#using <mscorlib.dll>
using namespace System;
using namespace System::Globalization;

int main() {
   // Defines the strings to compare.
   String*  myStr1 = S"calle";
   String*  myStr2 = S"calor";

   // Uses GetCompareInfo to create the CompareInfo that 
   // uses the S"es-ES" culture with international sort.
   CompareInfo*  myCompIntl = CompareInfo::GetCompareInfo(S"es-ES");

   // Uses GetCompareInfo to create the CompareInfo that 
   // uses the S"es-ES" culture with traditional sort.
   CompareInfo*  myCompTrad = CompareInfo::GetCompareInfo(0x040A);

   // Uses the CompareInfo property of the InvariantCulture.
   CompareInfo*  myCompInva = CultureInfo::InvariantCulture->CompareInfo;

   // Compares two strings using myCompIntl.
   Console::WriteLine(S"Comparing \"{0}\" and \"{1}\"", myStr1, myStr2);
      Console::WriteLine(S"   With myCompIntl::Compare: {0}", 
         __box(myCompIntl->Compare(myStr1, myStr2)));
   Console::WriteLine(S"   With myCompTrad::ompare: {0}", 
      __box(myCompTrad->Compare(myStr1, myStr2)));
   Console::WriteLine(S"   With myCompInva::Compare: {0}", 
      __box(myCompInva->Compare(myStr1, myStr2)));
}

/*
This code produces the following output.

Comparing "calle" and "calor"
   With myCompIntl::Compare: -1
   With myCompTrad::ompare: 1
   With myCompInva::Compare: -1
*/

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

See Also

CompareInfo Class | CompareInfo Members | System.Globalization Namespace

Show:
© 2014 Microsoft