Thread: Efficiency with the switch() statement...

  1. #1
    Unregistered
    Guest

    Efficiency with the switch() statement...

    What I am trying to accomplish is simple; however, whenever I write this simple switch function, I can't help but ask whether or not there is a better way to do this:

    void __fastcall TForm1::Edit5KeyPress(TObject *Sender, char &Key)
    {
    switch (Key) {
    case '1' : ;
    case '2' : ;
    case '3' : ;
    case '4' : ;
    case '5' : ;
    case '6' : ;
    case '7' : ;
    case '8' : ;
    case '9' : ;
    case '0' : ;
    case '.' : ;
    case char(13) : ;
    case char(8) : ;
    default : Key = char(13);
    break;
    }
    }

    // Key is a direct variable of the typed key.

    Basically, as you can see, this only allows for numeric keys, the decimal key, [enter], and [backspace] to be used. Other keys register as the [enter] key, which does nothing in an edit box.

    In Delphi, this is what one would do:

    case Key of
    '0'..'9' : ;
    chr(13) ......... etc.

    That saved a bunch of typing. Sure it doesn't seem like much for something like this, but what if I wanted to do something similar where about 50 different things had to be disabled and 50 enabled? This would get tedious...

    What can I do?

  2. #2
    Unregistered
    Guest
    Errr I left a break statement out.

    The above code won't work; a break statement should have been inserted before the default statement.

  3. #3
    Registered User larry's Avatar
    Join Date
    Sep 2001
    Posts
    96
    I've read you can't do this in C++. You simply can't use something like case 1..3. It would be better to use if statements, e.g. if you need the same thing to be done for values from '0' to '9';
    Please excuse my poor english...

  4. #4
    Unregistered
    Guest
    Ahh, good idea. Thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Switch statement / default:
    By kcpilot in forum C Programming
    Replies: 4
    Last Post: 12-02-2008, 03:14 PM
  2. switch statement
    By guillermoh in forum C Programming
    Replies: 5
    Last Post: 03-10-2008, 02:17 PM
  3. Switch Statement
    By DarkDot in forum C++ Programming
    Replies: 11
    Last Post: 03-28-2007, 10:11 PM
  4. switch statement issues...(undeclared identifiers)
    By mero24 in forum C++ Programming
    Replies: 2
    Last Post: 02-19-2005, 08:05 PM
  5. Uh-oh! I am having a major switch problem!
    By goodn in forum C Programming
    Replies: 4
    Last Post: 11-01-2001, 04:49 PM