{"record":{"id":"606cd6824d0bbf0f","repo":"dotnet/wpf","slug":"sr-valueinvalidforenum","errorCode":null,"errorMessage":"SR.ValueInvalidForEnum","messagePattern":"SR\\.ValueInvalidForEnum","errorType":"exception","errorClass":"InvalidEnumArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Window.cs","lineNumber":1120,"sourceCode":"                // this call ends up throwing an exception if accessing\n                // WindowStartupLocation is not allowed\n                VerifyApiSupported();\n\n                return _windowStartupLocation;\n            }\n\n            set\n            {\n                VerifyContextAndObjectState();\n\n                // this call ends up throwing an exception if accessing\n                // WindowStartupLocation is not allowed\n                VerifyApiSupported();\n\n                //validate WindowStartupLocation enum\n                if (!IsValidWindowStartupLocation(value))\n                {\n                    throw new InvalidEnumArgumentException(\"value\", (int)value, typeof( WindowStartupLocation ));\n                }\n                _windowStartupLocation = value;\n            }\n        }\n\n        /// <summary>\n        ///     The DependencyProperty for ShowInTaskbarProperty.\n        ///     Flags:              None\n        ///     Default Value:      true\n        /// </summary>\n        public static readonly DependencyProperty ShowInTaskbarProperty =\n                DependencyProperty.Register(\"ShowInTaskbar\",\n                        typeof(bool),\n                        typeof(Window),\n                        new FrameworkPropertyMetadata(BooleanBoxes.TrueBox,\n                                new PropertyChangedCallback(_OnShowInTaskbarChanged),\n                                new CoerceValueCallback(VerifyAccessCoercion)));\n","sourceCodeStart":1102,"sourceCodeEnd":1138,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Window.cs#L1102-L1138","documentation":"The Window.WindowStartupLocation property setter validates the assigned value against the WindowStartupLocation enum (Manual, CenterScreen, CenterOwner). Passing an out-of-range integer or invalid value throws InvalidEnumArgumentException (SR.ValueInvalidForEnum) for parameter 'value'.","triggerScenarios":"Assigning WindowStartupLocation = (WindowStartupLocation)castInt where the int is not 0/1/2, deserializing a numeric enum from config with an invalid value, or data-binding a raw integer to the property.","commonSituations":"Persisted settings files containing an invalid numeric value; interop or reflection code casting unvalidated ints; hand-edited XAML resource values.","solutions":["Use enum constants (WindowStartupLocation.CenterScreen) instead of raw casts","Validate with Enum.IsDefined before casting stored integers","Clamp or fall back to Manual when a persisted value is out of range","Parse with Enum.TryParse<T> with a defined fallback"],"exampleFix":"// before\nWindowStartupLocation = (WindowStartupLocation)settings.StartupLoc; // 7 -> throws\n// after\nWindowStartupLocation = Enum.IsDefined(typeof(WindowStartupLocation), settings.StartupLoc)\n    ? (WindowStartupLocation)settings.StartupLoc\n    : WindowStartupLocation.Manual;","handlingStrategy":"validation","validationCode":"if (!Enum.IsDefined(typeof(WindowStartupLocation), rawValue)) rawValue = (int)WindowStartupLocation.Manual;","typeGuard":"bool IsValidStartupLocation(int v) => Enum.IsDefined(typeof(WindowStartupLocation), v);","tryCatchPattern":"try { window.WindowStartupLocation = loc; } catch (InvalidEnumArgumentException) { window.WindowStartupLocation = WindowStartupLocation.Manual; }","preventionTips":["Use enum members, never raw int casts","Validate deserialized enum values with Enum.IsDefined","Store enums as strings in settings files"],"tags":["wpf","window","enum","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-21T21:30:21.729Z"}