2014-09-22 130 views
-1

對不起,我是相當新的枚舉,我試圖在邏輯上下文中實現它。在我的Employee類中,我創建了一些員工對象。我還爲Employee對象分配狀態創建了一個枚舉。我的目標是創建一個員工列表,併爲每個員工分配一個分配狀態,最後通過列表打印員工數據和員工分配狀態。使用枚舉的對象c#

我想知道:

1 - 這是一個很好的情況下使用枚舉? 2 - 如何爲每個員工分配工作分配狀態?例如,如果我想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你

+0

請張貼一些代碼給我們審查和展示一些更多的研究。 – krisdyson 2014-09-22 09:13:02

+0

我的appologies,似乎我沒有發佈的代碼。 – koffe14 2014-09-22 09:14:50

+0

你定義了一個'enum'類型,但你也必須實際使用它。添加一個'AssignmentStatus'類型的屬性。順便說一句,在.NET中,也可以調用該屬性AssignmentStatus。 – 2014-09-22 09:17:36

回答

2

1 - 這是一個很好的情況下使用枚舉?

當然

2 - 如何分配與分配狀態每位員工?

您需要公開AssignmentStatus類型的公共財產或創建一個構造函數來做到這一點

例如:

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

我太固定的Employees()方法。雖然我不建議在Employee這個類中放置這樣的代碼,因爲它沒有邏輯意義,但爲什麼Employee要維護一個Employees的列表?

然後,您可以設置像這樣的狀態:

Employee e = new Employee(0, "", "", false, Employee.AssignmentStatus.Assigned); 

或者

e.Status = Employee.AssignmentStatus.Assigned; 
+0

現在Employees()返回一個空的列表 – Pavenhimself 2014-09-22 09:22:17

+0

@Pavenhimself比之前編譯的更好。 – DGibbs 2014-09-22 09:25:05

+0

的確如此。看起來不錯 – Pavenhimself 2014-09-22 09:25:31

1
  1. 是的,這是用枚舉
  2. 您需要狀態的屬性可以設置它爲每個員工

    公衆的好方案AssignmentStatus狀態{get;組; }

0

您應該使用枚舉

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; 

    } 
1

我已經收拾你的代碼,並顯示如何分配分配狀態。 你會在代碼中找到解釋更多的評論。

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

我知道我應該按照你的建議將enum設置爲public。謝謝 – koffe14 2014-09-22 09:33:32