Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7987297
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T12:06:23+00:00 2026-06-04T12:06:23+00:00

How can I effective extend enum to have more than 2 options. I am

  • 0

How can I effective extend enum to have more than 2 options.
I am reading events from a file, line-by-line.
I have a constructor

public enum EventType
{ A,D }

public class Event
{
    public EventType Type { get; set; }

}

I assigned Type property like this:

 Type = tokens[2].Equals("A") ? EventType.A : EventType.D,

where token[2] is the string that holds values like “A”.

This works fine when there are only A and D, but I want to have 2 more types; say R and C. When I add them to enum field, how can I get the type? The above is giving compilation errors as if using Type as a variable.

I appreciate your immediate help!
Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T12:06:25+00:00Added an answer on June 4, 2026 at 12:06 pm

    There are really only three sensible ways to go about this:

    Enum.TryParse

    If the tokens will always correspond exactly to your enum members, you can use Enum.TryParse:

    EventType type;
    if (Enum.TryParse(tokens[2], out type)) {
        Type = type;
    }
    else { /* token does not exist as an enum member */ }
    

    This approach is the simplest, but it’s probably slower than the next one and it also has another drawback: the author of the code that provides tokens[2] and the author of the enum must always keep their code in sync.

    Use a dictionary

    var dict = new Dictionary<string, EventType>
    {
        { "A", EventType.A },
        { "D", EventType.D },
        // more items here
    }
    
    Type = dict[tokens[2]]; // no error checking, please add some
    

    This requires some setup, but it’s probably the fastest and it also allows accounting for changes in the input strings and/or enum values.

    Use an attribute

    Alternatively, you can annotate your enum members with a custom attribute and write a helper method that uses reflection to find the correct member based on the value of this attribute. This solution has its uses but it’s the least likely candidate; most of the time you should prefer one of the two alternatives.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can I disable a DropDownList in ASP.NET? Code: <asp:TemplateField HeaderText="Effective Total Hours"> <ItemTemplate>
Given a table with employee statuses and effective dates, how can I retrieve just
Can 2 or more equations defining a function in Haskell share the same where
From Effective Java 2nd edition item 67 page 266-268: The background thread calls s.removeObserver
I'm trying to manually dispatch events on a textfield so I can manipulate it
I read Effective Java by Joshua Bloch and removed the 'Constant Interface anti-pattern' from
Lets say I have a controller which provides search functions for a student: public
Jquery has utility a function called ' extend ' which can be used to
I have a mission critical Perl-CGI server-side application that I need to extend or
Can some one explain the below, rather complex recursive generic template usage? public abstract

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.