{"record":{"id":"6f7d693031bd31ae","repo":"dotnet/wpf","slug":"invalidenumargumentexception-substitution-int-value-typeof","errorCode":null,"errorMessage":"InvalidEnumArgumentException(\"Substitution\", (int)value, typeof(NumberSubstitutionMethod))","messagePattern":"InvalidEnumArgumentException\\(\"Substitution\", \\(int\\)value, typeof\\(NumberSubstitutionMethod\\)\\)","errorType":"validation","errorClass":"InvalidEnumArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/textformatting/NumberSubstitution.cs","lineNumber":123,"sourceCode":"        /// <param name=\"value\">CultureInfo object to validate; the type is assumed\n        /// to be CultureInfo as the type is validated by the property engine.</param>\n        /// <returns>Returns true if it's a valid culture, false if not.</returns>\n        private static bool IsValidCultureOverrideValue(object value)\n        {\n            return IsValidCultureOverride((CultureInfo)value);\n        }\n\n        /// <summary>\n        /// Specifies the type of number substitution to perform, if any.\n        /// </summary>\n        public NumberSubstitutionMethod Substitution\n        {\n            get { return _substitution; }\n\n            set\n            {\n                if ((uint)value > (uint)NumberSubstitutionMethod.Traditional)\n                    throw new InvalidEnumArgumentException(\"Substitution\", (int)value, typeof(NumberSubstitutionMethod));\n\n                _substitution = value;\n            }\n        }\n\n        /// <summary>\n        /// DP For CultureSource\n        /// </summary>\n        public static readonly DependencyProperty CultureSourceProperty =\n                    DependencyProperty.RegisterAttached(\n                        \"CultureSource\",\n                        typeof(NumberCultureSource),\n                        typeof(NumberSubstitution));\n\n        /// <summary>\n        /// Setter for NumberSubstitution DependencyProperty\n        /// </summary>\n        public static void SetCultureSource(DependencyObject target, NumberCultureSource value)","sourceCodeStart":105,"sourceCodeEnd":141,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/textformatting/NumberSubstitution.cs#L105-L141","documentation":"NumberSubstitution.Substitution setter validates the value against the NumberSubstitutionMethod enum ((uint)value > NumberSubstitutionMethod.Traditional) and throws InvalidEnumArgumentException(\"Substitution\", ...). Valid values are AsCulture, Context, European, NativeNational, Traditional.","triggerScenarios":"Assigning Substitution an out-of-range integer cast to NumberSubstitutionMethod (e.g. (NumberSubstitutionMethod)9), or restoring a serialized value not defined in the enum.","commonSituations":"Persisting the substitution method as int in config and the stored value exceeds the enum; data migrated from a different framework version; binding a raw slider/combobox index directly to the property.","solutions":["Assign only named enum members such as NumberSubstitutionMethod.Context","Validate integers with Enum.IsDefined(typeof(NumberSubstitutionMethod), v) before casting","Clamp or reject persisted config values outside 0..4 and fall back to Context","Fix the offending serialized/config value to a defined enum number"],"exampleFix":"// before\nnumberSubstitution.Substitution = (NumberSubstitutionMethod)storedInt; // storedInt = 9\n// after\nnumberSubstitution.Substitution = Enum.IsDefined(typeof(NumberSubstitutionMethod), storedInt)\n    ? (NumberSubstitutionMethod)storedInt\n    : NumberSubstitutionMethod.Context;","handlingStrategy":"validation","validationCode":"if (!Enum.IsDefined(typeof(NumberSubstitutionMethod), value)) value = NumberSubstitutionMethod.Context;","typeGuard":"bool IsValidSubstitutionMethod(int v) => v >= 0 && v <= (int)NumberSubstitutionMethod.Traditional;","tryCatchPattern":"try { numberSubstitution.Substitution = method; }\ncatch (InvalidEnumArgumentException ex) { log.Warn(\"Invalid Substitution, using Context\", ex); numberSubstitution.Substitution = NumberSubstitutionMethod.Context; }","preventionTips":["Only assign named NumberSubstitutionMethod members","Validate persisted integers with Enum.IsDefined","Do not bind raw integer inputs directly to the property"],"tags":["wpf","textformatting","enum","numbersubstitution"],"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"}