C # get property set with different types

I have such an enumeration and property.

public enum Type { Hourly = 1, Salary = 2, None = 3 }; public string EmployeeType { get { string type; switch (employeeType) { case Type.Hourly: type = "Hourly Employee"; break; case Type.Salary: type = "Salary Employee"; break; default: type = "None"; break; } return type; } // **EDIT:** // Now I am trying to parse the string as enum Type. // But Constructor still waits a string to set EmployeeType. set { employeeType = (Type)Enum.Parse(typeof(Type), value); } } 

This is my class:

 public class Employee { private Type employeeType; } 

And I want to create such a constructor:

 Employee(Employee.Type type) { EmployeeType = type; } 

EDIT:

Cannot implicitly convert type 'Payroll.Employee.Type' to 'string'

How do I write an access attribute for this property?

UPDATE:

I wanted get accessor to return a string and set accessor to take the parameter type Employee.Type. I found out that this is not possible to do in the property according to the C # specification. I have to write separate getter and setter methods.

+4
source share
4 answers

Use DescriptionAttribute .

 public enum Type { [Description("Hourly Employee")] Hourly = 1, [Description("Salary Employee")] Salary = 2, [Description("None")] None = 3 }; 

Then you just have

 public Type EmployeeType {get; set;} 

properties. And if someone wants to write this, they can get a description. I would name it Type instead of EmployeeType , because calling myEmployee.EmployeeType sounds redundant. Another option might be to expand the property and have two methods

 public string GetEmployeeType() { //your switch statement } public void SetEmployeeType(EmployeeType type) { _type = type; } 

Not as elegant as a property, but quickly does the job. Also remember that properties in IL are just methods.

+11
source

Like this:

 EmployeeType = (Type)Enum.Parse(typeof(Type), value); 
+4
source

I recommend that you do not use the word type, and you need to parse the enumeration:

 set { employeeType = (Type)Enum.Parse(typeof(Type), value); } 

Edit:

First, I cannot repeat enough not to use the word Type to enumerate OR strings to return a property. Secondly, using the enumerations here with the switch may cause you problems, but by default you can free you.

 public enum WorkType { Hourly = 1, Salary = 2, None = 3 }; // Initialize this to prevent craziness private WorkType employeeType = WorkType.None; public string EmployeeType { get { // I'm not sure why you want to return a string // in this property but whatevs. // First make sure that you have a valid enum if ((int)employeeType > 3 || (int)employeeType < 1) employeeType = WorkType.None; return employeeType.ToString(); // Don't need a switch, just call ToString() } set { // This might be better served with a TryParse. This will // be more fault tolerant if someone using your class passes // in an invalid WorkType. if(!TryParse(typeof(WorkType), value, out employeeType)) employeeType = WorkType.None; } } } 

I suspect that the problem you are working with in the conversion is that you are using an assignment that is not a type string:

 WorkType someType = WorkType.None; this.EmployeeType = someType; // Exception is here 

This is not a valid case because someType is a type and EmployeeType (value) is a string. To fix this, you need to assign it:

 this.EmployeeType = someType.ToString(); 

It all boils down to pretty dumb because it can be achieved with something simple:

 public enum WorkType { Hourly = 1, Salary = 2, None = 3 }; public WorkType EmployeeType { get; set; } // Any time you want to access the value of EmployeeType as a string you would // simply use the following line: // EmployeeType.ToString(); 
+3
source

Ideally, you should have a private member that you can set / get to which the property can connect. From there, you can make another way to get the version of "Human Readable / Formatted". eg.

 public enum EmployeeType { Hourly = 1, Salary = 2, None = 3 } private EmployeeType _EmployeeType; public EmployeeType EmployeeType { get { return this._EmployeeType; } set { this._EmployeeType = value; } } 

Then you have a way to return the formatted version

 public String EmployeeType() { switch (this._EmployeeType) { case EmployeeType.Hourly: return "Hourly Employee"; case EmployeeType.Salary: return "Salary Employee"; default: return "None"; } } 

or how I do it. Otherwise, the enumeration does not make sense, and you should just work with the string and check the input / output so that it falls into the pre-selected valid values.

EDIT I recommend this only because entering a string and trying to align it with the rename name (as others suggested) just seems wrong to me. Especially with the transition "Hour" to "Sentinel employee." (obj).EmployeeType = "Hourly Employee" will not work using Enum.Parse because there is no valid enumeration matching the input.

EDITv2 I really like @ using Yuri DescriptionAttribute better. Keep it structured by type, but make it legible when printing. A.

-1
source

All Articles