## 2 replies

Last post Sep 15, 2014 03:08 PM by gerrylowry

• Confused Dav...

Member

24 Points

205 Posts

### Switch question

Sep 13, 2014 05:26 PM|Confused Dave|LINK

How would you write this in C#?

```Select Case Asc(tChr)
Case 65 To 90, 97 To 122, 48 To 57
End Select```

Thanks

• Rion William...

All-Star

114593 Points

18503 Posts

MVP

### Re: Switch question

Sep 13, 2014 05:44 PM|Rion Williams|LINK

C# doesn't support ranges for switch statements like Visual Basic does.

The easiest approach might be to use an explicit if-statement that checks to see if it falls into your specific range. Additionally, the Asc() function returns an integer that corresponds to a certain character, which you could use by just converting the character to an integer as seen below :

```// Get your integer value
var x = Convert.ToInt32(tChr);

// Determine if it falls into your range
if(x >= 65 && x <= 90)
{
// Case 1: 65 - 90
}
else if(x >= 97 && x <= 122)
{
// Case 2: 97 - 122
}
else if(x >= 48 && x <= 57)
{
// Case 3: 48 - 57
}```

or if you needed to trigger an event for any of these, just use :

```// Get your integer value
var x = Convert.ToInt32(tChr);

// Determine if it falls into a range
if((x >= 65 && x <= 90) || (x >= 97 && x <= 122) || (x >= 48 && x <= 57))
{
// It falls into one of these ranges
}```

Finally, if you wanted a more exact conversion, it would simply require you to list out all of the ranges similar to below :

```switch(Convert.ToInt32(tChr))
{
case 48:
// Enumerate until 56 here
case 57:
// Cases 48 - 57
break;
case 65:
// Enumerate until 89 here
case 90:
// Cases 65 - 90
break;
case 97:
// Enumerate until 111 here
case 112:
// Cases 97 - 112
break;
}```

• gerrylowry

Star

14297 Points

5797 Posts

### Re: Switch question

Sep 15, 2014 03:08 PM|gerrylowry|LINK

@Confused Dav...  TIMTOWTDI =. there is more than one way to do it

Dave, you're simply trying to determine whether your value is a uppercase or lowercase letter, or a digit.

Better is to allow the MSDN Framework to do that for you.

"Char.IsLetterOrDigit Method (Char)" http://msdn.microsoft.com/en-us/library/cay4xx2f%28v=vs.110%29.aspx

Here's a simpler solution based on Char.IsLetterOrDigit:

```Char space = ' '; // Decimal  32
Char star  = '*'; // Decimal  42
Char slash = '/'; // Decimal  47
Char zero  = '0'; // Decimal  48
Char nine  = '9'; // Decimal  57
Char colon = ':'; // Decimal  58
Char at    = '@'; // Decimal  64
Char A     = 'A'; // Decimal  65
Char Z     = 'Z'; // Decimal  90
Char Lbrak = '['; // Decimal  91Char btick = '`'; // Decimal  96
Char a     = 'a'; // Decimal  97
Char z     = 'z'; // Decimal 122
Char curly = '{'; // Decimal 123```
```Console.WriteLine("[{0}] {1}", space, Char.IsLetterOrDigit(space));
Console.WriteLine("[{0}] {1}", star, Char.IsLetterOrDigit(star));
Console.WriteLine("[{0}] {1}", slash, Char.IsLetterOrDigit(slash));
Console.WriteLine("[{0}] {1}", zero,  Char.IsLetterOrDigit(zero));
Console.WriteLine("[{0}] {1}", nine,  Char.IsLetterOrDigit(nine));
Console.WriteLine("[{0}] {1}", colon, Char.IsLetterOrDigit(colon));
Console.WriteLine("[{0}] {1}", at,    Char.IsLetterOrDigit(at));
Console.WriteLine("[{0}] {1}", A,     Char.IsLetterOrDigit(A));
Console.WriteLine("[{0}] {1}", Z,     Char.IsLetterOrDigit(Z));Console.WriteLine("[{0}] {1}", Lbrak, Char.IsLetterOrDigit(Lbrak));Console.WriteLine("[{0}] {1}", btick, Char.IsLetterOrDigit(btick));
Console.WriteLine("[{0}] {1}", a,     Char.IsLetterOrDigit(a));
Console.WriteLine("[{0}] {1}", z,     Char.IsLetterOrDigit(z));
Console.WriteLine("[{0}] {1}", curly, Char.IsLetterOrDigit(curly));```

output:

```[ ] False
[*] False
[/] False
[0] True
[9] True
[:] False
[@] False
[A] True
[Z] True[[] False
[`] False
[a] True
[z] True
[{] False```

But there's a catch ... the .NET Framework is a Unicode world:

```Char a2dot = 'ä'; // a with umlaut

Console.WriteLine("[{0}] {1}", a2dot, Char.IsLetterOrDigit(a2dot));```

output:

`[ä] True`

if there is any possibility that your data may contain letters like ä, ç, é, ...   et cetera, then here's a fix to stay within your range:

`Console.WriteLine("[{0}] {1}", a2dot, Char.IsLetterOrDigit(a2dot) && ((Int32)a2dot<123));`

output:

`[ä] False`
B-)  Gerry Lowry, Chief Training Architect, Paradigm Mentors Learning never ends... +1 705-999-9195 wasaga beach, ontario canada TIMTOWTDI =.there is more than one way to do it