1 Replies - 156 Views - Last Post: 15 February 2020 - 07:42 AM Rate Topic: -----

#1 sonar87   User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 82
  • Joined: 04-February 08

How to call method of key in Dictionary<string, delegatetype>

Posted 14 February 2020 - 09:56 PM

I have tried the following based on what I did see available in the vs drop down list, and have tried normal invoke as well, but when code is ran and hits that point I am told "An item with the same key has already been added."

public delegate bool action(string[] input);
public static Dictionary<string, action> actionsList = new Dictionary<string, action>()
{
  { "key", MethodName },
  ...
}

public bool MethodName(string[] input)
{
    ...
    return true;
}




actionsList["key"].DynamicInvoke(inputArray);


tried also:

actionsList["key"](input);


but same error.

How do you call the stored method?

Is This A Good Question/Topic? 0
  • +

Replies To: How to call method of key in Dictionary<string, delegatetype>

#2 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7239
  • View blog
  • Posts: 24,542
  • Joined: 05-May 12

Re: How to call method of key in Dictionary<string, delegatetype>

Posted 15 February 2020 - 07:42 AM

The easiest way to invoke the delegate is the way you wrote actionsList["key"](input);.

The "An item with the same key has already been added." error you are getting is with regards to populating the dictionary, not when you are trying to read and use items in the dictionary. Instead of just running the code, actually use your debugger to see where the exception is being thrown.
Was This Post Helpful? 2
  • +
  • -

Page 1 of 1