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;
}
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
Member
24 Points
205 Posts
Switch question
Sep 13, 2014 05:26 PM|Confused Dave|LINK
How would you write this in C#?
Thanks
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 :
or if you needed to trigger an event for any of these, just use :
Finally, if you wanted a more exact conversion, it would simply require you to list out all of the ranges similar to below :
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:
output:
But there's a catch ... the .NET Framework is a Unicode world:
output:
if there is any possibility that your data may contain letters like ä, ç, é, ... et cetera, then here's a fix to stay within your range:
output: