Results 1 to 4 of 4

Thread: How to Covert List<string> values to Enum?

  1. #1

    Thread Starter
    Member
    Join Date
    Jan 2020
    Posts
    59

    How to Covert List<string> values to Enum?

    How can i convert this??

    List lstDays = new List { "Monday", "Tuesday ", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday" };

    Need Enum like

    enum lstDays
    {
    Monday,
    Tuesday,
    Wednesday,
    Thursday,
    Friday,
    Saturday,
    Sunday
    }

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,297

    Re: How to Covert List<string> values to Enum?

    I'm fairly certain that you have asked this question before and been told that it's nonsensical. If not, someone else asked the exact same question, which seems fairly suspicious. If you want an enumeration then declare an enumeration. There is no "conversion" to be done.

    As I also think you were told on that other occasion, there seems no good reason to want either of those code snippets, given that the System.DayOfWeek enumeration already exists.

  3. #3
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,988

    Re: How to Covert List<string> values to Enum?

    Well, it wasn't asked here by the same person, but it certainly doesn't make sense.

    An enum isn't a list, so there's no automatic conversion. It's seven items, though, at worst, so just create an enum by hand. You'll have to do so anyways, and you've already done so, which means you've basically answered your own question as a part of the question. Therefore, I assume you want something other than what you actually asked for.
    My usual boring signature: Nothing

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,297

    Re: How to Covert List<string> values to Enum?

    Quote Originally Posted by Shaggy Hiker View Post
    Well, it wasn't asked here by the same person
    No, it was at a different forum and the user name was different but the wording was almost identical:
    I try to Covert the List<> value to Enum but i'm Failed.

    List<string> lstDays = new List<string> { "Monday", "Tuesday ", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday" };

    Need Enum like

    Code:
    enum lstDays
        {
            Monday,
            Tuesday,
            Wednesday,
            Thursday,
            Friday,
            Saturday,
            Sunday
        }
    Please can help?
    so it's either the same person who is completely disregarding the advice already provided or it's someone else doing the same assignment and failing to provide a proper explanation of their requirements in the same way. Even if you think you need something like that, that need doesn't exist in a vacuum. You must think you need it for a reason but there was no explanation before and there's no explanation here now either.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width