Reduce code that references the same object?

0

Here the code, would you have any way to reduce it? (For learning purposes only)

worksheet.Row(1).Style.Font.FontColor = ClosedXML.Excel.XLColor.White;
worksheet.Row(1).Style.Font.Bold = true;
worksheet.Row(1).Style.Alignment.SetHorizontal(XLAlignmentHorizontalValues.Center);
worksheet.Row(1).Style.Alignment.SetVertical(XLAlignmentVerticalValues.Center);
worksheet.Row(1).Height = 32;

I thought of something like ...

worksheet.Row(1)(.Style.Font.FontColor = ClosedXML.Excel.XLColor.White).Style.Font.Bold = true;

But it does not work: /

    
asked by anonymous 17.10.2016 / 01:19

1 answer

1

This does not really work and does not make sense. The maximum you would give to do is:

var objeto = worksheet.Row(1).Style;
objeto.Font.FontColor = ClosedXML.Excel.XLColor.White;
objeto.Font.Bold = true;
objeto.Alignment.SetHorizontal(XLAlignmentHorizontalValues.Center);
objeto.Alignment.SetVertical(XLAlignmentVerticalValues.Center);

Obviously you could have another object without Stype to get Height and could also have one that has Font or Aligment together. But there is no gain.

I would avoid this until I was sure that I mastered all the consequences. This, as it is, without considering the context, will work, but a more complex example may not work the way you expect.

This is a normal repeat that does not cause problems, not a case for DRY .

    
17.10.2016 / 01:53