{"record":{"id":"74db471d6f8f3b9c","repo":"dotnet/wpf","slug":"sr-cannotconvertstringtotype","errorCode":null,"errorMessage":"SR.CannotConvertStringToType","messagePattern":"SR\\.CannotConvertStringToType","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/CharacterMetrics.cs","lineNumber":165,"sourceCode":"                    ++j;\n\n                // Let k be end-of-field without trailing whitespace.\n                int k = j;\n                while (k > i && s[k - 1] == ' ')\n                    --k;\n\n                if (k > i)\n                {\n                    // Non-empty field; convert it to double.\n                    ReadOnlySpan<char> field = s.AsSpan(i, k - i);\n                    if (!double.TryParse(\n                        field,\n                        NumberStyles.AllowDecimalPoint | NumberStyles.AllowLeadingSign,\n                        System.Windows.Markup.TypeConverterHelper.InvariantEnglishUS,\n                        out metrics[fieldIndex]\n                        ))\n                    {\n                        throw new ArgumentException(SR.Format(SR.CannotConvertStringToType, field.ToString(), \"double\"));\n                    }\n                }\n                else if (fieldIndex < NumRequiredFields)\n                {\n                    // Empty field; make sure it's an optional one.\n                    throw new ArgumentException(SR.CharacterMetrics_MissingRequiredField);\n                }\n\n                ++fieldIndex;\n\n                if (j < s.Length)\n                {\n                    // There's a comma so check if we've exceeded the number of fields.\n                    if (fieldIndex == NumFields)\n                        throw new ArgumentException(SR.CharacterMetrics_TooManyFields);\n\n                    // Initialize character index for next iteration.\n                    i = j + 1;","sourceCodeStart":147,"sourceCodeEnd":183,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/CharacterMetrics.cs#L147-L183","documentation":"CharacterMetrics.ParseMetrics splits a semicolon-separated metrics string into fields and parses each as an invariant-culture double. When double.TryParse fails for a required field, it throws ArgumentException with SR.CannotConvertStringToType naming the field and target type 'double'.","triggerScenarios":"A metrics string field containing non-numeric text, wrong decimal separator (e.g. ',' instead of '.'), or extra characters in a required field position.","commonSituations":"Locale-edited font metric files (comma decimal separators); truncated or concatenated fields shifting numeric values; whitespace or units ('px') inside numeric fields.","solutions":["Correct the field to be an invariant-culture double (use '.' as decimal separator, optional leading sign).","Re-export the metrics file with InvariantCulture formatting.","Pre-validate each field with double.TryParse(s, NumberStyles.AllowDecimalPoint | AllowLeadingSign, CultureInfo.InvariantCulture, out _) before construction."],"exampleFix":"// before\nnew CharacterMetrics(\"0,1,5;0,75;0;0;0;0;0\"); // '1,5' not parseable invariantly\n\n// after\nnew CharacterMetrics(\"0,1.5;0,75;0;0;0;0;0\");","handlingStrategy":"validation","validationCode":"static bool IsInvariantDouble(string s) =>\n    double.TryParse(s, NumberStyles.AllowDecimalPoint | NumberStyles.AllowLeadingSign,\n        System.Windows.Markup.TypeConverterHelper.InvariantEnglishUS, out _);","typeGuard":null,"tryCatchPattern":"try { var m = new CharacterMetrics(str); }\ncatch (ArgumentException ex) when (ex.Message.Contains(\"double\")) { Log($\"Bad numeric field: {ex.Message}\"); m = null; }","preventionTips":["Always write metric files using invariant culture ('.' decimal separator, no thousands separators).","Pre-parse and validate each field with double.TryParse(InvariantCulture) before constructing.","Strip whitespace/units from numeric fields at import."],"tags":["wpf","parsing","typography","culture"],"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"}