這裏是我的代碼:返回帶有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;
}
}
「出DeveloperDetails」 是無效的。你需要給它一個輸出到的對象。 DeveloperDetails dd = null; if(details.TryGetValue ...) 以上應該工作 – oppassum