.net - Using enums with objects c# -
sorry i'm quite new @ enums , i'm trying implement in logical context. in employee class i've created employee objects. i've created enum employee objects assignment status. aim create list of employees , give each employee assignment status , iterrate through list printing out employee data , employee assignment status.
i'm wondering:
1 - case use enums? 2 - how can assign each employee assignment status? example, if want emp1 have assignmentstatus.assigned how should apply syntax-wise?
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 - case use enums?
sure
2 - how can assign each employee assignment status?
you need expose public property of type assignmentstatus
or create constructor this
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 employees()
method too. though not recommend putting code in employee
class doesn't make logical sense, why should employee
maintain list of employees
?
you set status so:
employee e = new employee(0, "", "", false, employee.assignmentstatus.assigned);
or
e.status = employee.assignmentstatus.assigned;
Comments
Post a Comment