{"record":{"id":"bc25d2c07f98cad4","repo":"dotnet/wpf","slug":"sr-timing-repeatbehaviornotiterationcount","errorCode":null,"errorMessage":"SR.Timing_RepeatBehaviorNotIterationCount","messagePattern":"SR\\.Timing_RepeatBehaviorNotIterationCount","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Animation/RepeatBehavior.cs","lineNumber":110,"sourceCode":"        public bool HasDuration\n        {\n            get\n            {\n                return _type == RepeatBehaviorType.RepeatDuration;\n            }\n        }\n\n        /// <summary>\n        /// Returns the iteration count specified by this RepeatBehavior.\n        /// </summary>\n        /// <value>The iteration count specified by this RepeatBehavior.</value>\n        /// <exception cref=\"InvalidOperationException\">Thrown if this RepeatBehavior does not represent an iteration count.</exception>\n        public double Count\n        {\n            get\n            {\n                if (!HasCount)\n                    throw new InvalidOperationException(SR.Format(SR.Timing_RepeatBehaviorNotIterationCount, this));\n\n                return _iterationCount;\n            }\n        }\n\n        /// <summary>\n        /// Returns the repeat duration specified by this RepeatBehavior.\n        /// </summary>\n        /// <value>A TimeSpan representing the repeat duration specified by this RepeatBehavior.</value>\n        /// <exception cref=\"InvalidOperationException\">Thrown if this RepeatBehavior does not represent a repeat duration.</exception>\n        public TimeSpan Duration\n        {\n            get\n            {\n                if (!HasDuration)\n                    throw new InvalidOperationException(SR.Format(SR.Timing_RepeatBehaviorNotRepeatDuration, this));\n\n                return _repeatDuration;","sourceCodeStart":92,"sourceCodeEnd":128,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Animation/RepeatBehavior.cs#L92-L128","documentation":"RepeatBehavior.Count returns the iteration count only when the instance represents one (HasCount / KeyTimeType-style IterationCount). Reading Count on a RepeatBehavior created as Forever, a RepeatBehavior(TimeSpan), or the default throws InvalidOperationException with Timing_RepeatBehaviorNotIterationCount.","triggerScenarios":"Reading rb.Count when rb was built with new RepeatBehavior(TimeSpan.FromSeconds(2)), is RepeatBehavior.Forever, or is default(RepeatBehavior). Example: double n = RepeatBehavior.Forever.Count; throws.","commonSituations":"Serializing animation settings without checking the behavior type; UI code that assumes every RepeatBehavior has a count; migrating code that used an older API where count was always set.","solutions":["Guard with if (rb.HasCount) before accessing rb.Count.","Branch on the behavior: HasCount -> Count, HasDuration -> Duration, Forever -> infinite handling.","Do not treat Count as a numeric fallback; use HasCount/HasDuration to pick the property."],"exampleFix":"// before\ndouble count = repeatBehavior.Count;\n// after\ndouble count = repeatBehavior.HasCount ? repeatBehavior.Count : 0;","handlingStrategy":"type-guard","validationCode":"double? count = repeatBehavior.HasCount ? repeatBehavior.Count : (double?)null;","typeGuard":"static bool HasIterationCount(RepeatBehavior rb) => rb.HasCount;\nstatic double? SafeCount(RepeatBehavior rb) => rb.HasCount ? rb.Count : (double?)null;","tryCatchPattern":"double count;\ntry { count = repeatBehavior.Count; }\ncatch (InvalidOperationException) { count = 0; }","preventionTips":["Check HasCount / HasDuration / Forever before reading Count or Duration.","Treat RepeatBehavior as a discriminated union, not a plain struct with always-valid fields.","In serialization code, branch on the behavior type explicitly."],"tags":["wpf","animation","invalid-operation","repeat-behavior"],"backgroundTag":"invalid-state-transition","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-22T01:17:13.364Z"}