/// <summary> /// Parses the comma-delimited data into a series of double values. /// </summary> /// <remarks> /// The collection contains two kinds of values. /// OtherItemDataCsvDouble items contain double values /// OtherItemDataCsvEscape items contain escapes /// </remarks> /// <returns>A collection of OtherItemDataCsvItem</returns> protected Collection <OtherItemDataCsvItem> GetAsDouble() { Collection <OtherItemDataCsvItem> stringValues = GetAsString(); Collection <OtherItemDataCsvItem> values = new Collection <OtherItemDataCsvItem>(); foreach (OtherItemDataCsvItem item in stringValues) { OtherItemDataCsvString itemString = item as OtherItemDataCsvString; if (itemString != null) { double value; try { value = double.Parse(itemString.Value); } catch (FormatException) { throw new InvalidOperationException(Resources.OtherItemDataInvalidNumber); } values.Add(new OtherItemDataCsvDouble(value)); } else { values.Add(item); } } return(values); }
/// <summary> /// Create the comma-delimited representation of a set of data and escapes. /// </summary> /// <remarks> /// The escapes are inserted into the comma-delimited list in the appropriate places. /// Any escape that occurs after the last element is ignored. /// </remarks> /// <param name="values">The collection of values to store.</param> /// <exception cref="ArgumentNullException"> /// If the parameter <paramref name="values"/> is null. /// </exception> protected void SetOtherData(IList <OtherItemDataCsvItem> values) { Validator.ThrowIfArgumentNull(values, nameof(values), Resources.OtherItemDataValuesNull); StringBuilder builder = new StringBuilder(); int currentItemIndex = 0; foreach (OtherItemDataCsvItem item in values) { if (currentItemIndex != 0) { builder.Append(","); } OtherItemDataCsvEscape itemEscape = item as OtherItemDataCsvEscape; if (itemEscape != null) { string name = itemEscape.Name.Replace("=", @"\="); string value = itemEscape.Value.Replace("=", @"\="); builder.Append(name); builder.Append("="); builder.Append(value); } OtherItemDataCsvDouble itemDouble = item as OtherItemDataCsvDouble; if (itemDouble != null) { builder.Append(itemDouble.Value.ToString()); } OtherItemDataCsvString itemString = item as OtherItemDataCsvString; if (itemString != null) { string value = itemString.Value.Replace("=", @"\="); value = itemString.Value.Replace(",", @"\,"); builder.Append(value); } currentItemIndex++; } Data = builder.ToString(); ContentType = "text/csv"; ContentEncoding = string.Empty; }