As you can read in the Remarks section of the Hashtable
class, the objects you enumerate are DictionaryEntry
s. So you will have to rewrite it to something like:
foreach(DictionaryEntry s in keyPairs) {
//Is Section the Key?
if(s.Key == incomingSectionNameVariable) {
bExists = true;
break;
}
}
A DictionaryEntry
has a Key
and Value
element (that are of course the keys and the values in the Hashtable
. Both are Object
s since a Hashtable
is not generic and thus the compiler can not know what the type of the Key
and/or Value
is.
I advice you however to use a Dictionary<TKey,TValue>
since here you can specify the type of the Key
and Value
. In that case an example could look like:
private Dictionary<string,int> keyPairs = new Dictionary<string,int>();
foreach( KeyValuePair<string,int> kvp in keyPairs) {
//do something with kvp
}
But here kvp.Key
will be a string
so you don't have to cast it and it is safer to use.
keyPairs
can be casted toSectionPair
? Also, you should not use non-genericHashtable
. Move to genericHashSet<T>
to get compile-time type safety.