{"record":{"id":"dd077c5e44b42ab2","repo":"AvaloniaUI/Avalonia","slug":"iterationcount-can-t-be-a-negative-number","errorCode":null,"errorMessage":"IterationCount can't be a negative number.","messagePattern":"IterationCount can't be a negative number\\.","errorType":"validation","errorClass":"InvalidCastException","httpStatus":null,"severity":"error","filePath":"src/Avalonia.Base/Animation/IterationCount.cs","lineNumber":165,"sourceCode":"        }\n\n        /// <summary>\n        /// Parses a string to return a <see cref=\"IterationCount\"/>.\n        /// </summary>\n        /// <param name=\"s\">The string.</param>\n        /// <returns>The <see cref=\"IterationCount\"/>.</returns>\n        public static IterationCount Parse(string s)\n        {\n            s = s.ToUpperInvariant().Trim();\n\n            if (s.EndsWith(\"INFINITE\"))\n            {\n                return Infinite;\n            }\n            else\n            {\n                if (s.StartsWith(\"-\"))\n                    throw new InvalidCastException(\"IterationCount can't be a negative number.\");\n\n                var value = ulong.Parse(s, CultureInfo.InvariantCulture);\n\n                return new IterationCount(value);\n            }\n        }\n    }\n}\n","sourceCodeStart":147,"sourceCodeEnd":174,"githubUrl":"https://github.com/AvaloniaUI/Avalonia/blob/11c542726898ae954a1ef668c65ec79ec92ab17d/src/Avalonia.Base/Animation/IterationCount.cs#L147-L174","documentation":"Thrown by IterationCount.Parse when the input string (uppercased, trimmed) starts with '-'. IterationCount stores a ulong, which cannot be negative, so a leading minus sign is rejected up front rather than letting ulong.Parse fail with a less clear message.","triggerScenarios":"Calling IterationCount.Parse(\"-1\") or any string beginning with '-' (and not matching 'Infinite').","commonSituations":"User/UX input of a negative repeat count; sign error in computed count; sentinel value (-1) passed unparsed.","solutions":["Validate that the input is non-negative before parsing.","Map negative/'forever' intents to the 'Infinite' token instead of a negative number.","Use ulong.TryParse and reject <= 0 semantics explicitly in your UI."],"exampleFix":"// before\nvar ic = IterationCount.Parse(\"-1\");\n\n// after\nvar ic = IterationCount.Parse(\"Infinite\");\n// or\nvar ic = IterationCount.Infinite;","handlingStrategy":"validation","validationCode":"if (s.TrimStart().StartsWith('-'))\n    throw new InvalidCastException(\"IterationCount cannot be negative; use 'Infinite'.\");","typeGuard":"static bool IsNonNegativeIterationString(string s) => !s.TrimStart().StartsWith('-');","tryCatchPattern":null,"preventionTips":["Map 'forever'/unbounded intent to the 'Infinite' token, not a negative number.","Validate the input string's sign before parsing.","Use ulong.TryParse and surface a clear UI error for negative input."],"tags":["animation","parsing","validation","iteration-count","avalonia"],"backgroundTag":null,"analyzedSha":"11c542726898ae954a1ef668c65ec79ec92ab17d","analyzedAt":"2026-08-13T11:57:40.261Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}