• Advertisement
Sign in to follow this  

dictionary of delegates c# syntax

This topic is 3706 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I want to register a callback specific to a key, so i want to have a dictionary mapping keys to callbacks.
public delegate void KeyProc();
public Dictionary<Keys, KeyProc> keyprocs; 
//public Dictionary<Keys, event KeyProc> keyprocs; //Invalid token 'event' in class, struct, or interface member declaration

keyprocs.Add(Keys.PrintScreen, new KeyboardHook.KeyProc(OnPrintscreen));

if (keyprocs[key] != null)
{
    KeyProc proc = keyprocs[key];
    proc();
}

[edited: resolved one of the errors] [Edited by - thedustbustr on December 29, 2007 4:37:50 PM]

Share this post


Link to post
Share on other sites
Advertisement
Your test ("if keyprocs[key]") may be malformed. The indexer will throw a "key not found" exception if 'key' is not in the dictionary. If that isn't what you want, consider refactoring to

KeyProc proc;
if(keyprocs.TryGetValue(key,out proc))
proc();

Share this post


Link to post
Share on other sites
jpetrie is correct, i realized that later and forgot to edit. but my real question is, I want to use the commented out line (and declare my value using the 'event' keyword). The compiler won't let me.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement