2016-04-05 29 views
0

這裏是我的代碼:返回帶有out關鍵字的對象從詞典查找

namespace BingAdsDataImporter 
{ 
    class DeveloperDetails 
    { 

     public string ClientId { get; set; } 
     public string DeveloperToken { get; set; } 

    } 

    static class CollInit 
    { 
     public static Dictionary<string, DeveloperDetails> details = new Dictionary<string, DeveloperDetails>() 
     { 
      { "Clientnamehere", new DeveloperDetails {ClientId="xxx", DeveloperToken="xxxx"} } 
     }; 

     public static DeveloperDetails GetValue(string key) 
     { 
      if (details.TryGetValue(key, out DeveloperDetails)) // fails here Error 1 'BingAdsDataImporter.DeveloperDetails' is a 'type' but is used like a 'variable' 
      { 
       return dd; 
      } 
      else 
      { 
       return null; 
      } 

     } 
    } 
} 

如果這個失敗是靜態方法GetValue(字符串鍵)。 我想要返回一個DeveloperDetails對象或者可能爲該標記創建另一個方法,並更改此方法名稱以返回該鍵。我無法使用out關鍵字返回對象(儘管我不知道爲什麼),但我無法弄清楚如何從該對象返回特定的值,這是首選。任何幫助表示讚賞。

固定在這裏是我的新方法

public static DeveloperDetails GetObjectDetails(string key) 
    { 
     DeveloperDetails dd = null; 
     if (details.TryGetValue(key, out dd)) 
     { 
      return dd; 
     } 
     else 
     { 
      return null; 
     } 

    } 
    public static string GetClientId(string key) 
    { 
     DeveloperDetails dd = null; 
     if (details.TryGetValue(key, out dd)) 
     { 
      return dd.ClientId; 
     } 
     else 
     { 
      return null; 
     } 

    } 
    public static string GetDeveloperToken(string key) 
    { 
     DeveloperDetails dd = null; 
     if (details.TryGetValue(key, out dd)) 
     { 
      return dd.DeveloperToken; 
     } 
     else 
     { 
      return null; 
     } 

    } 
+0

「出DeveloperDetails」 是無效的。你需要給它一個輸出到的對象。 DeveloperDetails dd = null; if(details.TryGetValue ...) 以上應該工作 – oppassum

回答

1

StriplingWarrior的答案是你正在尋找的。

不過,如果你想返回從developerDetails只有一個值:

public static string GetValue(string key) 
{ 
    DeveloperDetails dd; 
    if (details.TryGetValue(key, out dd)) 
    { 
     return dd.ClientID; //or dd.DeveloperToken 
    } 
    else 
    { 
     return null; 
    } 

} 
+0

這兩個工作!非常感謝! – Radmation

3

out關鍵字需要一個變量來把價值分爲:

public static DeveloperDetails GetValue(string key) 
    { 
     DeveloperDetails dd; 
     if (details.TryGetValue(key, out dd)) 
     { 
      return dd; 
     } 
     else 
     { 
      return null; 
     } 
    } 

由於out參數將被留null如果該值不在字典中,這可以進一步簡化爲:

public static DeveloperDetails GetValue(string key) 
    { 
     DeveloperDetails dd; 
     details.TryGetValue(key, out dd); 
     return dd; 
    } 

或(在我寫了一個框架,一個無恥的插頭),你可以使用來自CallMeMaybe庫擴展:

public static DeveloperDetails GetValue(string key) 
    { 
     return details.GetMaybe(key).Else(() => null); 
    } 

如果你打算使用這個最後一種方法,你可以考慮完全取消GetValue方法,並使調用代碼利用Maybe<>類型。例如:

string clientId = details.GetValue(key).ClientId; // could throw null reference 

...成爲

Maybe<string> clientId = details.GetMaybe(key).Select(d => d.ClientId); 
+0

太棒了!謝謝。我簡化了我的代碼。 – Radmation

+0

我對看到你寫的圖書很感興趣:D我已經瞭解到'out'關鍵字需要一個變量聲明在上面,這對我來說很奇怪,但無論如何。 – Radmation

+2

我只是因爲你有一個名爲'CallMeMaybe'的庫而投票。你還有'ThisIsCrazy'和'HeresMyNumber'庫嗎? ;) – juharr

1

試試這個:

public static DeveloperDetails GetValue(string key) 
    { 
     DeveloperDetails dd = null; 
     if (details.TryGetValue(key, out dd)) 
     { 
      return dd; 
     } 
     else 
     { 
      return null; 
     } 

    } 

你給的DeveloperDetails是返回類型定義。但是需要傳遞一個變量名稱。

+0

這個工程!謝謝 – Radmation