方式:
代码
bool success = false;
foreach (Employee emp in employees)
{
while (emp.Busy != true || success == true)
{
if (emp.Busy == false && emp.Skills.ToString() == _skillRequired.ToString() && success == false)
{
emp.EmployeeWorkload = _jobName;
emp.ShiftsLeft = _shiftsLeft;
emp.Busy = true;
success = true;
}
else if (emp.Busy == true)
{
MessageBox.Show("Sorry there is no one available for this job");
}
问题
[Flags]
public enum MemberSkills { None = 0, CSharp = 1 << 0, SQL = 1 << 1, PHP = 1 << 2, Javascript = 1 << 3, Web = 1 << 4, Python = 1 << 5, Oracle = 1 << 6, CPlus = 1 << 7, Perl = 1 << 8 };
Employee e1 = new Employee(MemberJob.Employee, "Name", MemberSkills.CPlus | MemberSkills.CSharp, false);
Employee e2 = new Employee(MemberJob.Employee, "Another Name", MemberSkills.CSharp, false);
因为对象包含一个或多个MemberSkills
,所以if语句将对象读作“CSharp | CPlus”并且无法正确执行该方法。而是在他们的技能组中选择具有CSharp的员工。见e2
答案 0 :(得分:3)
Enum
课程中有一个名为HasFlag
的方法可以满足您的需求:
var _skillRequired = MemberSkills.CSharp;
if (emp.Skills.HasFlag(_skillRequired))
{
...
}
如果由于某种原因您无法更改skillRequired
,则您需要将该值解析为枚举:
var _skillRequiredAsEnum = (MemberSkills)Enum.Parse(typeof(MemberSkills), _skillRequired));
答案 1 :(得分:2)
由于枚举是Flags,你可以使用枚举的.HasFlag方法来检查是否设置了标志。
或者以旧的方式;你可以使用(emp.Skills&amp; _skillRequired)== _skillRequired查看是否设置了所有必需的标志。