{"record":{"id":"326ad88dda4f4259","repo":"dotnet/wpf","slug":"sr-badtextpositionorder","errorCode":null,"errorMessage":"SR.BadTextPositionOrder","messagePattern":"SR\\.BadTextPositionOrder","errorType":"validation","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Documents/ValidationHelper.cs","lineNumber":63,"sourceCode":"            }\n        }\n\n        // Verifies two positions are safe to use as a logical text span.\n        //\n        // Throws ArgumentNullException if startPosition == null || endPosition == null\n        //        ArgumentException if startPosition.TextContainer != endPosition.TextContainer or\n        //                             startPosition > endPosition\n        internal static void VerifyPositionPair(ITextPointer startPosition, ITextPointer endPosition)\n        {\n            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  &&","sourceCodeStart":45,"sourceCodeEnd":81,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Documents/ValidationHelper.cs#L45-L81","documentation":"ValidationHelper.VerifyPositionPair throws ArgumentException(SR.BadTextPositionOrder) when a start TextPointer compares greater than an end TextPointer. The library requires all start/end position pairs to be ordered so that start <= end; passing a reversed pair would break TextRange/TextSegment invariants. This is a pre-condition check on the public text-position API surface.","triggerScenarios":"Calling any API that takes (startPosition, endPosition) where startPosition.TextContainer == endPosition.TextContainer but startPosition.CompareTo(endPosition) > 0 — e.g. constructing a TextRange or selecting a range with the later position passed as 'start'.","commonSituations":"Constructing ranges from user-selection data where start/end were swapped; computing positions asynchronously and passing them back in reverse order; copying positions from two different selections.","solutions":["Swap the arguments so the earlier TextPointer is passed as startPosition.","Call startPosition.CompareTo(endPosition) before invoking and swap if > 0.","Verify both pointers come from the same TextContainer and represent the intended logical order."],"exampleFix":"// before\nnew TextRange(endPointer, startPointer);\n// after\nif (startPointer.CompareTo(endPointer) > 0) { var t = startPointer; startPointer = endPointer; endPointer = t; }\nnew TextRange(startPointer, endPointer);","handlingStrategy":"validation","validationCode":"if (start == null || end == null) throw new ArgumentNullException(nameof(start));\nif (start.CompareTo(end) > 0) (start, end) = (end, start);","typeGuard":null,"tryCatchPattern":"try { ... } catch (ArgumentException ex) { /* handle reversed/rejected positions */ }","preventionTips":["Always normalize start/end with CompareTo before building ranges","Derive ranges from selections using Selection.Start/End which are already ordered"],"tags":["wpf","argument-validation","textpointer"],"backgroundTag":"invalid-argument-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"}