If I have an object that can accept multiple members of the same type E.g:
var transaction = new Transaction(arg1, arg2)
.WithHeader("Header Thing 1", "Header Thing 2")
.WithLine(1, "Line1 detail")
.WithLine(2, "Line2 detail")
.WithLine(3, "Line3 detail")
.Create();
Is there a way to add a varying quantity of members using a loop? E.g:
var transaction = new Transaction(arg1, arg2)
.WithHeader("Header Thing 1", "Header Thing 2")
foreach(obj Line in Lines)
{
// add these members to the transaction object
.WithLine(Line.LineNum, Line.LineDetail)
}
.Create();
Yes but not with the syntax you're trying to use. You need to get an instance of the intermediate builder object so you can operate on it in the loop. Something like this:
var builder = new Transaction(arg1, arg2)
.WithHeader("Header Thing 1", "Header Thing 2");
foreach(obj line in Lines)
{
// add these members to the transaction object
builder.WithLine(Line.LineNum, Line.LineDetail)
}
var transaction = builder.Create();