{"record":{"id":"679ec5643b0d110a","repo":"dotnet/wpf","slug":"invalidenumargumentexception-action-int-action-typeof-679ec5","errorCode":null,"errorMessage":"InvalidEnumArgumentException(\"action\", (int)action, typeof(AnnotationAction))","messagePattern":"InvalidEnumArgumentException\\(\"action\", \\(int\\)action, typeof\\(AnnotationAction\\)\\)","errorType":"exception","errorClass":"InvalidEnumArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Annotations/AnnotationAuthorChangedEventArgs.cs","lineNumber":56,"sourceCode":"        #region Constructors\n\n        /// <summary>\n        ///     Creates an instance of AuthorChangedEventArgs.\n        /// </summary>\n        /// <param name=\"annotation\">the Annotation firing the event</param>\n        /// <param name=\"action\">the action taken on the Author</param>\n        /// <param name=\"author\">the Author that was changed</param>\n        /// <exception cref=\"ArgumentNullException\">annotation is null</exception>\n        /// <exception cref=\"InvalidEnumArgumentException\">action is not a valid value from AnnotationAction</exception>\n        public AnnotationAuthorChangedEventArgs(Annotation annotation, AnnotationAction action, Object author)\n        {\n            // The author parameter can be null here - it is possible to add a null to\n            // the list of authors and we must fire an event signalling a change in the collection.\n\n            ArgumentNullException.ThrowIfNull(annotation);\n            if (action < AnnotationAction.Added || action > AnnotationAction.Modified)\n            {\n                throw new InvalidEnumArgumentException(\"action\", (int)action, typeof(AnnotationAction));\n            }\n\n            _annotation = annotation;\n            _author = author;\n            _action = action;\n        }\n\n        #endregion Constructors\n\n        //------------------------------------------------------\n        //\n        //  Public Properties\n        //\n        //------------------------------------------------------\n\n        #region Public Properties\n\n        /// <summary>","sourceCodeStart":38,"sourceCodeEnd":74,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Annotations/AnnotationAuthorChangedEventArgs.cs#L38-L74","documentation":"The AnnotationAuthorChangedEventArgs constructor throws InvalidEnumArgumentException when the action parameter is outside the valid AnnotationAction range (Added..Modified). This guards event args used for AuthorChanged events so subscribers always receive a defined action value.","triggerScenarios":"Constructing AnnotationAuthorChangedEventArgs with an undefined AnnotationAction value, e.g. from an unchecked int cast ((AnnotationAction)42), a default-initialized field, or computed enum arithmetic producing an out-of-range value.","commonSituations":"Custom AnnotationStore implementations raising AuthorChanged events with bad enum values; deserializing persisted int action codes without validation; refactorings that changed enum members leaving stale numeric values.","solutions":["Pass only defined AnnotationAction members (Added, Removed, Modified) when constructing the args.","Validate with Enum.IsDefined(typeof(AnnotationAction), value) before constructing.","Fix unchecked int-to-AnnotationAction casts in custom store code.","Audit persisted action codes and map them to valid current enum members."],"exampleFix":"// before\nvar args = new AnnotationAuthorChangedEventArgs(annotation, (AnnotationAction)code, author);\n\n// after\nif (!Enum.IsDefined(typeof(AnnotationAction), code)) throw new InvalidDataException(\"bad action code\");\nvar args = new AnnotationAuthorChangedEventArgs(annotation, (AnnotationAction)code, author);","handlingStrategy":"validation","validationCode":"if (!Enum.IsDefined(typeof(AnnotationAction), action))\n    throw new InvalidDataException($\"Invalid AnnotationAction: {action}\");\nvar args = new AnnotationAuthorChangedEventArgs(annotation, action, author);","typeGuard":"bool IsValidAuthorAction(AnnotationAction a) =>\n    a is AnnotationAction.Added or AnnotationAction.Removed or AnnotationAction.Modified;","tryCatchPattern":"try { RaiseAuthorChanged(new AnnotationAuthorChangedEventArgs(a, act, author)); }\ncatch (InvalidEnumArgumentException ex) { log(ex); }","preventionTips":["Validate AnnotationAction values before constructing event args.","Avoid int casts from legacy persisted codes without mapping.","Cover AuthorChanged raising code with unit tests asserting valid enum values."],"tags":["wpf","annotations","enum","argument-validation"],"backgroundTag":"invalid-enum-value","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"}