Copying and Pasting cs Code

In cs, like in almost any computer programming language, reading data from a file can be tricky. You add extra lines of code to tell the computer what to do. Sometimes you can copy and paste these lines from other peoples’ code.

For example, you can follow the pattern in this listing:

         public override void SetLayoutVertical()
         {
             int columnCount = columnWidths.Length;
             int rowCount = preferredRowHeights.Length;

             int cornerY = (int)startCorner / 2;

             float startOffset = 0;
             float requiredSizeWithoutPadding = 0;

             for (int i = 0; i < rowCount; i++)
                 requiredSizeWithoutPadding += preferredRowHeights[i];

             if (rowCount > 1)
                 requiredSizeWithoutPadding += (rowCount - 1) * rowSpacing;

             startOffset = GetStartOffset(1, requiredSizeWithoutPadding);

             if (cornerY == 1)
                 startOffset += requiredSizeWithoutPadding;

             float positionY = startOffset;

             for (int i = 0; i < rowCount; i++)
             {
                 if (cornerY == 1)
                     positionY -= preferredRowHeights[i];

                 for (int j = 0; j < columnCount; j++)
                 {
                     int childIndex = (i * columnCount) + j;

                     // Safeguard against tables with incomplete rows
                     if (childIndex == rectChildren.Count)
                         break;

                     SetChildAlongAxis(rectChildren[childIndex], 1, positionY, preferredRowHeights[i]);
                 }

                 if (cornerY == 1)
                     positionY -= rowSpacing;
                 else
                     positionY += preferredRowHeights[i] + rowSpacing;
             }

             // Set preferredRowHeights to null to free memory
             preferredRowHeights = null;
         }