0

My below method is throwing an error:

Error 205 An object reference is required for the non-static field, method, or property 'System.Collections.DictionaryEntry.Key.get

 static string GetConfigValue(NameValueCollection tempCollection)
    {

        string paymentDisposition = string.Empty;

        Hashtable tempCollectionHash = new Hashtable();
        foreach (string key in tempCollection.Keys)
        {
            tempCollectionHash.Add(key, tempCollection[key]);
        }


        if(tempCollectionHash.ContainsKey("Cancellation"))
            paymentDisposition = (string)tempCollectionHash["Cancellation"];

        foreach (DictionaryEntry entry in tempCollectionHash)
        {
           if (DictionaryEntry.Key.Contains(" Cancellation"))
           {
               paymentDisposition = DictionaryEntry.Value.;
           }
        }

        return paymentDisposition;

    }

any idea why it's throwing error.

Ferhat Sayan
  • 216
  • 1
  • 4
  • 19
user1104946
  • 680
  • 1
  • 9
  • 30
  • what line throws the error? – Jacobr365 Feb 29 '16 at 14:47
  • 5
    `if (**entry**.Key.Contains(" Cancellation"))`. Not `DictionaryEntry`. You're using the class name instead of the variable. Same on `Value`. – D Stanley Feb 29 '16 at 14:48
  • Good catch @DStanley – Pikoh Feb 29 '16 at 14:49
  • Yes those two lines are giving error – user1104946 Feb 29 '16 at 14:50
  • Why are you turning your collection into a hash table anyways? You could just scan through it looking for a match and it would perform just as well if not faster. Using a hash table only helps if you're searching for multiple values. Or you could pass in a _dictionary_ that's already hashed by key. – D Stanley Feb 29 '16 at 14:53
  • Collection does not have method for contains so i am converting it to hash table – user1104946 Feb 29 '16 at 14:55
  • If you are doing a foreach anyway, you could check if the collection contains the value there, no need to use a hashtable :) – Pikoh Feb 29 '16 at 15:02

0 Answers0