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 SetLayoutHorizontal()
         {
             // If no column width is defined, then assign a reasonable default
             if (columnWidths.Length == 0)
                 columnWidths = new float[1] { 0f };

             int columnCount = columnWidths.Length;
             int cornerX = (int)startCorner % 2;

             float startOffset = 0;
             float requiredSizeWithoutPadding = 0;

             // We calculate the actual cell count for cases where the number of children is lesser than the number of columns
             int actualCellCount = Mathf.Min(rectChildren.Count, columnWidths.Length);

             for (int i = 0; i < actualCellCount; i++)
             {
                 requiredSizeWithoutPadding += columnWidths[i];
                 requiredSizeWithoutPadding += columnSpacing;
             }

             requiredSizeWithoutPadding -= columnSpacing;

             startOffset = GetStartOffset(0, requiredSizeWithoutPadding);

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

             float positionX = startOffset;

             for (int i = 0; i < rectChildren.Count; i++)
             {
                 int currentColumnIndex = i % columnCount;

                 // If it's the first cell in the row, reset positionX
                 if (currentColumnIndex == 0)
                     positionX = startOffset;

                 if (cornerX == 1)
                     positionX -= columnWidths[currentColumnIndex];

                 SetChildAlongAxis(rectChildren[i], 0, positionX, columnWidths[currentColumnIndex]);

                 if (cornerX == 1)
                     positionX -= columnSpacing;
                 else
                     positionX += columnWidths[currentColumnIndex] + columnSpacing;
             }
         }