Switch Statement: A constant value is expected

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • HaLo2FrEeEk
    Contributor
    • Feb 2007
    • 404

    Switch Statement: A constant value is expected

    I'm trying to make a switch statement that will handle hotkeys assigned to different functions in my application. The hotkeys will be user-changable, so I've got them saved as application settings. I have a class that allows the program to hook the keyboard and accept the hotkeys even if the program doesn't have focus. In the event handler for that hook I'm trying to create a switch statement that looks like this:

    Code:
    switch (e.KeyCode)
                {
                    case settings.hotkey1:
                        MessageBox.Show("Hot Key 1");
                        break;
                    case settings.hotkey2:
                        MessageBox.Show("Hot Key 2");
                        break;
                }
    But I'm getting an error:

    "A constant value is expected"

    I don't know what that means though. All I want is for the first case to fire if e.KeyCode equals the one I have saved for hotkey1, and the same for if e.KeyCode is hotkey2. How can I make it let me do that? I don't want to have a bunch of if statements, that would defeat the purpose of a switch statement existing.

    Any ideas?
  • Alex Papadimoulis
    Recognized Expert New Member
    • Jul 2010
    • 26

    #2
    You cannot accomplish this with a switch statement, and you will instead have to use an if/else construct.

    Code:
    if (e.KeyCode == settings.hotkey1)
      MessageBox.Show("Hot Key 1");
    else if (e.KeyCode == settings.hotkey2)
      MessageBox.Show("Hot Key 2");
    It's "just the way it is," as the switch statement requires that only integers (enums) and strings be used.


    ##
    As an aside, if anyone has any opinions/ideas as to why, I'd love to hear them. I do understand that there is an MSIL instruction called Switch, and that it jumps based off of a jump table... but the compiler of course can't use Switch if strings are used as cases. When that happens, it seems to generate an if/else block. Why can't the compiler do that for complex types?

    Comment

    • HaLo2FrEeEk
      Contributor
      • Feb 2007
      • 404

      #3
      So an Enum would work. Well...at runtime could I generate an enum with my settings values? There are only 2 hotkeys now, but I'd like to add more later. Is it possible to dynamically construct an Enum?

      Comment

      • GaryTexmo
        Recognized Expert Top Contributor
        • Jul 2009
        • 1501

        #4
        You can set up a hash table where the key pressed maps to an action, which is set up in an enum. Then you can switch on the action and do whatever...

        Code:
        enum Action
        {
          NoAction,
          Save,
          Load,
          Open
        }
        
        Hashtable keybinds = new HashTable();
        
        keybinds.Add(Keys.S, Action.Save);
        keybinds.Add(Keys.O, Action.Save);
        // .. and so on
        
        switch (keybinds[pressedKey])
        {
          case Action.Save:
            // do stuff
            break;
          // and so on
        }

        Comment

        • HaLo2FrEeEk
          Contributor
          • Feb 2007
          • 404

          #5
          But can I generate that Enum based on the data I have saved in the settings file? I'd like my users to be able to change the default hotkeys, which is why I'm using a settings file.

          Comment

          • GaryTexmo
            Recognized Expert Top Contributor
            • Jul 2009
            • 1501

            #6
            You don't generate the enum based on the data... the enum represents which action you take. You generate the hash mapping from a key to an action based on the data.

            Unless there's something more I'm not getting... do you know what the actions are beforehand? You just don't know what key does what, right?

            Comment

            • HaLo2FrEeEk
              Contributor
              • Feb 2007
              • 404

              #7
              Correct. I want the user to be able to set the hotkey to whatever they want, so I suppose you're right about generating the hashtable. Thanks.

              Comment

              Working...