I’m working on a legacy web application written in .NET. At the start of the application it checks for which user type is logged in and determines which UI elements to display based on the user type.
The way this is implemented now is a series of if statements to handle each user type case followed by a function call to set the various UI elements to either visible or invisible based on the user type. The problem is the function call is kind of messy. There are about 500 lines of the following:
setTabsVisible(true, true, true, true, true, true, true, true, true, true, false, false);
if (!locked)
{
setActionButtonsVisibile(true, false, true, "Submit", "");
script += setTabsReadOnly(false, false, false, false, false, false, true, false, false, false, true, true);
script += setSubTabsReadOnly(true, true, false, true, true, true, false, false, true, true, false, true);
}
script += setSubTabsVisible(false, false, false, false, false, false, true, false, false, true, true);
All that true, true, false, true, etc isn’t very readable at first glance. To see what’s being turned on or off you have to hover over the function and match up boolean values to the parameter list.
So I was thinking a better solution might be a bit field that is built from constants that are bitwise OR’d together. This way a function call might look more like this:
setTabsReadOnly(notesTab | documentsTab | signoffTab | etc);
However, C# doesn’t seem to have support for bitfields. Is there a better solution? Or is it even worth changing?
You should use Flag Enums as the above example shows to Simplify your code. For more infos about Enums, check this reference. So in your case you would have: