2016-10-13 9 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](); 
        } 
       } 
      } 

更新: 私はまだ

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

コードでショーとしてコンパイルエラーのカップルを取得し、私はあなたのプログラムにerrosの数を参照してください。

の1-カッコあります平衡していない:

 // 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](); // <<<-- where are the parameters? 
       } 
      } 
     } 

2 - アクションは二つのパラメータ1つの文字列と1つのint型取り:

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

をしかし、あなたは今使用して、それを呼び出しているが、すべてのパラメータ約不平を言っている。

アップデート2:

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

は、として定義されるべきである:=新しい辞書

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

>();

そして

 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"] =新しいアクション(actSpot)の行でこれを試しましたが、 ''コンパイルエラーが発生しました。 '' dActions [Key](BookOrComp、DP); '' – Zeus

+0

Juan、私はあなたの答えに従いましたが、コンパイルエラーが発生しました。私の更新を見てください。もう一度見てもらえますか? – Zeus

+0

"dActions"の簡単な方法です。Add( "Spot"、actSpot); dActions.Add( "Delta"、actDelta); ''は動作しているようです。これは辞書にアクションを追加する正しい方法ですか? – Zeus

関連する問題