{"record":{"id":"5816a3124a40de31","repo":"dotnet/wpf","slug":"the-value-of-argument-min-min-is-invalid-for-enum-type","errorCode":null,"errorMessage":"The value of argument 'min' (min) is invalid for Enum type 'DispatcherPriority'.","messagePattern":"The value of argument 'min' \\(min\\) is invalid for Enum type 'DispatcherPriority'\\.","errorType":"exception","errorClass":"InvalidEnumArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/WindowsBase/System/Windows/Threading/PriorityRange.cs","lineNumber":256,"sourceCode":"        }\n        \n        private void Initialize(DispatcherPriority min, bool isMinInclusive, DispatcherPriority max, bool isMaxInclusive) // NOTE: should be Priority\n        {\n            /*\n            if(min == null)\n            {\n                throw new ArgumentNullException(\"min\");\n            }\n            \n            if (!min.IsValid)\n            {\n                throw new ArgumentException(\"Invalid priority.\", \"min\");\n            }\n            */\n            if(min < DispatcherPriority.Invalid || min > DispatcherPriority.Send)\n            {\n                // If we move to a Priority class, this exception will have to change too.\n                throw new System.ComponentModel.InvalidEnumArgumentException(\"min\", (int)min, typeof(DispatcherPriority));\n            }\n            if(min == DispatcherPriority.Inactive)\n            {\n                throw new ArgumentException(SR.InvalidPriority, nameof(min));\n            }\n\n            /*            \n            if(max == null)\n            {\n                throw new ArgumentNullException(\"max\");\n            }\n\n            if (!max.IsValid)\n            {\n                throw new ArgumentException(\"Invalid priority.\", \"max\");\n            }\n            */\n            if(max < DispatcherPriority.Invalid || max > DispatcherPriority.Send)","sourceCodeStart":238,"sourceCodeEnd":274,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/WindowsBase/System/Windows/Threading/PriorityRange.cs#L238-L274","documentation":"PriorityRange.Initialize validates that the 'min' bound is a defined DispatcherPriority between DispatcherPriority.Invalid (-1) and DispatcherPriority.Send (10). If 'min' falls outside that range, an InvalidEnumArgumentException names the offending argument, its integer value, and the enum type. The older ArgumentException('Invalid priority.') path is commented out in favor of this richer exception.","triggerScenarios":"Calling any PriorityRange constructor (which delegates to Initialize) with a min value cast from an arbitrary int (e.g. (DispatcherPriority)20 or (DispatcherPriority)-5), or passing a stale/renamed enum constant that no longer maps to a valid priority.","commonSituations":"Loading priorities from config files or persisted settings where the integer was saved from a different WPF version's enum; interop code computing priorities arithmetically; bindings that deserialize user-supplied ints directly into DispatcherPriority.","solutions":["Check the integer being cast to DispatcherPriority and ensure it lies within the valid range (-1 through 10).","Use only named DispatcherPriority constants (Send, Normal, Background, ContextIdle, ApplicationIdle, SystemIdle, Inactive, Invalid, etc.) instead of raw ints.","If the value comes from config or persistence, validate/clamp it before constructing the PriorityRange and log the raw value for diagnosis.","Note that DispatcherPriority.Inactive is separately rejected with ArgumentException (see error 5311); pick a dispatchable priority if you need active processing."],"exampleFix":"// before\nvar range = new PriorityRange((DispatcherPriority)userValue, true, DispatcherPriority.Send, true);\n// after\nif (userValue >= (int)DispatcherPriority.Invalid && userValue <= (int)DispatcherPriority.Send && userValue != (int)DispatcherPriority.Inactive)\n{\n    var range = new PriorityRange((DispatcherPriority)userValue, true, DispatcherPriority.Send, true);\n}","handlingStrategy":"validation","validationCode":"static bool IsValidMinPriority(DispatcherPriority p) =>\n    p >= DispatcherPriority.Invalid && p <= DispatcherPriority.Send && p != DispatcherPriority.Inactive;\n\nif (!IsValidMinPriority(min)) throw new ArgumentOutOfRangeException(nameof(min), (int)min, \"min must be a dispatchable DispatcherPriority (-1..10, excluding Inactive).\");","typeGuard":"static bool IsDefinedDispatcherPriority(int value) =>\n    Enum.IsDefined(typeof(DispatcherPriority), value) &&\n    value >= (int)DispatcherPriority.Invalid && value <= (int)DispatcherPriority.Send;","tryCatchPattern":"try\n{\n    var range = new PriorityRange(min, isMinInclusive, max, isMaxInclusive);\n}\ncatch (System.ComponentModel.InvalidEnumArgumentException ex)\n{\n    logger.LogError(ex, \"PriorityRange min is not a valid DispatcherPriority\");\n    // fall back to a safe default range\n}","preventionTips":["Use named DispatcherPriority constants, never raw integer casts.","Validate persisted/config integers with Enum.IsDefined before casting.","Remember Inactive and Invalid are sentinels, not schedulable priorities."],"tags":["wpf","dispatcher","enum-validation","argument"],"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"}