I have following classes:
class Department
{
private string departmentId;
private string departmentName;
private Hashtable doctors = new Hashtable();//Store doctors for
//each department
public Hashtable Doctor
{
get { return doctors; }
}
}
I have an array list that holds department objects:
private static ArrayList deptList = new ArrayList();
public ArrayList Dept
{
get { return deptList; }
}
I am trying to get all the doctors(the Hashtable in department class) from each department:
foreach (Department department in deptList)
{
foreach (DictionaryEntry docDic in department.Doctor)
{
foreach (Doctor doc in docDic.Value)//this is where I gets an error
{
if (doc.ID.Equals(docID))//find the doctor specified
{
}
}
}
}
But I can not compile the program. It gives an Error:
foreach statement cannot operate on variables of type 'object' because
'object' does not contain a public definition for 'GetEnumerator'
You are trying to iterate through a dictionary entry’s
Valuefield treating it as if it was a collection ofDoctors. The iteration fordocDicshould already do what you are looking for, just need to cast the appropriate field (probablyValue) of thedocDicDictionaryEntry.Better yet, you could use generics and denote the types of the dictionary key/value at declaration of the map:
(similar change for the
Doctorfield)Then you don’t need the casting above at all.
Note: I assumed you are mapping from a doctor’s id (key) to the
Doctorobjects (value), and that the id is a string