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
}
}
}