If you have a List<string> with entries:

[0] = Parameter1.Name

[1] = Parameter1.Value

[2] = Parameter2.Name

[3] = Parameter2.Value

and want to convert this list to a string like Parameter1.Name = [Parameter1.Value] Parameter2.Name = [Parameter2.Value], you can use the following code:

 

/// <summary>
/// Convert a List of strings to a string in the format: Variable1.Name = [Variabel1.Value] Variable2.Name = [Variabel2.Value]
/// </summary>
/// <param name="variables">
/// Can't be null
/// Should have an evennumber of items
/// </param>
/// <returns></returns>
public static string GetFormattedLine(List<string> variables)
{
    StringBuilder result = new StringBuilder(string.Empty);

    if (variables == null)
    {
        throw new ArgumentException(“Can't be null”, "variables");
    }

    if (variables.Count % 2 != 0)
    {
        throw new ApplicationException(string.Format(“[{0}] is not an evennumber”, "variabels.Count"));
    }

    for (int i = 0; i < variables.Count; i++)
    {
        if (i % 2 == 0)
        {
            result.AppendFormat("{0} = [{1}] ", variables[i], variables[i + 1]);   
        }
    }

    return result.ToString().Trim();
}

One Comment

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.