tags:

views:

130

answers:

2

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

+1  A: 

Java has EnumSet. I can't speak for C#.

EJP
my implementation is in c#. will google the same. thanks.
Then I don't know why this question has a Java tag.
EJP
@EJP hope u realise that you can have a similar implementation case in java right and as you pointed out this can be acheived using a keyword in java.. i dont mark answers based on first cum first serve unfortunately.. i look at the various answers and then mark them, if that is what is upsetting you..
also if I am not mistaken dtbs soln is more generalistic which was exactly what i was looking for. you can use the same logic in Java if i am not mistaken.
There are many solutions. I answered your question with a Java answer because your question was tagged that way. I didn't read your mind either.
EJP
+4  A: 

In .NET, sets of enum values are usually realized using a Flags enumeration:

[Flags]
public enum ParentDepartments
{
    None = 0x0,
    Electrical = 0x1,
    Mechanical = 0x2,
    Biology = 0x4,
}

public static ParentDepartments GetParentDepartments(Course course)
{
    var departments = ParentDepartments.None;

    if (course.ElectricalStudents.Any())
        departments |= ParentDepartments.Electrical;

    if (course.ComputerStudents.Any())
        departments |= ParentDepartments.Electrical;

    if (course.MechanicalStudents.Any())
        departments |= ParentDepartments.Mechanical;

    if (course.BioengineeringStudents.Any())
        departments |= ParentDepartments.Biology;

    return departments;
}
dtb
nice I like this.. dint know about this. Thanks. will give others a chance to respond and then will choose an answer..
is having None a necessity?
Having a 0 value in an enum is strongly advised. "However, we strongly recommend that an enum contain a constant with a value of 0." http://msdn.microsoft.com/en-us/library/sbbt4032.aspx
asawyer
Love your solution. +1
Juan Nunez