Hi guys,
You may have run into situations wherein you declare a certain enum and you want to evaluate a certain object to find out which enum categories it satisfies i.e an object may satisfy the conditions for it to be classified into more than one enum.
An example would be, say in an university i have various Course and this has the list of students attending from different specialization attending the same. Thus we have
public class Course
{
int Id;
IList<Electrical> ElectricalStudents;
IList<Computer> ComputerStudents;
IList<Bioengineering> BioengineeringStudents;
IList<Mechanical> MechanicalStudents;
}
and my enum is as follows
public enum ParentDepartment
{
Electrical,
Mechanical,
Biology
}
and for every course object I have I need to identify which parentdepartments the students fall into so that I can use them appropriately( so that i can mail those respective departments)
How would you handle such a case? What is the best way to go about the same.
The way i can think of is have a static function on Course object whose definition is something like this
public static IList<ParentDepartment> GetParentDepartments(Course course)
{
public parentDepartmentList=new List<ParentDepartment>();
if(ElectricalStudents.Count>0)
AddToList(parentDepartmentList,ParentDepartment.Electrical);
if(ComputerStudents.Count>0)
AddToList(parentDepartmentList,ParentDepartment.Electrical);
if(MechanicalStudents.Count>0)
AddToList(parentDepartmentList,ParentDepartment.Mechanical);
if(BioengineeringStudents.Count>0)
AddToList(parentDepartmentList,ParentDepartment.Biology);
}
private void AddToList(IList<ParentDepartment> parentDepartmentList,ParentDepartment parentdept)
{
if(!parentDepartmentList.Contains(parentdept))
parentDepartmentList.Add(parentdept);
}
Hope the things are clear. Please note this an hypothetical model and please avoid giving me solution like changing the model.
Any improvement to the functions is also greatly appreciated. I just dont like the way things have been impelemented here. Is there a leaner and better method to handle the same?
Any answer is greatly appreciated.
P.S: Sorry for making this C# specific but I think the concept can be extended over to any language. Thanks