tags:

views:

44

answers:

2

I have a hash table which stores IPs as strings for the key and an object of my creation in the corresponding value.

I keep getting an exception at foreach (DictionaryEntry info in MasterHash.Keys). More specifically it happens as the debugger gets to DictionaryEntry. I have tried to provoke a message from my try catch statement but the compiler does not like me trying to cast e to .ToString or .Message.

private void UpdateMap(Hashtable masterHash)
{
    try
    {
        foreach (DictionaryEntry info in masterHash.Keys)
        {
            AxShockwaveFlashObjects.AxShockwaveFlash axFlash = wfh.Child as AxShockwaveFlashObjects.AxShockwaveFlash;
            IPInstance foo = (IPInstance)info.Value;

            axFlash.CallFunction(foo.GetMarkerCall().ToString(SaveOptions.DisableFormatting));
        }
    }
    catch (Exception e)
    {
        Console.WriteLine(e.ToString());
    }
}
+1  A: 

It might be an invalid cast exception. masterHash.Keys is not a DictionaryEntry. The keys should be strings (your IP string).

EDIT:

ICollection MyKeys = MyTable.Keys;

foreach (object Key in MyKeys)
{
    Console.WriteLine(Key.ToString());
}
Joel Etherton
I tried Implementing the foreach with string instead of Dictionary entry. The IDE is now complaining: Error 1 'string' does not contain a definition for 'Value' and no extension method 'Value' accepting a first argument of type 'string' could be found (are you missing a using directive or an assembly reference?)How do I retrieve my objects while using string in the foreach?
Paul
Looking a little more closely, a hashtable's keys are enumerated as an ICollection. I'll edit a suggestion.
Joel Etherton
If you are using .net 3.0 or higher you can use `var` instead of object in your `foreach` statement.
juharr
A: 

I do not know why you are unable to get the values of the exception caught. However, you said that you

stores IPs as strings for the key

but instead you are looping through the keys but expect DictionaryEntry.

Try doing:

foreach(String s in masterHash.Keys)

or something like that instead.

KLee1
I tried Implementing the foreach with string instead of Dictionary entry. The IDE is now complaining: Error 1 'string' does not contain a definition for 'Value' and no extension method 'Value' accepting a first argument of type 'string' could be found (are you missing a using directive or an assembly reference?)
Paul
How do I retrieve my objects while using string in the foreach?
Paul