Baz1nga
Baz1nga

Reputation: 15579

Working with enums(constructing a list)

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)
            Add(parentDepartmentList,ParentDepartment.Electrical);
    if(ComputerStudents.Count>0)
            Add(parentDepartmentList,ParentDepartment.Electrical);
    if(MechanicalStudents.Count>0)
            Add(parentDepartmentList,ParentDepartment.Mechanical);
    if(BioengineeringStudents.Count>0)
            Add(parentDepartmentList,ParentDepartment.Biology);
}

private void Add(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

Upvotes: 3

Views: 348

Answers (2)

dtb
dtb

Reputation: 217253

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;
}

Upvotes: 4

user207421
user207421

Reputation: 310860

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

Upvotes: 1

Related Questions