2017-03-28 3 views
0

過去の質問スレッドとリンクは、2つのstringの値を保持することができますので、私はパラメータのクラスと私の辞書を作成するコードHEREDictionary TryGetValueでは、クラスの一部である場合にoutパラメータを記述して(値を取得する)方法を教えてください。

fullpastします。今、私はTryGetValueこのクラス内の文字列の両方outに書き込みをしようとしている:あなたが見ることができるように

public class DictionaryInitializer 
{ 
    public class DictionarySetup 
    { 
     public string theDescription { get; set; } 
     public string theClass { get; set; } 
    } 

を、DictionarySetupにネストtheDescriptiontheClassがあります。 、そして、私は辞書に私TryGetValueを使用する予定の私の拡張メソッド

public class DictionaryInit 
    { 
     //IS_Revenues data 
     public Dictionary<int, DictionarySetup> accountRevenue = new Dictionary<int, DictionarySetup>() 
      { 
       { 400000, new DictionarySetup {theDescription="Call", theClass="Revenues"}} 
      }; 
     public Dictionary<int, DictionarySetup> accountExpenses = new Dictionary<int, DictionarySetup>() 
      { 
       {790100, new DictionarySetup { theDescription="Currency Hedge", theClass="Other income/expense"}} 
      }; 
    } 

:その後、私はここに、そのクラスを使用して辞書を作成し

public void DictionaryUseKey(int MapCode, int MapKey, int rowindex, Dictionary<int, DictionarySetup> AccountLexicon) 
    { 
     AccountLexicon[1] = new DictionarySetup(); 
     DictionarySetup Classes; 
     DictionarySetup Descriptions; 
     //Saw the above code in another thread, not sure if it's what I should be doing but it seems pretty close to what I want, however, I don't know how to specify the DictionarySetup.theDescription for example; 
     AccountLexicon.TryGetValue(MapKey, out Classes); 
     { 
      //I want to be able to write theDescription and theClass into string variables for use below if the `TryGetValue` returns true, but it seems to me that it can only out one value? How does this work? 
      DGVMain.Rows[rowindex].Cells[3].Value = ?? how do I write something like... theValues.theDescription; 
      DGVMain.Rows[rowindex].Cells[11].Value = ?? how do I write something like... theValues.theClass; 
     } 
    } 

最後に、私は私の中に拡張メソッドを呼び出しますイベントそう様:実際

private void btnMapper_Click(object sender, EventArgs e) 
    { 
     for (int rowindex = 0; rowindex < DGVMain.RowCount; rowindex++) 
     { 
      int accountKey = Convert.ToInt32(DGVMain.Rows[rowindex].Cells[2].Value); 
      int projCode = Math.Abs(Convert.ToInt32(DGVMain.Rows[rowindex].Cells[7].Value)); 
      int deptCode = Math.Abs(Convert.ToInt32(DGVMain.Rows[rowindex].Cells[9].Value)); 
      int AbsoluteKey = Math.Abs(accountKey); 
      while (AbsoluteKey >= 10) { AbsoluteKey /= 10; } 
      while (deptCode >= 10) { deptCode /= 10; } 
      theDictionary = new DictionaryInit(); 
      DictionaryUseKey(deptCode, accountKey, theDictionary.accountRevenue); 
     } 
    } 
+1

私はあなたの質問にユーザー定義の拡張メソッドは表示されません。 – Amy

+1

out-parameters(およびそのためのref-parameters)を持つ変数のみを使用できます。あなたの言葉からは、代わりにプロパティを使用しようとしているようです。 –

+2

@Amy申し訳ありませんが、私はこれらが正確に何と呼ばれているのか分かりません。機能?ちょうど方法?拡張メソッドという用語は、私が上記で書いたような例で捨てられました:DictionaryUseKey(deptCode、accountKey、theDictionary.accountRevenue); – Arvayne

答えて

2

、ブール値が指定されたキーの存在を表すキーが見つかった場合、CORRを返す意志TryGetValue方法その値はoutパラメーターに格納されます。ここであなたのケースでは、出力パラメータはClassesであり、あなたのコードにはDictionarySetup Classesのように定義されています。つまり、キーが辞書にある場合は、対応するDictionarySetupオブジェクトはClassesに格納されますので、theDescriptiontheClassにはClassesからアクセスできます。以下のコードを検討してください:

if(AccountLexicon.TryGetValue(MapKey, out Classes)) 
{ 
    DGVMain.Rows[rowindex].Cells[3].Value = Classes.theDescription; 
    DGVMain.Rows[rowindex].Cells[11].Value = Classes.theClass; 
} 
+1

ああ、私は正しい軌道に乗っていたようだ。あなたの提案をテストしましょう。 – Arvayne

関連する問題