0

I'm trying to create a dictionary of actions and then run them in a loop. This question helped, but I still get a compile error when adding the action to the dictionary:- No overload for 'Action' matches delegate 'Action'.

Thanks for any help.

Dictionary<string, Action> dActions = new Dictionary<string, Action>();
            // do the actions need to be created?
            Action<string, int> actSpot = new Action<string, int>(oBSM.Spot);
            Action<string, int> actDelta = new Action<string, int>(oBSM.Delta);
            dActions["Spot"] = new Action(actSpot);
            dActions["Delta"] = new Action(actDelta);

            // or add action to dictionary?
            dActions.Add("Spot", oBSM.Spot(string BookOrComp, int DP);
            dActions.Add("Delta", oBSM.Delta(string BookOrComp, int DP);

            foreach (var BookOrComp in ListBookOrComp)
            {
                foreach (string Key in dActions.Keys)
                {
                    for (int DP = 1; DP <= 21; DP++)
                    {
                        dActions[Key]();
                    }
                }
            }

Update: I still get a couple of compile errors as show in the code

Dictionary<string, Action> dActions = new Dictionary<string, Action>();
            // create actions
            Action<string, int> actSpot = new Action<string, int>(oBSM.Spot);  
            Action<string, int> actDelta = new Action<string, int>(oBSM.Delta);  
            dActions["Spot"] = new Action(actSpot); // no overload for Action matches delegate Action
            dActions["Delta"] = new Action(actDelta); // ditto


            foreach (var BookOrComp in ListBookOrComp)
            {
                foreach (string Key in dActions.Keys)
                {
                    for (int DP = 1; DP <= 21; DP++)
                    {
                        dActions[Key](BookOrComp,DP);  // delegate Action  does not take 2 arguments
                    }
                }
            }
Community
  • 1
  • 1
Zeus
  • 1,496
  • 2
  • 24
  • 53

1 Answers1

2

I see a number of erros in your program:

1- Parenthesis are not balanced in:

        // or add action to dictionary?
        dActions.Add("Spot", oBSM.Spot(string BookOrComp, int DP);
        dActions.Add("Delta", oBSM.Delta(string BookOrComp, int DP);

You need to add a closing parenthesis there. Besides, I am not sure if that syntax is correct, I'd create an object and then add it to the dictionary.

2- The action takes two parameters one string and one int:

        Action<string, int> actSpot = new Action<string, int>(oBSM.Spot);
        Action<string, int> actDelta = new Action<string, int>(oBSM.Delta);

But you are calling it using now parameters at all:

         foreach (var BookOrComp in ListBookOrComp)
        {
            foreach (string Key in dActions.Keys)
            {
                for (int DP = 1; DP <= 21; DP++)
                {
                    dActions[Key](); // <<<-- where are the parameters?
                }
            }
        }

I think this is the error the compiler is complaining about.

Update 2:

Dictionary<string, Action> dActions = new Dictionary<string, Action>();

Should be defined as:

Dictionary<string, Action<string, int>> dActions 

= new Dictionary>();

And

        dActions["Spot"] = new Action(actSpot); // no overload for Action matches delegate Action

Should be

        dActions["Spot"] = actSpot; // actSpot already created with new Action...

or:

        dActions["Spot"] = new Action<string, int>(oBSM.Spot);

PS:

You must understand that when you do this:

        dActions["Delta"] = new Action(actDelta); // ditto

You are calling the constructor of Action with a parameter of type Action<string. int>, and Action does not have that constructor.

ichramm
  • 6,437
  • 19
  • 30
  • Thanks - stupid of me to miss the parenthesis. I agree to create the object and then add it to the dictionary. I tried this in the line ``dActions["Spot"] = new Action(actSpot);`` but got a compile error. Adding the parameters to the calling line also gave a compile error: ``dActions[Key](BookOrComp,DP);`` – Zeus Oct 13 '16 at 02:22
  • Juan, I followed your answer but still get compile errors - see my update. Can you take another look? – Zeus Oct 13 '16 at 05:02
  • The simple method of ``dActions.Add("Spot", actSpot); dActions.Add("Delta", actDelta);`` seems to be working. Is this the correct way of adding actions to a dictionary? – Zeus Oct 13 '16 at 07:18
  • yes, that works (it fails only if the key is already in the dictionary) – ichramm Oct 17 '16 at 14:42