{"record":{"id":"a7ce173026ffde59","repo":"dotnet/wpf","slug":"invalidenumargumentexception-defaultresult-int-defaultresult","errorCode":null,"errorMessage":"InvalidEnumArgumentException (\"defaultResult\", (int)defaultResult, typeof(MessageBoxResult))","messagePattern":"InvalidEnumArgumentException \\(\"defaultResult\", \\(int\\)defaultResult, typeof\\(MessageBoxResult\\)\\)","errorType":"exception","errorClass":"InvalidEnumArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/MessageBox.cs","lineNumber":380,"sourceCode":"            IntPtr owner,\n            string messageBoxText,\n            string caption,\n            MessageBoxButton button,\n            MessageBoxImage icon,\n            MessageBoxResult defaultResult,\n            MessageBoxOptions options)\n        {\n            if (!IsValidMessageBoxButton(button))\n            {\n                throw new InvalidEnumArgumentException (\"button\", (int)button, typeof(MessageBoxButton));\n            }\n            if (!IsValidMessageBoxImage(icon))\n            {\n                throw new InvalidEnumArgumentException (\"icon\", (int)icon, typeof(MessageBoxImage));\n            }\n            if (!IsValidMessageBoxResult(defaultResult))\n            {\n                throw new InvalidEnumArgumentException (\"defaultResult\", (int)defaultResult, typeof(MessageBoxResult));\n            }\n            if (!IsValidMessageBoxOptions(options))\n            {\n                throw new InvalidEnumArgumentException(\"options\", (int)options, typeof(MessageBoxOptions));\n            }\n\n            // UserInteractive??\n            //\n            /*if (!SystemInformation.UserInteractive && (options & (MessageBoxOptions.ServiceNotification | MessageBoxOptions.DefaultDesktopOnly)) == 0) {\n                throw new InvalidOperationException(\"UNDONE: SR.GetString(SR.CantShowModalOnNonInteractive)\");\n            }*/\n\n            if ( (options & (MessageBoxOptions.ServiceNotification | MessageBoxOptions.DefaultDesktopOnly)) != 0)\n            {\n                if (owner != IntPtr.Zero)\n                {\n                    throw new ArgumentException(SR.CantShowMBServiceWithOwner);\n                }","sourceCodeStart":362,"sourceCodeEnd":398,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/MessageBox.cs#L362-L398","documentation":"MessageBox.ShowCore validates that defaultResult is a defined MessageBoxResult value (None, OK, Cancel, Yes, No). This parameter sets the dialog's default button; an undefined value raises InvalidEnumArgumentException naming parameter \"defaultResult\".","triggerScenarios":"Calling a MessageBox.Show overload that takes a defaultResult with an undefined value, e.g. (MessageBoxResult)99 from a config int, or passing a MessageBoxButton/other enum in the defaultResult slot.","commonSituations":"Persisting last-used dialog result and feeding it back as default; mixing up MessageBoxResult with MessageBoxButton (both small enums); computing the value from a switch with a bad default.","solutions":["Pass a defined MessageBoxResult value: None, OK, Cancel, Yes, or No.","Validate external values with Enum.IsDefined(typeof(MessageBoxResult), value) before calling Show.","Match defaultResult to the button set: e.g. don't use MessageBoxResult.Yes with MessageBoxButton.OKCancel."],"exampleFix":"// before\nMessageBox.Show(msg, title, MessageBoxButton.YesNo, MessageBoxImage.Question, (MessageBoxResult)lastInt);\n// after\nvar def = Enum.IsDefined(typeof(MessageBoxResult), lastInt) && lastInt != (int)MessageBoxResult.None ? (MessageBoxResult)lastInt : MessageBoxResult.No;\nMessageBox.Show(msg, title, MessageBoxButton.YesNo, MessageBoxImage.Question, def);","handlingStrategy":"validation","validationCode":"bool valid = Enum.IsDefined(typeof(MessageBoxResult), defaultResult);\nif (!valid) defaultResult = MessageBoxResult.None;","typeGuard":"bool IsValidResult(MessageBoxResult r) => r is MessageBoxResult.None or MessageBoxResult.OK or MessageBoxResult.Cancel or MessageBoxResult.Yes or MessageBoxResult.No;","tryCatchPattern":"try { MessageBox.Show(msg, title, button, icon, defaultResult); }\ncatch (InvalidEnumArgumentException ex) when (ex.ParamName == \"defaultResult\") { MessageBox.Show(msg, title, button, icon, MessageBoxResult.None); }","preventionTips":["Keep MessageBoxResult and MessageBoxButton enums separate","Make defaultResult consistent with the button set (e.g. Yes only with YesNo*)","Validate persisted results before reusing them"],"tags":["wpf","messagebox","enum","argument-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-22T01:17:13.364Z"}