{"record":{"id":"2f34b864b9250c8c","repo":"dotnet/wpf","slug":"invalidenumargumentexception-argumentname-direction","errorCode":null,"errorMessage":"InvalidEnumArgumentException(argumentName, direction, LogicalDirection)","messagePattern":"InvalidEnumArgumentException\\(argumentName, direction, LogicalDirection\\)","errorType":"validation","errorClass":"InvalidEnumArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Documents/ValidationHelper.cs","lineNumber":73,"sourceCode":"            ArgumentNullException.ThrowIfNull(startPosition);\n            ArgumentNullException.ThrowIfNull(endPosition);\n            if (startPosition.TextContainer != endPosition.TextContainer)\n            {\n                throw new ArgumentException(SR.Format(SR.InDifferentTextContainers, \"startPosition\", \"endPosition\"));\n            }\n            if (startPosition.CompareTo(endPosition) > 0)\n            {\n                throw new ArgumentException(SR.Format(SR.BadTextPositionOrder, \"startPosition\", \"endPosition\"));\n            }\n        }\n\n        // Throws an ArgumentException if direction is not a valid enum.\n        internal static void VerifyDirection(LogicalDirection direction, string argumentName)\n        {\n            if (direction != LogicalDirection.Forward &&\n                direction != LogicalDirection.Backward)\n            {\n                throw new InvalidEnumArgumentException(argumentName, (int)direction, typeof(LogicalDirection));\n            }\n        }\n\n        // Throws an ArgumentException if edge is not a valid enum.\n        internal static void VerifyElementEdge(ElementEdge edge, string param)\n        {\n            if (edge != ElementEdge.BeforeStart &&\n                edge != ElementEdge.AfterStart  &&\n                edge != ElementEdge.BeforeEnd   &&\n                edge != ElementEdge.AfterEnd)\n            {\n                throw new InvalidEnumArgumentException(param, (int)edge, typeof(ElementEdge));\n            }\n        }\n\n        // ...............................................................\n        //\n        // TextSchema Validation","sourceCodeStart":55,"sourceCodeEnd":91,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Documents/ValidationHelper.cs#L55-L91","documentation":"ValidationHelper.VerifyDirection rejects any LogicalDirection value other than Forward or Backward, throwing InvalidEnumArgumentException with the caller-supplied argument name. LogicalDirection is a two-value enum; anything else (e.g. a cast invalid int) is an invalid enum argument.","triggerScenarios":"Passing a LogicalDirection value obtained by casting an out-of-range int (e.g. (LogicalDirection)2) to any API that routes through VerifyDirection.","commonSituations":"Deserializing enum values from config or persisted data where the raw int is unvalidated; arithmetic on enum values.","solutions":["Pass only LogicalDirection.Forward or LogicalDirection.Backward.","Validate/parse the source int with Enum.IsDefined before casting.","Clamp or normalize persisted values at load time."],"exampleFix":"// before\nvar dir = (LogicalDirection)userValue; // userValue = 5\n// after\nvar dir = Enum.IsDefined(typeof(LogicalDirection), userValue) ? (LogicalDirection)userValue : LogicalDirection.Forward;","handlingStrategy":"validation","validationCode":"if (!Enum.IsDefined(typeof(LogicalDirection), value)) throw new InvalidEnumArgumentException(nameof(value), value, typeof(LogicalDirection));","typeGuard":"bool IsValidLogicalDirection(object v) => v is LogicalDirection d && (d == LogicalDirection.Forward || d == LogicalDirection.Backward);","tryCatchPattern":"try { ... } catch (InvalidEnumArgumentException ex) { /* repair or default the direction */ }","preventionTips":["Never cast raw ints to LogicalDirection without Enum.IsDefined","Validate persisted enum values at deserialization time"],"tags":["wpf","enum","invalid-enum-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"}