2017-03-28 1 views
0

과거 질문 스레드 및 링크는 두 string 값을 저장할 수 있도록 내가 매개 변수에 대한 클래스 내 사전을 만드는 코드 HERE사전 TryGetValue에서 클래스의 일부인 경우 out 매개 변수를 쓰고 값을 가져 오는 방법은 무엇입니까?

fullpast합니다. 지금은이 클래스의 TryGetValue 문자열의 두 out에 쓰기 위해 노력하고있어 :

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

당신이 볼 수 있듯이, theDescriptiontheClassDictionarySetup에 중첩이있다. 그럼 난 여기에 클래스를 사용하여 사전을 만들 것입니다 :

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

을 그리고, 나는 사전에 내 TryGetValue를 사용하려면 내 확장 방법 :

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 매개 변수 (및 해당 참조 매개 변수)가있는 변수 만 사용할 수 있습니다. 당신의 말로는, 대신 당신이 속성을 사용하려는 것 같습니다. –

+2

@Amy 죄송합니다. 정확히 이러한 이름이 무엇인지 모르겠습니다. 기능? 그냥 방법? 용어 확장 메서드는 위에서 쓴 것과 같은 예제를 사용하여 던졌습니다. DictionaryUseKey (deptCode, accountKey, theDictionary.accountRevenue); – Arvayne

답변

2

, 부울 값이 지정된 키의 존재를 나타내는 키가 발견되면, CORR를 반환합니다 TryGetValue 방법 해당 값은 out 매개 변수에 저장됩니다. 여기에 귀하의 경우 out 매개 변수는 Classes이고 다음과 같이 코드에 정의되어 있습니다 : DictionarySetup Classes. 즉, 사전에 키가있는 경우 해당 DictionarySetup 개체는 Classes에 저장되므로 theDescriptiontheClassClasses에서 액세스 할 수 있습니다. 아래 코드를 고려하십시오.

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

아, 그래서 제대로 된 것 같습니다. 너의 제안을 시험해 보자. – Arvayne