If you find that these operations are confusing - and to be honest, I certainly do - then consider raising 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); }
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)));
I hope you will agree much more clearly than anything that is bit-twiddling. But we can make it even more clear.
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 selecting them, just don't add them in the first place.
source share