If you need to support a lot of logical flags, I would change it to a dictionary and do something like this:
class Class1 { private Dictionary<String, Boolean> boolenVars = new Dictionary<String, Boolean>(); public Boolean getFlag(String key) { if (this.boolenVars.ContainsKey(key)) return this.boolenVars[key]; else return false; } public void setFlag(String key, Boolean value) { if (this.boolenVars.ContainsKey(key)) this.boolenVars[key] = value; else this.boolenVars.Add(key, value); } public void clearFlags() { this.boolenVars.Clear(); } }
Instead of using a string key, you can create an enumeration for this purpose to ensure that there are no typos when using flags.
This solution will not require any additional code changes, even if you decide to add 7526 new Boolean Flags.
This solution also provides - if you expose the dictionary using the public getter method or method - a list of all the "set" logical flags.
source share