none
How to cast Enum in to String RRS feed

  • Question

  • Hi,

    I have a Enum like this:

     public enum TransferReason
        {
            Garbage = 0,
            Crime = 1,
            Sick = 2,
        }

    I want to make e DropDown that list all values of this Enum.

    DropDown Items are Strings, it also has a public method

    public AddItem(string)
    {
    ....
    }

    If I try like this:

                foreach (TransferManager.TransferReason material in (TransferManager.TransferReason[])Enum.GetValues(typeof(TransferManager.TransferReason)))
                {
    
                    AddItem(material);
                }
            }

    Obviously it does not work as "material" is not a string.

    How should I cast it to string? I tried few ways but no luck so far.

    Cris.

    Wednesday, October 2, 2019 1:38 PM

Answers

  • Hi cris-id,

    Thank you for posting here.

    >>I want to make e DropDown that list all values of this Enum.

    For your question, plase try the code below.

     comboBox1.Items.Clear();
                // Conversion from Enum to String  
                foreach (string item in Enum.GetNames(typeof(TransferReason)))
                {
                    comboBox1.Items.Add(item);
                }

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, October 3, 2019 2:14 AM
    Moderator

All replies

  • Try this

    comboBox1.DataSource = Enum.GetValues(typeof(TransferReason))
        .Cast<TransferReason>().ToList();

    Get the selected item

    if (Enum.TryParse(comboBox1.Text, out TransferReason currenTransferReason))
    {
        Console.WriteLine(currenTransferReason);
    }


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange


    Wednesday, October 2, 2019 1:53 PM
    Moderator
  • Every object in .NET has a `ToString` method so you can use it. 

    foreach (var material in Enum.GetValues(typeof(TransferManager.TransferReason)))
    {
       AddItem(material.ToString());
    }

    However you don't really need to do this because every UI control generally supports getting a list of objects and automatically calling `ToString` on them. So in general you can just bind the list of values directly to the `Items` or equivalent property on the control and it will handle the conversion.


    Michael Taylor http://www.michaeltaylorp3.net

    Wednesday, October 2, 2019 1:55 PM
    Moderator
  • Hi cris-id,

    Thank you for posting here.

    >>I want to make e DropDown that list all values of this Enum.

    For your question, plase try the code below.

     comboBox1.Items.Clear();
                // Conversion from Enum to String  
                foreach (string item in Enum.GetNames(typeof(TransferReason)))
                {
                    comboBox1.Items.Add(item);
                }

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, October 3, 2019 2:14 AM
    Moderator
  • Hi,

    Thank you all for suggestions.

    I saw some related to Windows.Forms.DropDown.

    When I tried this with this control it worked.

    But actually DropDown I am trying to use is ColosalFramework item, and as far as I know it will not accept abject with AddItem method.

    I will try solutions with getting strings from enum.

    Cris.

    Thursday, October 3, 2019 10:44 AM
  • Hi cris-id,

    What is your winform DropDown control? I need more information to test.

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, October 4, 2019 9:32 AM
    Moderator
  • Hi Wendy,

    Your code from above works perfectly.

    Thanks,

    Cris.

    Wednesday, October 16, 2019 10:44 AM
  • Hi cris-id,

    Glad to help. Do not forget to mark my reply as answer. Thank you~

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Tuesday, October 22, 2019 4:04 AM
    Moderator