[英]Using enums with objects c#
Sorry i'm quite new at enums and i'm trying to implement it in a logical context. 抱歉,我在枚举方面还很新,我正在尝试在逻辑上下文中实现它。 In my Employee class i've created some employee objects. 在员工类中,我创建了一些员工对象。 I've also created an enum for the Employee objects assignment status. 我还为Employee对象分配状态创建了一个枚举。 My aim is to create a list of employees and give each employee an assignment status and finally iterrate through a list printing out the employee data and the employee assignment status. 我的目的是创建一个雇员列表,并为每个雇员分配工作状态,最后遍历列表以打印出雇员数据和雇员分配状态。
I'm wondering: 我很好奇:
1 - Is this a good case to use enums? 1-使用枚举是否合适? 2 - How can i assign each employee with an assignment status? 2-如何为每个员工分配工作状态? For example, if i want Emp1 to have AssignmentStatus.Assigned how should i apply this syntax-wise? 例如,如果我希望Emp1具有AssignmentStatus.Assigned,则应如何在语法上应用此方法?
public class Employee
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public bool IsCurrentlyEmployed { get; set; }
enum AssignmentStatus
{
Assigned,
Idle,
Trainee,
NotDefined
}
public Employee(int id, string firstName, string lastName, bool isCurrentlyEmployed)
{
Id = id;
FirstName = firstName;
LastName = lastName;
IsCurrentlyEmployed = IsCurrentlyEmployed;
}
public Employee Employees()
{
Employee Emp1 = new Employee(1, "John", "Smith", true);
Employee Emp2 = new Employee(2, "Kevin", "Moore", true);
Employee Emp3 = new Employee(3, "Eric", "Johnson", false);
Employee Emp4 = new Employee(4, "Michell", "McDevour", true);
Employee Emp5 = new Employee(5, "Henry", "Jones", true);
Employee Emp6 = new Employee(6, "Sarah", "Holmes", true);
List<Employee> listEmployees = new List<Employee>();
}
Thnk you 谢谢
1 - Is this a good case to use enums? 1-使用枚举是否合适?
Sure 当然
2 - How can i assign each employee with an assignment status? 2-如何为每个员工分配工作状态?
You need to expose a public property of type AssignmentStatus
or create a constructor to do this 您需要公开AssignmentStatus
类型的公共属性或创建一个构造函数来执行此操作
For example: 例如:
public class Employee
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public bool IsCurrentlyEmployed { get; set; }
public AssignmentStatus Status { get; set; }
enum AssignmentStatus
{
Assigned,
Idle,
Trainee,
NotDefined
}
public Employee(int id, string firstName, string lastName, bool isCurrentlyEmployed, AssignmentStatus assignmentStatus)
{
Id = id;
FirstName = firstName;
LastName = lastName;
IsCurrentlyEmployed = IsCurrentlyEmployed;
this.Status = assignmentStatus;
}
public List<Employee> Employees()
{
Employee Emp1 = new Employee(1, "John", "Smith", true, AssignmentStatus.Assigned);
Employee Emp2 = new Employee(2, "Kevin", "Moore", true, AssignmentStatus.Assigned);
Employee Emp3 = new Employee(3, "Eric", "Johnson", false, AssignmentStatus.Assigned);
Employee Emp4 = new Employee(4, "Michell", "McDevour", true, AssignmentStatus.Assigned);
Employee Emp5 = new Employee(5, "Henry", "Jones", true, AssignmentStatus.Assigned);
Employee Emp6 = new Employee(6, "Sarah", "Holmes", true, AssignmentStatus.Assigned);
List<Employee> listEmployees = new List<Employee>();
listEmployees.Add(Emp1);
listEmployees.Add(Emp2);
listEmployees.Add(Emp3);
listEmployees.Add(Emp4);
listEmployees.Add(Emp5);
listEmployees.Add(Emp6);
return listEmployees;
}
I fixed your Employees()
method too. 我也修复了您的Employees()
方法。 Though I would not recommend putting code like this in the Employee
class as it doesn't make logical sense, why should an Employee
maintain a list of Employees
? 尽管我不建议将这样的代码放在Employee
类中,因为这在逻辑上没有意义,但是为什么Employee
应该维护一个Employees
列表?
You could then set the status like so: 然后,您可以像这样设置状态:
Employee e = new Employee(0, "", "", false, Employee.AssignmentStatus.Assigned);
Or 要么
e.Status = Employee.AssignmentStatus.Assigned;
You need a property for the Status to be able to set it for each employee 您需要状态的属性才能为每个员工设置
public AssignmentStatus Status { get; public AssignmentStatus Status {get; set; 组; } }
I have tidied up the code for you and shown how to assign the assignment status. 我已经为您整理了代码,并说明了如何分配分配状态。 You'll find comments in the code that explain a little further. 您会在代码中找到注释,这些注释会进一步解释。
public class Employee
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public bool IsCurrentlyEmployed { get; set; }
public eAssignmentStatus AssignmentStatus { get; set; }
/// <summary>
/// Defines the assignment status for the employee. Prefixed 'e' to denote it's an enum and avoid clashes with the property name.
/// </summary>
public enum eAssignmentStatus
{
NotDefined,
Assigned,
Idle,
Trainee
}
/// <summary>
/// Main constructor
/// </summary>
/// <param name="id"></param>
/// <param name="firstName"></param>
/// <param name="lastName"></param>
/// <param name="isCurrentlyEmployed"></param>
public Employee(int id, string firstName, string lastName, bool isCurrentlyEmployed)
{
Id = id;
FirstName = firstName;
LastName = lastName;
IsCurrentlyEmployed = IsCurrentlyEmployed;
}
/// <summary>
/// Constructor overload with eAssignmentStatus parameter
/// </summary>
/// <param name="id"></param>
/// <param name="firstName"></param>
/// <param name="lastName"></param>
/// <param name="isCurrentlyEmployed"></param>
/// <param name="assignmentStatus"></param>
public Employee(int id, string firstName, string lastName, bool isCurrentlyEmployed, eAssignmentStatus assignmentStatus)
: this(id, firstName, lastName, isCurrentlyEmployed)
{
AssignmentStatus = assignmentStatus;
}
/// <summary>
/// Creates some employee objects, however, I would recommend putting this method inside another 'Factory' class, as you'd have to create
/// an instance of Employee in order to call "Employees()"
/// </summary>
/// <returns>Now returns a list as this is what your method name implies.</returns>
public List<Employee> Employees()
{
Employee Emp1 = new Employee(1, "John", "Smith", true, eAssignmentStatus.Assigned);
Employee Emp2 = new Employee(2, "Kevin", "Moore", true, eAssignmentStatus.Idle);
Employee Emp3 = new Employee(3, "Eric", "Johnson", false, eAssignmentStatus.Trainee);
Employee Emp4 = new Employee(4, "Michell", "McDevour", true);
Employee Emp5 = new Employee(5, "Henry", "Jones", true);
Employee Emp6 = new Employee(6, "Sarah", "Holmes", true);
List<Employee> listEmployees = new List<Employee>();
return listEmployees;
}
}
You should use the enum 您应该使用枚举
public class Employee
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public bool IsCurrentlyEmployed { get; set; }
public AssignmentStatus EmployeeAssignmentStatus { get; set; }
enum AssignmentStatus
{
Assigned,
Idle,
Trainee,
NotDefined
}
public Employee(int id, string firstName, string lastName, bool isCurrentlyEmployed, AssignmentStatus status)
{
Id = id;
FirstName = firstName;
LastName = lastName;
IsCurrentlyEmployed = IsCurrentlyEmployed;
EmployeeAssignmentStatus = status;
}
public List<Employee> Employees()
{
Employee Emp1 = new Employee(1, "John", "Smith", true, AssignmentStatus.Assigned );
Employee Emp2 = new Employee(2, "Kevin", "Moore", true, AssignmentStatus.Idle );
Employee Emp3 = new Employee(3, "Eric", "Johnson", false, AssignmentStatus.Trainee);
Employee Emp4 = new Employee(4, "Michell", "McDevour", true, AssignmentStatus.NotDefined);
Employee Emp5 = new Employee(5, "Henry", "Jones", true, AssignmentStatus.NotDefined);
Employee Emp6 = new Employee(6, "Sarah", "Holmes", true, AssignmentStatus.NotDefined));
List<Employee> listEmployees = new List<Employee>();
listEmployees.add(Emp1);
listEmployees.add(Emp2);
listEmployees.add(Emp3);
listEmployees.add(Emp4);
listEmployees.add(Emp5);
listEmployees.add(Emp6);
return listEmployees;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.