-4

In C#, I have used Enum to get the drop down values:

class Enum
{
    public enum Fields
    {       
        AssignedTo = "Assigned To",
        CloseReason = "Close Reason",
        CustomerId = "Customer ID",
        CustomerName = "Customer Name",
        CompanyID = "Company ID",
        CompanyName = "Company Name",
    }
}

When I build the solution. I am getting the error: cannot implicitly convert type 'string' to 'int'.

I have removed the double quotes but still it shows the same error. What do I do wrong?

Quality Catalyst
  • 6,531
  • 8
  • 38
  • 62
ChanGan
  • 4,254
  • 11
  • 74
  • 135

5 Answers5

2

In C# string are not allowed types for enums.

Quote from MSDN https://msdn.microsoft.com/en-us/library/sbbt4032.aspx

The approved types for an enum are byte, sbyte, short, ushort, int, uint, long, or ulong

If will have to do some kind of conversion (i.e. using [DisplayName] attribute) to use enum in dropdown.

morincer
  • 884
  • 6
  • 6
0

this is syntactically invalid in c#

https://msdn.microsoft.com/en-us/library/sbbt4032.aspx

you will need a workaround to create string enum in c#

https://www.google.com/?gfe_rd=cr&ei=sBwXV_XZDKO_wQOjmaqABw&gws_rd=ssl#q=string+enum+in+c%23

or hor
  • 723
  • 3
  • 11
0

It is giving this error because by default the enum expects you to assign an integer value to the fields inside an enum.

public enum Fields
{       
    AssignedTo = 1,
    CloseReason = 2,
    CustomerId = 3,
    CustomerName = 4,
    CompanyID = 5,
    CompanyName = 6
}

If you skip the assigning part , it will by default start assign the fields starting from integer 0.

Quality Catalyst
  • 6,531
  • 8
  • 38
  • 62
0

i am not sure if it is because you are trying to display string to the enum then is because enum only use int 16 and 32 so we have to create an extension to make it show strings and then use the int value to call it

using System.Reflection;

  public static class EnumExtensions
{

    public static string DisplayName(this Enum value)
    {
        FieldInfo field = value.GetType().GetField(value.ToString());

        EnumDisplayNameAttribute attribute
                = Attribute.GetCustomAttribute(field, typeof(EnumDisplayNameAttribute))
                    as EnumDisplayNameAttribute;

        return attribute == null ? value.ToString() : attribute.DisplayName;
    }
}

public class EnumDisplayNameAttribute : Attribute
{
    private string _displayName;
    public string DisplayName
    {
        get { return _displayName; }
        set { _displayName = value; }
    }
}
class Enum
  {
         public enum Fields
{       
    [EnumDisplayName(DisplayName = "Assigned To"
    AssignedTo,
    [EnumDisplayName(DisplayName = "Close Reason"
    CloseReason,
    [EnumDisplayName(DisplayName = "Customer ID"
    CustomerId,
    [EnumDisplayName(DisplayName = "Customer Name"
    CustomerName,
    [EnumDisplayName(DisplayName = "Company ID"
    CompanyID,
    [EnumDisplayName(DisplayName = "Company Name"
    CompanyName ,
}
}

just like i did for my group description for listview

shawn
  • 93
  • 1
  • 9
0

I do not think you want to be using enum for a drop down. Try looking into Dictionary or DataTable or some other structures. Maybe we could help if you give us more detail about what exactly are you trying to do.

EDIT: Here is a simple example of using List<string> as data source for ComboBox

List<string> dsList = new List<string>();
dsList.Add("Assigned To");
dsList.Add("Close Reason");
dsList.Add("Customer ID");
dsList.Add("Customer Name");
dsList.Add("Company ID");
dsList.Add("Company Name");

comboBox1.DataSource = dsList;

Also, you can dynamicaly change the List based on what options you want to display to the user

Fila
  • 193
  • 7
  • I do have 4 drop down options.. Eg. If I use public const string AssignedTo and the dropdown options, then it shows the value of other drop down options also which may irrelevant. If I give this in Enum, it limit that option. Eg., If I use Close Reason, i should see the drop down values of Close Reason only not other values.. This logic i am trying. – ChanGan Apr 20 '16 at 06:28
  • Still not sure what exactly you want to do, but I added an example to my answer - you can try using List as a data source to your control. Hopefully it will push you to the right direction. – Fila Apr 20 '16 at 07:19