{"record":{"id":"fa8cbe56ec9d41e4","repo":"dotnet/wpf","slug":"sr-baddistance","errorCode":null,"errorMessage":"SR.BadDistance","messagePattern":"SR\\.BadDistance","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Documents/DocumentSequenceTextPointer.cs","lineNumber":785,"sourceCode":"        }\n\n        /// <summary>\n        /// <see cref=\"ITextPointer.CreatePointer(int,LogicalDirection)\"/>\n        /// </summary>\n        public static ITextPointer CreatePointer(DocumentSequenceTextPointer thisTp, int distance, LogicalDirection gravity)\n        {\n            ValidationHelper.VerifyDirection(gravity, \"gravity\");\n\n            // Special case for common case of distance == 0\n            // to avoid calculating child container size, which\n            // could be expansive especially in case where child\n            // container requires virtualization.\n            DocumentSequenceTextPointer newTp = new DocumentSequenceTextPointer(thisTp.ChildBlock, thisTp.ChildPointer.CreatePointer(gravity));\n            if (distance != 0)\n            {\n                if (!xGapAwareScan(newTp, distance))\n                {\n                    throw new ArgumentException(SR.BadDistance, nameof(distance));\n                }\n            }\n            return newTp;\n        }\n\n        #endregion TextPointer Methods\n\n        //------------------------------------------------------\n        //\n        //  Public Properties\n        //\n        //------------------------------------------------------\n\n\n        //------------------------------------------------------\n        //\n        //  Public Events\n        //","sourceCodeStart":767,"sourceCodeEnd":803,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Documents/DocumentSequenceTextPointer.cs#L767-L803","documentation":"DocumentSequenceTextPointer.CreatePointer throws ArgumentException (BadDistance) when the requested offset distance cannot be honored by scanning the text container. xGapAwareScan returns false when the distance overshoots the content or cannot be achieved respecting gaps between child pages, so the call fails rather than returning a pointer at an undefined position.","triggerScenarios":"Calling CreatePointer(distance) on a DocumentSequenceTextPointer with a distance larger than the remaining symbol count (or smaller than the available backward distance) in the sequence — the virtualized multi-page container cannot place a pointer that far away.","commonSituations":"Advancing a pointer by a fixed stride without checking remaining document length; distances computed from positions in a different text container; documents with gap (page-boundary) regions that make nominal distances unrecoverable.","solutions":["Clamp distance to the container's symbol count (e.g. via the symbol offsets of the start/end pointers) before calling.","Use a smaller step size and iterate moves, checking position between steps.","Catch ArgumentException and fall back to a CreatePointer() at the current position."],"exampleFix":"// before\nvar p = docPointer.CreatePointer(bigDistance);\n// after\nvar distance = Math.Min(bigDistance, Math.Abs(endOffset - currentOffset));\nvar p = docPointer.CreatePointer(distance);","handlingStrategy":"validation","validationCode":"var max = Math.Abs(docPointer.TextContainerEndOffset - docPointer.Offset); distance = Math.Clamp(requested, -max, max);","typeGuard":null,"tryCatchPattern":"try { p = pointer.CreatePointer(distance); } catch (ArgumentException) { p = pointer.CreatePointer(); /* stay at current position */ }","preventionTips":["Clamp distance against the container's symbol range before calling.","Advance large distances in bounded steps.","Don't reuse distances computed for a different text container."],"tags":["textpointer","argument-validation","wpf"],"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"}