Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Compare Flags in C#? (part 2)

Tags:

c#

.net

flags

bit

Bit flags are a little difficult to understand :)

I know about this and this questions and I do understand the answers and I even followed this article from a good friend of mine.

But I still cant figure it out when I need to "evolute" more than the standard...

What I'm trying to do is this:

    if (HttpContext.Current.Session["DebugSessionText"] != null)
    {
        showType = parDebug.Write2LogType.WARN | 
                   parDebug.Write2LogType.ERROR | 
                   parDebug.Write2LogType.INFO;

        if (!chkInfo.Checked)
            showType &= ~parDebug.Write2LogType.INFO;  // remove INFOs        
        if (!chkError.Checked)
            showType &= ~parDebug.Write2LogType.ERROR; // remove ERRORs

        List<myDebugRow> list =
            (List<myDebugRow>)HttpContext.Current.Session["DebugSessionText"];

        gv.DataSource = list.FindAll(x => x.Type == showType));
    }
    gv.DataBind();

I do need to filter a List object, so I can get just what the user wants (show only INFO errors, exception ERRORs but WARNing errrors will always be showed) ...

Is there a direct way to do this or I need to filter it manually without using the LAMBDA expression?

Thank you for all the help.

like image 912
balexandre Avatar asked Nov 30 '22 19:11

balexandre


2 Answers

With

x.Type == showType

you will only get the items that match all the conditions (bit flags) exactly. With

(x.Type & showType) != 0

You will find all items that have at least a 1 bit match with showType, which is probably what you want.

like image 112
Henk Holterman Avatar answered Dec 04 '22 21:12

Henk Holterman


If you find these operations confusing -- and frankly, I certainly do -- then consider upping the level of abstraction. Write yourself some helper extension methods.

static WriteToLogType AddWarn(this WriteToLogType x) { return x | WriteToLogType.WARN; }
static WriteToLogType ClearWarn(this WriteToLogType x) { return x & ~WriteToLogType.WARN; }
static bool HasWarn(this WriteToLogType x) { return 0 != (x & WriteToLogType.WARN); }
// same for Error, Info
static bool HasExactly(this WriteToLogType x, WriteToLogType y) { return x == y; }
static bool HasAny(this WriteToLogType x, WriteToLogType y) { return 0 != (x & y); }
static bool HasAll(this WriteToLogType x, WriteToLogType y) { return y == (x & y); }

And now your program becomes

    showType = WriteToLogType.None.AddWarn().AddInfo().AddError();
    if (!chkInfo.Checked) showType = showType.ClearInfo();
    if (!chkError.Checked) showType = showType.ClearError();
    List<myDebugRow> list = whatever;
    gv.DataSource = list.FindAll(x => x.Type.HasAny(showType)));

Which I hope you agree is far more clear than all that bit-twiddling. But we can make this more clear still.

    showType = WriteToLogType.None.AddWarn();
    if (chkInfo.Checked) showType = showType.AddInfo();
    if (chkError.Checked) showType = showType.AddError();
    List<myDebugRow> list = whatever;
    gv.DataSource = list.FindAll(x => x.Type.HasAny(showType)));

Instead of adding a bunch of flags and then taking them away, simply don't add them in the first place.

like image 28
Eric Lippert Avatar answered Dec 04 '22 21:12

Eric Lippert