Invalid Cast exception with hashtable

I have the following code.  expCollection is a hashtable that contains a collection of classes of type TRI.FarmSelect.Expense:

private void getBudgetItemPrices(Hashtable expCollection, int budgetID, int regionCode)
{
      /* Loop through expenses collection and for each expense find prices/costs and assign */
      foreach(TRI.FarmSelect.Expense expense in expCollection)
      {
            if (DB.getBudgetItemCost(Response, budgetID, regionCode, expense))
            {
                  /* do stuff... */
            }
      }
}

I get a runtime error "Specified cast is not valid" in the foreach statement.  I am positive that the expCollection contains classes of the Expense type.  Why would it give this runtime error? Thanks.
LVL 1
djs120Asked:
Who is Participating?
 
b1xml2Commented:
foreach(DictionaryEntry e in expCollection)
{
      TRI.FarmSelect.Expense item = e.Value as TRI.FarmSelect.Expense;
      if (item != null)
      {
            / * do stuff */
      }
}

or
foreach(TRI.FarmSelect.Expense expense in expCollection.Values)
{

}
0
 
djs120Author Commented:
Second suggestion worked like a charm (didn't try the first one).  Thanks!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.