Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /// <summary>
- /// Finds the maximum length of a key and padd all the rest of the keys to be
- /// the same fixed length, and calls the delegate supplied.
- /// </summary>
- /// <param name="keyValues">The key value pairs.</param>
- /// <param name="extraPadding">Additional number of chars to pad to the keys.</param>
- /// <param name="printer">The delegate to call to print to.</param>
- public static void DoFixedLengthPrinting(IDictionary keyValues, int extraPadding, Action<string, object> printer)
- {
- // Get the length of the longest named argument.
- int maxLength = 0;
- foreach (DictionaryEntry entry in keyValues)
- {
- int keyLen = ((string)entry.Key).Length;
- if (keyLen > maxLength)
- maxLength = keyLen;
- }
- maxLength += extraPadding;
- // Iterate through all the keys and build a fixed length key.
- // e.g. if key is 4 chars and maxLength is 6, add 2 chars using space(' ').
- foreach (DictionaryEntry entry in keyValues)
- {
- string newKeyWithPadding = GetFixedLengthString((string)entry.Key, maxLength, " ");
- printer(newKeyWithPadding, entry.Value);
- }
- }
- /// <summary>
- /// Builds a fixed length string with the maxChars provided.
- /// </summary>
- /// <param name="text">Current string to start with.</param>
- /// <param name="maxChars">Maximum number of characters.</param>
- /// <param name="paddingChar">Padding character.</param>
- /// <returns>Final created string.</returns>
- public static string GetFixedLengthString(string text, int maxChars, string paddingChar)
- {
- int leftOver = maxChars - text.Length;
- string finalText = text;
- for (int ndx = 0; ndx < leftOver; ndx++)
- finalText += paddingChar;
- return finalText;
- }
Advertisement
Add Comment
Please, Sign In to add comment