{"record":{"id":"735dff3d15760eab","repo":"dotnet/wpf","slug":"sr-format-sr-invalidstringthickness-s","errorCode":null,"errorMessage":"SR.Format(SR.InvalidStringThickness, s)","messagePattern":"SR\\.Format\\(SR\\.InvalidStringThickness, s\\)","errorType":"exception","errorClass":"FormatException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/ThicknessConverter.cs","lineNumber":196,"sourceCode":"\n        /// <summary>\n        /// Constructs a <see cref=\"Thickness\"/> struct out of string representation supplied by <paramref name=\"s\"/> and the specified <paramref name=\"cultureInfo\"/>.\n        /// </summary>\n        /// <param name=\"s\">The string representation of a <see cref=\"Thickness\"/> struct.</param>\n        /// <param name=\"cultureInfo\">The <see cref=\"CultureInfo\"/> which was used to format this string.</param>\n        /// <returns>A new instance of <see cref=\"Thickness\"/> struct representing the data contained in <paramref name=\"s\"/>.</returns>\n        /// <exception cref=\"FormatException\">Thrown when <paramref name=\"s\"/> contains invalid string representation.</exception>\n        internal static Thickness FromString(string s, CultureInfo cultureInfo)\n        {\n            TokenizerHelper th = new(s, cultureInfo);\n            Span<double> lengths = stackalloc double[4];\n            int i = 0;\n\n            // Peel off each double in the delimited list.\n            while (th.NextToken())\n            {\n                if (i >= 4) // In case we've got more than 4 doubles, we throw\n                    throw new FormatException(SR.Format(SR.InvalidStringThickness, s));\n\n                lengths[i] = LengthConverter.FromString(th.GetCurrentToken(), cultureInfo);\n                i++;\n            }\n\n            // We have a reasonable interpretation for one value (all four edges),\n            // two values (horizontal, vertical),\n            // and four values (left, top, right, bottom).\n            return i switch\n            {\n                1 => new Thickness(lengths[0]),\n                2 => new Thickness(lengths[0], lengths[1], lengths[0], lengths[1]),\n                4 => new Thickness(lengths[0], lengths[1], lengths[2], lengths[3]),\n                _ => throw new FormatException(SR.Format(SR.InvalidStringThickness, s)),\n            };\n        }\n\n    #endregion","sourceCodeStart":178,"sourceCodeEnd":214,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/ThicknessConverter.cs#L178-L214","documentation":"FromString parses a delimited string of up to four doubles into Thickness components. If the tokenized string contains more than four numeric tokens, it throws FormatException with InvalidStringThickness including the offending string.","triggerScenarios":"Calling ThicknessConverter.ConvertFrom (or Thickness.Parse) with a string like \"1,2,3,4,5\" — five or more comma/space separated numbers.","commonSituations":"User-entered or config-file thickness strings with too many values; concatenation bugs building the string; pasting values with duplicated separators producing extra tokens.","solutions":["Correct the input string to 1, 2, or 4 space/comma separated numbers.","Validate token count (split on ',' / whitespace) before converting.","Catch FormatException around ConvertFrom/Parse and surface a user-facing validation message."],"exampleFix":"// before\nvar t = (Thickness)converter.ConvertFrom(\"1,2,3,4,5\"); // throws\n// after\nvar t = (Thickness)converter.ConvertFrom(\"1,2,3,4\");","handlingStrategy":"validation","validationCode":"var tokens = s.Split(new[]{' ', ','}, StringSplitOptions.RemoveEmptyEntries);\nif (tokens.Length > 4)\n    throw new FormatException($\"Thickness string must have 1, 2, or 4 numbers: '{s}'\");","typeGuard":null,"tryCatchPattern":"try { return (Thickness)converter.ConvertFrom(s); }\ncatch (FormatException ex) when (ex.Message.Contains(s)) { return defaultValue; }","preventionTips":["Normalize/validate thickness strings from user input before parsing.","Use Thickness.Parse with culture-aware input sanitization.","Sanitize duplicated separators when building the string programmatically."],"tags":["wpf","type-converter","format"],"backgroundTag":"invalid-argument-format","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-21T21:30:21.729Z"}