{"record":{"id":"0e28408e398527c9","repo":"dotnet/wpf","slug":"invalidenumargumentexception-value-int-value-typeof-0e2840","errorCode":null,"errorMessage":"InvalidEnumArgumentException(value, (int) value, typeof(ListSortDirection))","messagePattern":"InvalidEnumArgumentException\\(value, \\(int\\) value, typeof\\(ListSortDirection\\)\\)","errorType":"validation","errorClass":"InvalidEnumArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/WindowsBase/System/ComponentModel/SortDescription.cs","lineNumber":73,"sourceCode":"                    throw new InvalidOperationException(SR.Format(SR.CannotChangeAfterSealed, \"SortDescription\"));\n\n                _propertyName = value;\n            }\n        }\n\n        /// <summary>\n        /// Sort direction.\n        /// </summary>\n        public ListSortDirection Direction\n        {\n            get { return _direction; }\n            set\n            {\n                if (_sealed)\n                    throw new InvalidOperationException(SR.Format(SR.CannotChangeAfterSealed, \"SortDescription\"));\n\n                if (value < ListSortDirection.Ascending || value > ListSortDirection.Descending)\n                    throw new InvalidEnumArgumentException(\"value\", (int) value, typeof(ListSortDirection));\n\n                _direction = value;\n            }\n        }\n\n        /// <summary>\n        /// Returns true if the SortDescription is in use (sealed).\n        /// </summary>\n        public bool IsSealed\n        {\n            get { return _sealed; }\n        }\n\n        #endregion Public Properties\n\n        //------------------------------------------------------\n        //\n        //  Public methods","sourceCodeStart":55,"sourceCodeEnd":91,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/WindowsBase/System/ComponentModel/SortDescription.cs#L55-L91","documentation":"The SortDescription.Direction setter validates the assigned value against the ListSortDirection enum (Ascending=0, Descending=1) and throws InvalidEnumArgumentException for any out-of-range integer cast to the enum. This catches invalid enum casts that the C# type system otherwise allows.","triggerScenarios":"Assigning (ListSortDirection)2 or any int outside 0..1 to SortDescription.Direction, typically from an int variable read from config, a database, or user input.","commonSituations":"Mapping a UI toggle or persisted sort-order integer to ListSortDirection without validating the raw value first.","solutions":["Validate the int is 0 or 1 before casting, or use Enum.IsDefined(typeof(ListSortDirection), value).","Clamp or map the source value: value != 0 ? ListSortDirection.Descending : ListSortDirection.Ascending.","Fix the source of the bad integer (config, serialization, UI binding)."],"exampleFix":"// before\nsort.Direction = (ListSortDirection)rawInt; // throws for rawInt=2\n// after\nsort.Direction = Enum.IsDefined(typeof(ListSortDirection), rawInt) ? (ListSortDirection)rawInt : ListSortDirection.Ascending;","handlingStrategy":"validation","validationCode":"if (!Enum.IsDefined(typeof(ListSortDirection), rawInt)) throw new ArgumentOutOfRangeException(nameof(rawInt));","typeGuard":"bool IsValidSortDirection(int v) => v == 0 || v == 1;","tryCatchPattern":"try { desc.Direction = (ListSortDirection)v; } catch (InvalidEnumArgumentException) { desc.Direction = ListSortDirection.Ascending; }","preventionTips":["Validate raw ints with Enum.IsDefined before casting","Avoid casting untrusted integers to enums"],"tags":["wpf","sortdescription","enum","argument-exception"],"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-21T21:30:21.729Z"}