Advertising (This ad goes away for registered users. You can Login or Register)

C# Help...

Programming on your favorite platform, for your favorite platform? Post here
Post Reply
Nickolas
Posts: 174
Joined: Sat Jan 22, 2011 3:14 pm
Location: In a black hole...

C# Help...

Post by Nickolas » Mon Sep 12, 2011 5:33 pm

I need help on a (C#) switch statement. I want to excecute the same code if a char is a letter (a-z, A-Z but not a number or a symbol).

I've thought of this.

Code: Select all

switch (ch) {
   case 'a':
      //My code...
      break;
   case 'b':
      //My code...
      break;      
//And so on...
}     
 
Since I want the same code to be excecuted for all letters is there an easier way to write this?
Advertising
Image
Image
Image
Image

User avatar
m0skit0
Guru
Posts: 3817
Joined: Mon Sep 27, 2010 6:01 pm

Re: C# Help...

Post by m0skit0 » Mon Sep 12, 2011 5:56 pm

I have no idea about C#, but meh...

Classic way:

Code: Select all

if ((ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z'))
{
    // Your code
}
Or using Regex class you can use regular expressions, that is:

Code: Select all

Match match = Regex.Match(ch, @"[a-z]", RegexOptions.IgnoreCase);
if (match.Success)
{
    // Your code
}
I think with regexps you need to use char strings, but I'm not sure since I've never used C#.
Advertising
I wanna lots of mov al,0xb
Image
"just not into this RA stuffz"

Nickolas
Posts: 174
Joined: Sat Jan 22, 2011 3:14 pm
Location: In a black hole...

Re: C# Help...

Post by Nickolas » Tue Sep 13, 2011 9:19 am

Oh, so I can use '<' and '>' with letters? I didn't know that. Thanks! I don't want to use the regex method because I don't know if it has been implemented in Mono. (I am thinking of porting my application to the Mono Framework...)

Anyway, I'll try it and tell you if it worked. :D
Image
Image
Image
Image

User avatar
m0skit0
Guru
Posts: 3817
Joined: Mon Sep 27, 2010 6:01 pm

Re: C# Help...

Post by m0skit0 » Tue Sep 13, 2011 9:39 am

Nickolas wrote:so I can use '<' and '>' with letters?
Well, I'm not 100% sure since I've never used C#, but most likely yes since almost all languages allow that (specially C and C++ in which syntax C# is inspired).
Nickolas wrote:I don't want to use the regex method because I don't know if it has been implemented in Mono
It's an external library, and yes it is: http://sourceforge.net/projects/deveel-regex/
I wanna lots of mov al,0xb
Image
"just not into this RA stuffz"

User avatar
varun
Banned
Posts: 651
Joined: Thu Jan 27, 2011 12:32 pm
Location: Mars

Re: C# Help...

Post by varun » Thu Sep 22, 2011 11:49 am

ur code should have worked

User avatar
m0skit0
Guru
Posts: 3817
Joined: Mon Sep 27, 2010 6:01 pm

Re: C# Help...

Post by m0skit0 » Thu Sep 22, 2011 12:44 pm

Dude, we already know his code works. Next time read the question before posting useless stuff:

Nickolas wrote:is there an easier way to write this?
I wanna lots of mov al,0xb
Image
"just not into this RA stuffz"

User avatar
varun
Banned
Posts: 651
Joined: Thu Jan 27, 2011 12:32 pm
Location: Mars

Re: C# Help...

Post by varun » Tue Oct 04, 2011 6:15 am

sorry my fault.
there is another way but i am not sure if it is eaier.- using conditional operator(?) and then nesting it

http://www.cplusplus.com/doc/tutorial/operators/

User avatar
m0skit0
Guru
Posts: 3817
Joined: Mon Sep 27, 2010 6:01 pm

Re: C# Help...

Post by m0skit0 » Tue Oct 04, 2011 6:58 am

I almost never use conditional operator, it's bad for legibility.
I wanna lots of mov al,0xb
Image
"just not into this RA stuffz"

Post Reply

Return to “Programming”