{"record":{"id":"cf94007a6e52709c","repo":"dotnet/wpf","slug":"specified-argument-was-out-of-range-of-valid-values-cf9400","errorCode":null,"errorMessage":"Specified argument was out of range of valid values. (Parameter 'value.PageMediaSizeName')","messagePattern":"Specified argument was out of range of valid values\\. \\(Parameter 'value\\.PageMediaSizeName'\\)","errorType":"validation","errorClass":"ArgumentOutOfRangeException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/ReachFramework/PrintConfig/PrtTicket_Public_Simple.cs","lineNumber":795,"sourceCode":"            }\n            set\n            {\n                // first performing input parameter validation\n                if (value != null)\n                {\n                    // either media name or the width/height values need to be specified\n                    if ((value.PageMediaSizeName == null) &&\n                        (value.Width == null || value.Height == null))\n                    {\n                        throw new ArgumentOutOfRangeException(nameof(value));\n                    }\n\n                    // if size name is specified, it needs to be valid name\n                    if ((value.PageMediaSizeName != null) &&\n                        (value.PageMediaSizeName < PrintSchema.PageMediaSizeNameEnumMin ||\n                         value.PageMediaSizeName > PrintSchema.PageMediaSizeNameEnumMax))\n                    {\n                        throw new ArgumentOutOfRangeException(\"value.PageMediaSizeName\");\n                    }\n\n                    // if width or height value is specified, it needs to be positive\n                    if ((value.Width != null) && (value.Width <= 0))\n                    {\n                        throw new ArgumentOutOfRangeException(nameof(value),\n                                  PTUtility.GetTextFromResource(\"ArgumentException.PositiveValue\"));\n                    }\n\n                    if ((value.Height != null) && (value.Height <= 0))\n                    {\n                        throw new ArgumentOutOfRangeException(nameof(value),\n                                  PTUtility.GetTextFromResource(\"ArgumentException.PositiveValue\"));\n                    }\n                }\n\n                // then adding the set operation to the cache (replacing older set operations)\n                if (_setterCache.ContainsKey(CapabilityName.PageMediaSize))","sourceCodeStart":777,"sourceCodeEnd":813,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/ReachFramework/PrintConfig/PrtTicket_Public_Simple.cs#L777-L813","documentation":"The PageMediaSize setter also validates that a supplied PageMediaSizeName lies within PrintSchema.PageMediaSizeNameEnumMin..Max; otherwise it throws ArgumentOutOfRangeException with parameter name 'value.PageMediaSizeName'. The name is stored as an enum-backed value, so an integer-cast name outside the schema range is rejected.","triggerScenarios":"new PageMediaSize((PageMediaSizeName)oddInt, w, h) where oddInt is not a defined PageMediaSizeName; loading a numeric paper-size code from a legacy store and casting it directly.","commonSituations":"Vendor paper codes or localized paper IDs mapped onto PageMediaSizeName by numeric value; settings persisted before enum changes; user-authored config files with hand-typed integers.","solutions":["Check Enum.IsDefined(typeof(PageMediaSizeName), nameValue) before constructing PageMediaSize.","Use only named PageMediaSizeName members (A4, Letter, etc.) sourced from PrintCapabilities.PageMediaSizeCollection.","Prefer constructing PageMediaSize with explicit Width/Height when no named size matches.","Catch ArgumentOutOfRangeException and retry with a default named size."],"exampleFix":"// before\nticket.PageSize = new PageMediaSize((PageMediaSizeName)paperCode, w, h); // paperCode=999\n\n// after\nticket.PageSize = Enum.IsDefined(typeof(PageMediaSizeName), paperCode)\n    ? new PageMediaSize((PageMediaSizeName)paperCode, w, h)\n    : new PageMediaSize(w, h);","handlingStrategy":"validation","validationCode":"public static bool IsValidMediaSizeName(PageMediaSizeName? n) => n == null || Enum.IsDefined(typeof(PageMediaSizeName), n.Value);","typeGuard":"static bool IsMediaSizeName(int raw) => Enum.IsDefined(typeof(PageMediaSizeName), raw);","tryCatchPattern":"try { ticket.PageSize = new PageMediaSize(name, w, h); }\ncatch (ArgumentOutOfRangeException) { ticket.PageSize = new PageMediaSize(w, h); }","preventionTips":["Validate names with Enum.IsDefined before constructing.","Prefer names pulled from the printer's capabilities.","Fall back to explicit Width/Height for unknown names."],"tags":["wpf","printing","argument-out-of-range","enum"],"backgroundTag":"argument-out-of-range","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"}