{"record":{"id":"26e78e2a4df286a2","repo":"dotnet/wpf","slug":"the-value-of-argument-unit-0-is-invalid-for-enum-type","errorCode":null,"errorMessage":"The value of argument 'unit' ({0}) is invalid for Enum type 'TextUnit'.","messagePattern":"The value of argument 'unit' \\((.+?)\\) is invalid for Enum type 'TextUnit'\\.","errorType":"exception","errorClass":"InvalidEnumArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/UIAutomation/UIAutomationClientSideProviders/MS/Internal/AutomationProxies/WindowsEditBoxRange.cs","lineNumber":174,"sourceCode":"\n                        // move start left until we reach a paragraph boundary.\n                        for (; !AtParagraphBoundary(text, Start); Start--);\n\n                        // move end right until we reach a paragraph boundary (different from Start).\n                        End = Math.Min(Math.Max(End, Start + 1), text.Length);\n                        for (; !AtParagraphBoundary(text, End); End++);\n                    } \n                    break;\n\n                case TextUnit.Format:\n                case TextUnit.Page:\n                case TextUnit.Document:\n                    MoveTo(0, _provider.GetTextLength());\n                    break;\n\n                //break;\n                default:\n                    throw new System.ComponentModel.InvalidEnumArgumentException(\"unit\", (int)unit, typeof(TextUnit));\n            }\n        }\n\n        ITextRangeProvider ITextRangeProvider.FindAttribute(int attributeId, object val, bool backwards)\n        {\n            AutomationTextAttribute attribute = AutomationTextAttribute.LookupById(attributeId);\n            // generic controls are plain text so if the attribute matches then it matches over the whole range.\n\n            // To workaround the conversion that Marshaling of COM-interop did.\n            object targetAttribute = GetAttributeValue(attribute);\n            if (targetAttribute is Enum)\n            {\n                targetAttribute = (int)targetAttribute;\n            }\n\n            return val.Equals(targetAttribute) ? new WindowsEditBoxRange(_provider, Start, End) : null;\n        }\n","sourceCodeStart":156,"sourceCodeEnd":192,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/UIAutomation/UIAutomationClientSideProviders/MS/Internal/AutomationProxies/WindowsEditBoxRange.cs#L156-L192","documentation":"ITextRangeProvider.ExpandToEnclosingUnit throws InvalidEnumArgumentException when the TextUnit argument is not one of the defined values (Character/Word/Line/Paragraph/Page/Document). The switch has no handler for unknown unit values.","triggerScenarios":"Calling ExpandToEnclosingUnit (or code paths that call it) with a TextUnit value cast from an unvalidated int, or a TextUnit from a newer/other framework version not recognized by this switch.","commonSituations":"Casting raw int attribute values from config or interop to TextUnit; handling a protocol payload whose unit field is out of the enum's range; copying constants from documentation that don't match this API's enum.","solutions":["Validate that Enum.IsDefined(typeof(TextUnit), value) before calling ExpandToEnclosingUnit.","Only pass literal TextUnit enum members, never raw ints.","Catch InvalidEnumArgumentException at the boundary and map unknown units to a supported one (e.g. Document).","Upgrade/reference the matching UIAutomation assemblies so the TextUnit enum values line up."],"exampleFix":"// before\nvar unit = (TextUnit)rawInt;\nrange.ExpandToEnclosingUnit(unit); // InvalidEnumArgumentException\n// after\nvar unit = Enum.IsDefined(typeof(TextUnit), rawInt) ? (TextUnit)rawInt : TextUnit.Document;\nrange.ExpandToEnclosingUnit(unit);","handlingStrategy":"type-guard","validationCode":"if (!Enum.IsDefined(typeof(TextUnit), value)) throw new ArgumentOutOfRangeException(nameof(value));","typeGuard":"static bool IsValidTextUnit(int v) => v >= (int)TextUnit.Character && v <= (int)TextUnit.Document && Enum.IsDefined(typeof(TextUnit), v);","tryCatchPattern":"try { range.ExpandToEnclosingUnit(unit); }\ncatch (InvalidEnumArgumentException) { range.ExpandToEnclosingUnit(TextUnit.Document); }","preventionTips":["Never cast unvalidated ints to TextUnit.","Use Enum.IsDefined before conversions.","Pin UIAutomation assembly versions so enum values agree."],"tags":["uiautomation","textpattern","enum","invalid-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"}