Reputation: 481
I have a program which needs to support "User Options" to determine how it will overwrite files, the user can choose from "Options" which can result into several combinations making it hard to code all the possible "IF... ELSE statements", this complex result evaluation is hard to code and it is getting too long and also driving me nuts!
I'm looking to solve this with some sort of "parsing" to evaluate all the possible results in a faster and more organic way without long chains of IF...ELSE blocks
Here is what I have in my program options:
For example: a user has selected to overwrite files and picked the option "FILE SIZE" and selected ">=" as criteria for this option, and also selected "FILE DATE" plus "<=", and picked an "OR", all options select will result in something like "FILE >= x" OR "FILE DATE <= x".
Given the options above on the screen shot, a user can create all sorts of possible logical options and combine them using "OR" and "AND", and also pick the ">, <, >=, <=, =, <>".
The complexity behind this little screen is huge and I've been researching how to tackle down this and I heard about things called Lambda expressions and Binary Trees but I have no clue if it does apply to my problem, I would like to at least have somebody to point me to the right direction, I don't even know how to correctly classify my "issue" when googling around for answers :)
Thanks in advance to all!
Upvotes: 1
Views: 203
Reputation: 64943
I don't think your issue would be solved using expression trees. Expression trees are functional expressions that can be analyzed, improved or evaluated before they're compiled. It's a good solution when you want to create a fluent configuration which should provide which properties provide some configuration decided by the developer (there're other use cases but this goes beyond your question):
config.EnableWhatever(x => x.HasWhatever)
Your choice should be around enumerations with [FlagsAttribute]
:
[Flags]
public enum FileSizeOptions
{
None = 0,
IfFileSizeIsGreaterThan = 1,
IfFileSizeIsLowerThan = 2,
OtherOption = 4,
All = IfFileSizeIsGreaterThan | IfFileSizeIsLowerThan | OtherOptions
}
FileSizeOptions options = FileSizeOptions.IfFileSizeIsGreaterThan | FileSizeOptions.OtherOption;
if(options.HasFlag(FileSizeOptions.All))
{
// Do stuff
} else if(options.HasFlag(FileSizeOptions.IfFileSizeIsGreaterThan))
{
// Do stuff
} // and so on...
I mean, you should use masks instead of booleans and .NET has enums as the recommended way to implement masks or flags.
That is, you can evaluate if an enumeration value has 1 or N possible flags defined in the whole enumeration.
Upvotes: 1