The Visual C++ System::String class does not support greater than and less than operators (312844)



The information in this article applies to:

  • Microsoft Visual C++ 2005 Express Edition
  • Microsoft Visual C++ .NET (2003)
  • Microsoft Visual C++ .NET (2002)

This article was previously published under Q312844
This article refers to the following Microsoft .NET Framework Class Library namespace:
  • System.String

SYMPTOMS

The Visual C++ System::String class does not support the < and > operators.

CAUSE

This behavior occurs because of the large number of sort variations that you can use with the < and > operators.

RESOLUTION

You can use the Compare, CompareOrdinal, CompareTo, and Equals methods to compare string objects. These methods can honor or ignore case, language, or culture-specific information.

STATUS

This behavior is by design.

MORE INFORMATION

The following Visual C++ example demonstrates how to compare two strings:
//compiler option : cl /clr
#using <mscorlib.dll>
#include <tchar.h>

using namespace System;

// This is the entry point for this application.
int _tmain(void)
{
    
  String* myString= "1234";
			
  int rc = myString->CompareTo("abcd");
  
						
  if (rc>0)
	Console::WriteLine("1234 is greater than abcd"); 
			
   else if (rc==0)
	Console::WriteLine("1234 is equal to abcd");

   else
	Console::WriteLine("1234 is less than abcd");


  return 0;
}
				
Note You must add the common language runtime support compiler option (/clr:oldSyntax) in Visual C++ 2005 to successfully compile the previous code sample. To add the common language runtime support compiler option in Visual C++ 2005, follow these steps:
  1. Click Project, and then click <ProjectName> Properties.

    Note <ProjectName> is a placeholder for the name of the project.
  2. Expand Configuration Properties, and then click General.
  3. Click to select Common Language Runtime Support, Old Syntax (/clr:oldSyntax) in the Common Language Runtime support project setting in the right pane, click Apply, and then click OK.
For more information about the common language runtime support compiler option, visit the following Microsoft Web site:

/clr (Common Language Runtime Compilation)
http://msdn2.microsoft.com/en-us/library/k8d11d4s.aspx


Modification Type:MajorLast Reviewed:1/12/2006
Keywords:kbprb KB312844 kbAudDeveloper