Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I pass more than one enum to a method that receives only one?

I'm wondering if the following is possible:

The method Regex.Match can receive an enum, so I can specify:

RegexOptions.IgnoreCase
RegexOptions.IgnorePatternWhiteSpace
RegexOptions.Multiline

What if I need to specify more than just one? (eg. I want my regex to be Multiline and I want it to ignore the pattern whitespace).

Could I use | operator like in C/C++?

like image 554
Oscar Mederos Avatar asked May 16 '11 19:05

Oscar Mederos


2 Answers

You need to annotate it with [Flags] attribute and use | operator to combine them.

In the case you mentioned, you can do that because RegexOptions enum is annotated with it.


More References:

A helpful way to use the FlagsAttribute with enumerations

Example Snippet from above CodeProject article:

Definition:

[FlagsAttribute]
public enum NewsCategory : int 
{
    TopHeadlines =1,
    Sports=2,
    Business=4,
    Financial=8,
    World=16,
    Entertainment=32,
    Technical=64,
    Politics=128,
    Health=256,
    National=512
}

Use:

mon.ContentCategories = NewsCategory.Business | 
                        NewsCategory.Entertainment | 
                        NewsCategory.Politics;
like image 63
decyclone Avatar answered Sep 27 '22 02:09

decyclone


Since it is an Enum with a Flags Attribute, you can use:

RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhiteSpace | RegexOptions.Multiline
like image 24
Marcelo Avatar answered Sep 23 '22 02:09

Marcelo