{"record":{"id":"d2c03504b08fcdf9","repo":"dotnet/wpf","slug":"the-value-of-argument-max-max-is-invalid-for-enum-type","errorCode":null,"errorMessage":"The value of argument 'max' (max) is invalid for Enum type 'DispatcherPriority'.","messagePattern":"The value of argument 'max' \\(max\\) 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":277,"sourceCode":"            {\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)\n            {\n                // If we move to a Priority class, this exception will have to change too.\n                throw new System.ComponentModel.InvalidEnumArgumentException(\"max\", (int)max, typeof(DispatcherPriority));\n            }\n            if(max == DispatcherPriority.Inactive)\n            {\n                throw new ArgumentException(SR.InvalidPriority, nameof(max));\n            }\n            \n            if (max < min)\n            {\n                throw new ArgumentException(SR.InvalidPriorityRangeOrder);\n            }\n\n            _min = min;\n            _isMinInclusive = isMinInclusive;\n            _max = max;\n            _isMaxInclusive = isMaxInclusive;\n        }\n\n        // This is a constructor for our special static members.","sourceCodeStart":259,"sourceCodeEnd":295,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/WindowsBase/System/Windows/Threading/PriorityRange.cs#L259-L295","documentation":"PriorityRange.Initialize validates that the 'max' bound is a defined DispatcherPriority between DispatcherPriority.Invalid (-1) and DispatcherPriority.Send (10). Out-of-range values raise InvalidEnumArgumentException naming 'max', its integer value, and typeof(DispatcherPriority).","triggerScenarios":"Calling a PriorityRange constructor with a max value cast from an arbitrary int (e.g. (DispatcherPriority)11 or higher), or from a value produced by arithmetic on enum constants.","commonSituations":"Persisted priority integers from older or newer framework versions; computed priorities like (DispatcherPriority)(normal + 1) that overflow past Send; deserialization of raw ints from XML/JSON config.","solutions":["Ensure the integer cast to DispatcherPriority for max is within -1..10.","Use named DispatcherPriority constants for max (commonly DispatcherPriority.Send as the upper bound).","Validate persisted/config values before constructing the range; reject or clamp out-of-range ints with logging.","Remember Inactive as max is separately rejected with ArgumentException (error 5313)."],"exampleFix":"// before\nvar range = new PriorityRange(DispatcherPriority.Normal, true, (DispatcherPriority)maxRaw, true);\n// after\nif (maxRaw >= (int)DispatcherPriority.Invalid && maxRaw <= (int)DispatcherPriority.Send)\n{\n    var range = new PriorityRange(DispatcherPriority.Normal, true, (DispatcherPriority)maxRaw, true);\n}","handlingStrategy":"validation","validationCode":"static bool IsValidMaxPriority(DispatcherPriority p) =>\n    p >= DispatcherPriority.Invalid && p <= DispatcherPriority.Send && p != DispatcherPriority.Inactive;\n\nif (!IsValidMaxPriority(max)) throw new ArgumentOutOfRangeException(nameof(max), (int)max, \"max must be a valid DispatcherPriority (-1..10, excluding Inactive).\");","typeGuard":"static bool IsWithinPriorityRange(int value) =>\n    value >= (int)DispatcherPriority.Invalid && value <= (int)DispatcherPriority.Send;","tryCatchPattern":"try\n{\n    var range = new PriorityRange(min, true, max, true);\n}\ncatch (System.ComponentModel.InvalidEnumArgumentException ex)\n{\n    logger.LogError(ex, \"PriorityRange max is not a valid DispatcherPriority: {Raw}\", rawMax);\n    throw; // config error — fail fast\n}","preventionTips":["Clamp computed priorities: max = (DispatcherPriority)Math.Min(raw, (int)DispatcherPriority.Send).","Validate ints from persistence before casting.","Use DispatcherPriority.Send as the canonical upper bound."],"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"}