2016-10-13 6 views
0

조치 사전을 작성한 후 루프에서 실행하려고합니다. This 질문에 도움이되었지만 사전에 작업을 추가 할 때 컴파일 오류가 계속 발생합니다. - No overload for 'Action' matches delegate 'Action'.사전 작성 및 실행

감사합니다.

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](); 
        } 
       } 
      } 

업데이트 : 있습니다

1 괄호 : 나는 아직도

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

답변

2

내가 프로그램에서하는 오류의 수를 확인 코드에서 쇼와 같은 컴파일 오류의 몇 가지를 얻을 에 균등하지 않음 :

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

거기에 닫는 괄호를 추가해야합니다. 게다가 구문이 올바른지 나는 확신 할 수 없다. 객체를 만든 다음 사전에 추가 할 것이다.

2 동작은 두 개의 매개 변수를 하나의 문자열 하나 개 INT 취

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

을하지만 지금 사용하여 호출은 전혀 매개 변수 :

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

나는이 컴파일러 오류라고 생각합니다 에 관하여 불평하고있다.

업데이트 2 :) (>

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

= 새로운 사전;

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

같이 정의되어야 하는가

그리고

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

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

또는해야합니다

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

PS :

당신은 이해해야 당신이 수행 할 때

 dActions["Delta"] = new Action(actDelta); // ditto 
입력 Action<string. int>의 매개 변수를 Action의 생성자를 호출

Action는 생성자가 없습니다.

+0

감사합니다. 괄호를 놓치기를 바보입니다. 개체를 만든 다음 사전에 추가하는 데 동의합니다. 나는''dActions [ "Spot"] = new Action (actSpot)''라인에서 이것을 시도했지만 컴파일 에러가 발생했습니다. ''dActions [Key] (BookOrComp, DP);'' – Zeus

+0

Juan, 나는 당신의 답을 따라 갔지만 여전히 컴파일 오류가 발생했다. 나의 업데이트를 보아라. 다른 모습을 보여 주시겠습니까? – Zeus

+0

"dActions"의 간단한 방법입니다.추가 ("Spot", actSpot); dActions.Add ("Delta", actDelta);''작동하는 것 같습니다. 이것은 사전에 동작을 추가하는 올바른 방법입니까? – Zeus