开发者

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

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开发者_JAVA技巧 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.


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.


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.


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

should be

gv.DataSource = list.FindAll(x => 0 != (x.Type & showType)));

As you don't want the Type to be exactly what showType is, right? You could manually iterate over the list and do a compare and remove what you don't need though I'm not sure it is an elegant solution.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜