none
Enum should Include "All"? RRS feed

  • Question

  • Dear All

    Thank you for your attention.

    My web application using an enum

    OrderType  { New , Completed, Voided }

    There is a search page for view history order records.

    User could search 'All', 'New', 'Completed' or 'Voided'

    I would then like to know is 'All' value should be added to OrderType.

    Thanks.

    Best Regards

    mintssoul


    Best Regards
    mintssoul

    Thursday, May 28, 2015 4:45 AM

Answers

  • "I would then like to know is 'All' value should be added to OrderType."

    No. What would OrderType.All mean anyway?

    What you really need is a separate enum that can be used to represent the search option selected by the user. Or, use the existing enum as is and represent the "all" idea by using nullable variables, that will make the filtering code slightly easier to write:

    bool IsMatch(Order order, OrderType? type) {
        return type == null || order.Type == type;
    }

    • Marked as answer by mintssoul Thursday, May 28, 2015 7:02 AM
    Thursday, May 28, 2015 5:48 AM
    Moderator

All replies

  • Hi ,

    you can do this

    enum OrderType  
            {
                New ,
                Completed,
                Voided ,
    
                All = New | Completed | Voided
            }

    If this answer was helpful please remember to close your threads by marking helpful posts as answer

    Fares


    • Edited by Fares amr Thursday, May 28, 2015 5:21 AM
    Thursday, May 28, 2015 5:20 AM
  • "I would then like to know is 'All' value should be added to OrderType."

    No. What would OrderType.All mean anyway?

    What you really need is a separate enum that can be used to represent the search option selected by the user. Or, use the existing enum as is and represent the "all" idea by using nullable variables, that will make the filtering code slightly easier to write:

    bool IsMatch(Order order, OrderType? type) {
        return type == null || order.Type == type;
    }

    • Marked as answer by mintssoul Thursday, May 28, 2015 7:02 AM
    Thursday, May 28, 2015 5:48 AM
    Moderator