I have an object with multiple boolean (and integer) properties. I want to create a collection of objects with lot of different properties to have a good dataSet used for tests.
For example
public class A
{
public int value {get; set;} //want to try beetween 0 and 5
public bool foo {get; set;}
public bool bar {get; set;}
public bool boo {get; set;}
}
For this example I like to have a list with 6*2*2 = 24 elementw, with value
= 0/1/2/3/4/5, foo
= true/false, bar
= true/false, but boo
always false.
I could do that using 24 lines of code. But it's a lot and if I have one more bool it doubles the number of line.
I may be use one loop per property but I don't think it's a good way to have too many nested loop. I also might do this by reflection, but I think this is too much work, isn´t it?
I think there is another way to do it better but I don't find it.
What is your problem on this old loop-based approach?
var boolVals = new[] { true, false };
var intVals = new[] { 0, 1, 2, 3, 4, 5 };
var myList = new List<A>();
foreach(var foo in boolVals) {
foreach(var value in intVals) {
foreach(var bar in boolVals) {
foreach (var boo in boolVals) {
myList.Add(new A { value = value, foo = foo, bar = bar, boo = boo });
}
}
}
}
You have to add a new nesting for every property you add and loop the appropriate set of values this property might have.
This appraoch fils every possible permutation of your values within a few lines of easy to read code.
Of course it looks a bit ugly. But it increases only by one further nesting on every added property.
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