1

Please explain why dictionary's 'getAt' method fails

List<BString> infoKeys = new List<BString>(infoDict.Keys); 
if (infoKeys.Contains(TorrentFileKeyWords.FILES_KEY) == true) //"files"
{   
        List<BaseType> multiFiles = ((BList)dict[TorrentFileKeyWords.FILES_KEY]).Value; <<< this fails

So infoDict is a Dictionary<String, BString> Contains on infoDict.Keys is used to find a specific item (of type BString) But line 4 fails... doesnt have sens

I am not used with c#.. so what methods do I have to override (now i have: GetHashCode, ==, != & equals)

Arion
  • 31,011
  • 10
  • 70
  • 88
pulancheck1988
  • 2,024
  • 3
  • 28
  • 46
  • 6
    What error are you getting? – Douglas Apr 05 '12 at 19:51
  • 2
    You haven't presented enough code to help you properly, and you haven't presented the error either. Please read http://tinyurl.com/so-hints. – Jon Skeet Apr 05 '12 at 19:54
  • What are the types really? If `infoDict` was a `Dictionary`, then `infoDict.Keys` would be a collection of `String`, not a collection of `BString`. – Guffa Apr 05 '12 at 19:56

2 Answers2

3

You shouldn’t need to copy your Keys to a new list to perform the lookup. In fact, you can check whether the key is present in the dictionary and retrieve its associated value in a single operation using the TryGetValue method:

BList bList;
if (dict.TryGetValue(TorrentFileKeyWords.FILES_KEY, out bList))
{
    List<BaseType> multiFiles = bList.Value;
    // use multiFiles here
}
Douglas
  • 53,759
  • 13
  • 140
  • 188
2

I suspect the problem is that you're using infoDict in one place, and dict in another...

It's not clear why you're creating a list from the keys of infoDict rather than just calling ContainsKey, or (better) using TryGetValue to start with. Additionally, I would advise against a "B" prefix for your type names.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194