none
what is the best way to extend enum RRS feed

  • Question

  • I read a transaction file including type of events. When I had 2 types of events enum was working. How can I urgently make it work with more type of events? I normally use;

    public enum EventType
        {
            A, D, 
        }

     where, EventType was used in the constructor of the Event class

    public EventType Type { get; set; }

    I read the transactions from input file

    Type = tokens[2].Equals ("A") ? EventType.A : EventType.D,
     //tokens[2] stores the relevant event data from the input file.

    This is working when I have just 2 event types; but what can I do to make it work if I had say 4 event types in addition to A and D?

    I really appreciate if you folks could give a hint.

    Thanks in advance, Gordion



    • Edited by gordion Tuesday, May 22, 2012 9:36 AM
    Tuesday, May 22, 2012 9:35 AM

Answers

  • If your tokens are going to match your enum members. "R" = EventType.R, "A" = EventType.A  etc, you can use:

    try
    {
        Type = (EventType)Enum.Parse(typeof(EventType), tokens[2], true);
    }
    catch(ArgumentException)
    {
        // error: value didn't match a valid enum.
        // handle appropriately
    }
    

    If your tokens will be different, you can use a switch statement.  I'd make a function for this:

    private static EventType Token2EventType(string token)
    {
        switch (token.ToLowerInvariant())
        {
            case "r": return EventType.R;
            case "c": return EventType.C;
            case "a": return EventType.A;
            case "d": return EventType.D;
            default: throw new ArgumentException("Requested value '" + token + "'was not found.");
        }
    }
    


    This signature unintentionally left blank.

    • Marked as answer by gordion Tuesday, May 22, 2012 10:47 AM
    Tuesday, May 22, 2012 10:22 AM
  • use switch case statement

    switch(tokens[2])

    {

    case "A":

    break;

    case "D":

    break;

    case "R":break;

    case "C":break;

    }


    Mark Answered, if it solves your question
    Rohit Arora

    • Marked as answer by gordion Tuesday, May 22, 2012 10:47 AM
    Tuesday, May 22, 2012 10:23 AM

All replies

  • Option 1. You have to add more enums in your enum EventType.

    Option 2. Dynamically add enums through reflection


    Mark Answered, if it solves your question
    Rohit Arora

    Tuesday, May 22, 2012 9:40 AM
  • I added more enums in the Event Type like;

    R, C, A, D.

    But then how can I check it like this and assign? I couldn't get this part worked out...

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

    when I tried putting these in an if clause to check for the "event type" in token, it just didn't work with plenty of errors like trying to use Type as variable...

    Thanks


    GDIon


    • Edited by gordion Tuesday, May 22, 2012 9:46 AM
    Tuesday, May 22, 2012 9:43 AM
  • If your tokens are going to match your enum members. "R" = EventType.R, "A" = EventType.A  etc, you can use:

    try
    {
        Type = (EventType)Enum.Parse(typeof(EventType), tokens[2], true);
    }
    catch(ArgumentException)
    {
        // error: value didn't match a valid enum.
        // handle appropriately
    }
    

    If your tokens will be different, you can use a switch statement.  I'd make a function for this:

    private static EventType Token2EventType(string token)
    {
        switch (token.ToLowerInvariant())
        {
            case "r": return EventType.R;
            case "c": return EventType.C;
            case "a": return EventType.A;
            case "d": return EventType.D;
            default: throw new ArgumentException("Requested value '" + token + "'was not found.");
        }
    }
    


    This signature unintentionally left blank.

    • Marked as answer by gordion Tuesday, May 22, 2012 10:47 AM
    Tuesday, May 22, 2012 10:22 AM
  • use switch case statement

    switch(tokens[2])

    {

    case "A":

    break;

    case "D":

    break;

    case "R":break;

    case "C":break;

    }


    Mark Answered, if it solves your question
    Rohit Arora

    • Marked as answer by gordion Tuesday, May 22, 2012 10:47 AM
    Tuesday, May 22, 2012 10:23 AM
  • Thanks Guys. This was really helpful!

    GDIon

    Tuesday, May 22, 2012 10:48 AM