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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With