TheSwamp

Code Red => .NET => Topic started by: A_LOTA_NOTA on March 06, 2008, 01:49:39 PM

Title: Trying to learn some C# Question about String.Compare
Post by: A_LOTA_NOTA on March 06, 2008, 01:49:39 PM
I'm sure this is something simple that I'm missing. This is an exaple I found for "Sting.Compare" but I can't get it to compile.

Code: [Select]
public class Test
{
// The program begains inside Main
      public static void Main()
      {
//code goes here

string s1 = "Jon";
string s2 = "Jonny";

  if (String.Compare (s1 , s2) < 0)
        System.Console.Out.WriteLine(s1 + " comes before " + s2);

       }// end of main
}// end of Test 

Thanks for the help!
Title: Re: Trying to learn some C# Question about String.Compare
Post by: T.Willey on March 06, 2008, 01:54:14 PM
C# is case sensitive, so String.Compare is not equal to string.Compare.

This might be better placed in the .Net forum.
Title: Re: Trying to learn some C# Question about String.Compare
Post by: A_LOTA_NOTA on March 06, 2008, 01:59:32 PM
C# is case sensitive, so String.Compare is not equal to string.Compare.

That did it!! Thanks!!
Title: Re: Trying to learn some C# Question about String.Compare
Post by: T.Willey on March 06, 2008, 02:42:23 PM
C# is case sensitive, so String.Compare is not equal to string.Compare.

That did it!! Thanks!!
Glad it worked, you're welcome.
Title: Re: Trying to learn some C# Question about String.Compare
Post by: Kerry on March 06, 2008, 03:06:51 PM
Actually String and string are functionally the same .. string is aliased from String ..

a piccy ... it compiles for me .
Title: Re: Trying to learn some C# Question about String.Compare
Post by: T.Willey on March 06, 2008, 03:18:39 PM
Actually String and string are functionally the same .. string is aliased from String ..

a piccy ... it compiles for me .
You are correct.  I just tried it in one of my routines, and it compiled also.  I don't know why it worked for the OP then.  Maybe it fixed something else in the code.  Thanks for the heads up Kerry.
Title: Re: Trying to learn some C# Question about String.Compare
Post by: A_LOTA_NOTA on March 10, 2008, 11:36:44 AM
Actually String and string are functionally the same .. string is aliased from String ..

a piccy ... it compiles for me .
You are correct.  I just tried it in one of my routines, and it compiled also.  I don't know why it worked for the OP then.  Maybe it fixed something else in the code.  Thanks for the heads up Kerry.

I changed the "S" in String.Compare to lower case & it compiles. Then I change it back to upper case I get "Test.cs(11,7): error CS0103: The name 'String' does not exist in the current context
Title: Re: Trying to learn some C# Question about String.Compare
Post by: SomeCallMeDave on March 10, 2008, 11:44:11 AM
Perhaps try adding
     using System;


or using fully qualified name System.String.Compare.....



Title: Re: Trying to learn some C# Question about String.Compare
Post by: Glenn R on March 10, 2008, 04:57:25 PM
As has been said above, C# is a CASE SENSITIVE language and also to add a 'using' directive for 'System' in your code, as your use of System.Console.Out.Writeline indicates you have not.

However, it goes a bit deeper than has already been said. C# defines aliases for the CTS's (Common Type System) datatypes. So, for example, 'int' in C# is actually an alias for the CTS type System.Int32 as is 'string' an alias for System.String.

So, as Dave mentioned above, you would have to have a using declaration for System (using System;) at the head of your code file, OR, use fully qualified CTS types in your code (which I would NOT recommend).

If you were to NOT use the using system declaration, you would have to write System.String.Compare(yada yada)...which will make your code harder to read.

Cheers,
Glenn.