simonm_jp
asked on
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.Dictio naryEntry' to type 'MyApplication.BLL.Categor ySubType'. "
Hashtable categories = objComplaintSingleton.getC ategorySub Types();
ComplaintCategory objComplaintCategory;
foreach (CategorySubType objCategorySubType in categories)
{
objComplaintCategory = new ComplaintCategory();
objComplaintCategory.Compl aintID = complaintId;
objComplaintCategory.Categ orySubType ID = objCategorySubType.Id;
objComplaintCategory.Categ oryTypeID = objCategorySubType.Categor yTypeId;
objComplaintCategory.Save( );
objComplaintCategory = null;
}
Note the Hashtable holds string as a key and an object of type CategorySubType as the value.
Thanks.
When I use the following code to iterate over a Hashtable I get this error: "Unable to cast object of type 'System.Collections.Dictio
Hashtable categories = objComplaintSingleton.getC
ComplaintCategory objComplaintCategory;
foreach (CategorySubType objCategorySubType in categories)
{
objComplaintCategory = new ComplaintCategory();
objComplaintCategory.Compl
objComplaintCategory.Categ
objComplaintCategory.Categ
objComplaintCategory.Save(
objComplaintCategory = null;
}
Note the Hashtable holds string as a key and an object of type CategorySubType as the value.
Thanks.
The value references within a Hashtable are actually of type System.Collections.Diction aryEntry, 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.getC ategorySub Types();
ComplaintCategory objComplaintCategory;
foreach (System.Collections.Dictio naryEntry dictEntry in categories)
{
CategorySubType objCategorySubType = (CategorySubType)dictEntry ;
objComplaintCategory = new ComplaintCategory();
objComplaintCategory.Compl aintID = complaintId;
objComplaintCategory.Categ orySubType ID = objCategorySubType.Id;
objComplaintCategory.Categ oryTypeID = objCategorySubType.Categor yTypeId;
objComplaintCategory.Save( );
objComplaintCategory = null;
}
More specifics about the System.Collections.Hashtab le class at http://msdn.microsoft.com/library/en-us/cpref/html/frlrfsystemcollectionshashtableclasstopic.asp
Hashtable categories = objComplaintSingleton.getC
ComplaintCategory objComplaintCategory;
foreach (System.Collections.Dictio
{
CategorySubType objCategorySubType = (CategorySubType)dictEntry
objComplaintCategory = new ComplaintCategory();
objComplaintCategory.Compl
objComplaintCategory.Categ
objComplaintCategory.Categ
objComplaintCategory.Save(
objComplaintCategory = null;
}
More specifics about the System.Collections.Hashtab
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Doh! ... should have picked that one up myself.
Thanks ToAoM.
Thanks ToAoM.
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.