locked
Switch question RRS feed

  • Question

  • User218791533 posted

    How would you write this in C#?

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


    Thanks

    Saturday, September 13, 2014 5:26 PM

Answers

  • User281315223 posted

    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;
    }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, September 13, 2014 5:44 PM

All replies

  • User281315223 posted

    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;
    }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, September 13, 2014 5:44 PM
  • User-434868552 posted

    @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  91
    Char 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
    Monday, September 15, 2014 3:08 PM