This documentation is archived and is not being maintained.

CompareInfo.GetCompareInfo Method (String)

.NET Framework 1.1

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( _
   ByVal name As String _
) As CompareInfo
[C#]
public static CompareInfo GetCompareInfo(
 string name
);
[C++]
public: static CompareInfo* GetCompareInfo(
 String* name
);
[JScript]
public static function GetCompareInfo(
   name : String
) : CompareInfo;

Parameters

name
A string representing the culture name.

Return Value

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

Exceptions

Exception Type Condition
ArgumentNullException name is a null reference (Nothing in Visual Basic).
ArgumentException name is an invalid culture name.

Remarks

If a security decision depends on a string comparison or a case-change operation, use the InvariantCulture to ensure that the behavior will be consistent regardless of the culture settings of the system.

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] 
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.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family

See Also

CompareInfo Class | CompareInfo Members | System.Globalization Namespace | CompareInfo.GetCompareInfo Overload List

Show: