/// <summary> /// Parses a string representation of a Lookup value to a LookupFieldValue instance. /// </summary> /// <param name="fieldValue">Field value to parse.</param> /// <returns>LookupFieldValue instance representing the specified string value.</returns> public static LookupFieldValue Parse(string fieldValue) { if (string.IsNullOrEmpty(fieldValue)) { return(null); } LookupFieldValue result = new LookupFieldValue(); if (fieldValue == "***") { result.IsSecret = true; } else { // // Find the delimiter. // int index = fieldValue.IndexOf(";#", StringComparison.Ordinal); // // Set the id. // int id; if (!int.TryParse(fieldValue.Substring(0, index), NumberStyles.Integer, CultureInfo.InvariantCulture, out id)) { throw new ArgumentException("Invalid lookup field identifier.", "fieldValue"); } result._id = id; // // Set the value. // if (index >= 0) { index += 2; if (index < fieldValue.Length) { result._value = fieldValue.Substring(index, fieldValue.Length - index); } } else { result._value = ""; } } return(result); }
/// <summary> /// Parses a string representation of a LookupMulti value to a LookupMultiFieldValue instance. /// </summary> /// <param name="fieldValue">Field value to parse.</param> /// <returns>LookupMultiFieldValue instance representing the specified string value.</returns> public static LookupMultiFieldValue Parse(string fieldValue) { if (string.IsNullOrEmpty(fieldValue)) { return(null); } LookupMultiFieldValue result = new LookupMultiFieldValue(); // // Keep track of found identifiers to eliminate duplicates. // Dictionary <int, bool> values = new Dictionary <int, bool>(); // // Remove trailing ;# if present. // if (fieldValue.StartsWith(";#", StringComparison.Ordinal)) { fieldValue = fieldValue.Substring(2); } // // Find all the id;#value sequences and parse them. // int b = 0; int j; while (b < fieldValue.Length) { // // Look for next occurrence of ;#. // int i = fieldValue.IndexOf(";#", b, StringComparison.Ordinal); // // If the current position (b) and the position where a delimiter is found are the same, there's no id. // In this case we'll need to skip the value part of the pair and move on to the next pair. // bool skip = (i == b); // // No more occurrences of delimiters, take the remainder of the string. // if (i < 0) { j = fieldValue.Length; } // // Delimiter found. Skip it, and try to find a corresponding value by looking for the next delimiter. // else { i += 2; if (i < fieldValue.Length) { j = fieldValue.IndexOf(";#", i, StringComparison.Ordinal); if (j < 0) { j = fieldValue.Length; } } else { j = fieldValue.Length; } } // // If the pair was considered valid, i.e. no absent id part, parse it. // if (!skip) { string t = fieldValue.Substring(b, j - b); if (t != ";#") { try { // // Parse the pair and look for duplicates. // var val = LookupFieldValue.Parse(t); if (!values.ContainsKey(val.Id)) { values.Add(val.Id, true); result._values.Add(val); } } catch (ArgumentException) { } } } // // Get rid of the last delimiter and move on to the next pair. // b = j + 2; } return(result); }