Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a function for merging data from an object (a PrintTestDataItem) into a template file for a printed label (a LabelFormatDocument called 'label' in this snippet). The LabelFormatDocument is a third-party class that I use to set various fields on my label and I do this by setting the numerous substrings to the values that I want from my PrintTestDataItem object.

There are a lot of these substrings and my current function just sets each substring sequentially, which is obviously not an ideal way to do this sort of operation. What would be a better way to set these substrings programmatically and any other suggestions for increasing the maintainability of this function?

My first idea is to use a dictionary to store the substring names and map them to the appropriate parameter of the PrintTestDataItem, is this the right path to go down?

public void MergePrintData(PrintTestDataItem printTestData)
    {
        label.SubStrings["Barcode"].Value = printTestData.IDNumber.ToString();
        label.SubStrings["Date"].Value = printTestData.TestDate.ToString();
        label.SubStrings["WRN"].Value = printTestData.WRNNumber;
        label.SubStrings["Operator"].Value = printTestData.OperatorName;
        label.SubStrings["PrintheadType"].Value = printTestData.HeadType;
        label.SubStrings["PrintheadSerial"].Value = printTestData.HeadSerial;
        label.SubStrings["Batch"].Value = printTestData.BatchNumber;
        label.SubStrings["PrintFrequency"].Value = (printTestData.PrintFrequency + "kHz");
        label.SubStrings["PrintDistance"].Value = (printTestData.PrintDistance + "mm");
        label.SubStrings["InkType"].Value = printTestData.InkType.ToString();
        label.SubStrings["InkColour"].Value = printTestData.InkColour;
        label.SubStrings["InkTemperature"].Value = (printTestData.InkTemperature + "°C");
        label.SubStrings["DifferentialPressure"].Value = (printTestData.DifferentialPressure + "mBar");
        label.SubStrings["MeniscusPressure"].Value = (printTestData.MeniscusPressure + "mBar");
        label.SubStrings["Comments"].Value = printTestData.Comments;
        label.SubStrings["EncoderDivide"].Value = printTestData.EncoderDivide;
        label.SubStrings["EncoderMultiply"].Value = printTestData.EncoderMultiply;
        label.SubStrings["Offset"].Value = printTestData.Offset;
        label.SubStrings["NumberOfGreyLevels"].Value = printTestData.NumberOfGreyLevels;
        label.SubStrings["Mirror"].Value = BooleanRemapping(printTestData.Mirror);
        label.SubStrings["DIR"].Value = BooleanRemapping(printTestData.DIR);
        label.SubStrings["ReversePrint"].Value = BooleanRemapping(printTestData.ReversePrint);
        label.SubStrings["PrintDirection"].Value = printTestData.PrintDirection.ToString();
        label.SubStrings["PrintMode"].Value = printTestData.PrintMode.ToString();
        label.SubStrings["PrintTransportMode"].Value = printTestData.PrintTransportMode.ToString();
        label.SubStrings["AmbientTemperature"].Value = (printTestData.AmbientTemperature + "°C");
        label.SubStrings["HoursRun"].Value = printTestData.HoursRun;
        label.SubStrings["LinesDown"].Value = printTestData.LinesDown;
    }
share|improve this question

I think the solution for this case maybe is more painful than a long code of config, and this code looks like test data.

A possible workaround could be match the label string with the same property of printTestData. and then call it by reflection. But i prefer have the long config. instead of a hackish-call-property.

share|improve this answer
    
@JNH, I like this config suggestion from a code maintenance view; especially if it is test data. I like the idea of the explicit declaration of the participating properties vice reflection; reflection is harder to debug and other public properties could cause problems if inadvertently picked up by the reflective code. So given the config, read it into a dictionary. – radarbob Nov 17 '15 at 16:46

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.