Author Topic: Trying to learn some C# Question about String.Compare  (Read 4827 times)

0 Members and 1 Guest are viewing this topic.

A_LOTA_NOTA

  • Guest
Trying to learn some C# Question about String.Compare
« 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!

T.Willey

  • Needs a day job
  • Posts: 5251
Re: Trying to learn some C# Question about String.Compare
« Reply #1 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.
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.

A_LOTA_NOTA

  • Guest
Re: Trying to learn some C# Question about String.Compare
« Reply #2 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!!

T.Willey

  • Needs a day job
  • Posts: 5251
Re: Trying to learn some C# Question about String.Compare
« Reply #3 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.
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Trying to learn some C# Question about String.Compare
« Reply #4 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 .
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

T.Willey

  • Needs a day job
  • Posts: 5251
Re: Trying to learn some C# Question about String.Compare
« Reply #5 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.
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.

A_LOTA_NOTA

  • Guest
Re: Trying to learn some C# Question about String.Compare
« Reply #6 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

SomeCallMeDave

  • Guest
Re: Trying to learn some C# Question about String.Compare
« Reply #7 on: March 10, 2008, 11:44:11 AM »
Perhaps try adding
     using System;


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



« Last Edit: March 10, 2008, 11:47:32 AM by David Blackmon »

Glenn R

  • Guest
Re: Trying to learn some C# Question about String.Compare
« Reply #8 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.