C# Iterate over Hashtable

Hi Experts,

When I use the following code to iterate over a Hashtable I get this error:   "Unable to cast object of type 'System.Collections.DictionaryEntry' to type 'MyApplication.BLL.CategorySubType'."

        Hashtable categories = objComplaintSingleton.getCategorySubTypes();
        ComplaintCategory objComplaintCategory;
        foreach (CategorySubType objCategorySubType in categories)
        {
            objComplaintCategory = new ComplaintCategory();
            objComplaintCategory.ComplaintID = complaintId;
            objComplaintCategory.CategorySubTypeID = objCategorySubType.Id;
            objComplaintCategory.CategoryTypeID = objCategorySubType.CategoryTypeId;
            objComplaintCategory.Save();
            objComplaintCategory = null;
        }

Note the Hashtable holds string as a key and an object of type CategorySubType as the value.

Thanks.
simonm_jpAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jesse HouwingScrum Trainer | Microsoft MVP | ALM Ranger | ConsultantCommented:
use
 foreach (CategorySubType objCategorySubType in categories.Values)

to iterate over each value stored.

 There's also a property Keys which allows you to iterate over the keys.
ThogekCommented:
The value references within a Hashtable are actually of type System.Collections.DictionaryEntry, so you may need to iterate over the values using this type, and then cast each one as needed.  E.g., try something like

    Hashtable categories = objComplaintSingleton.getCategorySubTypes();
    ComplaintCategory objComplaintCategory;
    foreach (System.Collections.DictionaryEntry dictEntry in categories)
    {
        CategorySubType objCategorySubType = (CategorySubType)dictEntry;
        objComplaintCategory = new ComplaintCategory();
        objComplaintCategory.ComplaintID = complaintId;
        objComplaintCategory.CategorySubTypeID = objCategorySubType.Id;
        objComplaintCategory.CategoryTypeID = objCategorySubType.CategoryTypeId;
        objComplaintCategory.Save();
        objComplaintCategory = null;
    }

More specifics about the System.Collections.Hashtable class at http://msdn.microsoft.com/library/en-us/cpref/html/frlrfsystemcollectionshashtableclasstopic.asp
Jesse HouwingScrum Trainer | Microsoft MVP | ALM Ranger | ConsultantCommented:
@Thogek  that won't work.

If dictEntry is of type DictionaryEntry, it will never be castable to CategorySubType.

Your code will work as follows:

    Hashtable categories = objComplaintSingleton.getCategorySubTypes();
    ComplaintCategory objComplaintCategory;
    foreach (System.Collections.DictionaryEntry dictEntry in categories)
    {
        CategorySubType objCategorySubType = (CategorySubType)dictEntry.Value; // CHANGED HERE
        objComplaintCategory = new ComplaintCategory();
        objComplaintCategory.ComplaintID = complaintId;
        objComplaintCategory.CategorySubTypeID = objCategorySubType.Id;
        objComplaintCategory.CategoryTypeID = objCategorySubType.CategoryTypeId;
        objComplaintCategory.Save();
        objComplaintCategory = null;
    }

But this is simpler and will work even faster:

    Hashtable categories = objComplaintSingleton.getCategorySubTypes();
    ComplaintCategory objComplaintCategory;
    foreach (CategorySubType objCategorySubType in categories.Values)
    {
        objComplaintCategory = new ComplaintCategory();
        objComplaintCategory.ComplaintID = complaintId;
        objComplaintCategory.CategorySubTypeID = objCategorySubType.Id;
        objComplaintCategory.CategoryTypeID = objCategorySubType.CategoryTypeId;
        objComplaintCategory.Save();
        objComplaintCategory = null;
    }

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
simonm_jpAuthor Commented:
Doh! ... should have picked that one up myself.  

Thanks ToAoM.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.