Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c# property get,set with different types

I have such an enum and a 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 should I write the set accessor of the property?

UPDATE:

I wanted the get accessor to return string and set accessor to take parameter type Employee.Type. I learned that it is impossible to do this in a property according to the C# spec. I have to write separate getter and setter methods.

like image 528
Timuçin Avatar asked Apr 15 '11 15:04

Timuçin


3 Answers

Use DescriptionAttribute instead.

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

Then you would just have an

public Type EmployeeType {get; set;}

property. And if somebody wanted to write it out, they could get the description. I'd also call it Type instead of EmployeeType, because the call myEmployee.EmployeeType sounds redundant. Your other option might be to unroll the property and have two methods

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

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

like image 135
Yuriy Faktorovich Avatar answered Sep 20 '22 04:09

Yuriy Faktorovich


Like this:

EmployeeType = (Type)Enum.Parse(typeof(Type), value);
like image 31
Iain Ward Avatar answered Sep 20 '22 04:09

Iain Ward


I recommend you don't use the word type, and you need to parse the enum:

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

Edit:

First, I can't reiterate enough not to use the word Type for either the enum OR the string for returning the property. Second, the usage of enums here with switch could land you in trouble, but the default may bail you out.

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 the problem you're running into with the conversion is that you're using an assignment that is not a string like:

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

This is an invalid case because someType is a type and EmployeeType (value) is a string. To fix this you need to assign it with:

this.EmployeeType = someType.ToString();

All of this sort of boils down to pretty silly because it can be accomplished with something as simple as:

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();
like image 23
Joel Etherton Avatar answered Sep 19 '22 04:09

Joel Etherton