{"record":{"id":"379597b320abf59f","repo":"dotnet/wpf","slug":"sr-badfixedtextposition","errorCode":null,"errorMessage":"SR.BadFixedTextPosition","messagePattern":"SR\\.BadFixedTextPosition","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Documents/FixedTextContainer.cs","lineNumber":320,"sourceCode":"        #region Internal Methods\n\n        //--------------------------------------------------------------------\n        // Utility Method\n        //---------------------------------------------------------------------\n        internal FixedTextPointer VerifyPosition(ITextPointer position)\n        {\n            ArgumentNullException.ThrowIfNull(position);\n\n            if (position.TextContainer != this)\n            {\n                throw new ArgumentException(SR.Format(SR.NotInAssociatedContainer, \"position\"));\n            }\n\n            FixedTextPointer ftp = position as FixedTextPointer;\n\n            if (ftp == null)\n            {\n                throw new ArgumentException(SR.Format(SR.BadFixedTextPosition, \"position\"));\n            }\n\n            return ftp;\n        }\n\n\n        internal int GetPageNumber(ITextPointer  textPointer)\n        {\n            FixedTextPointer fixedTextPointer = textPointer as FixedTextPointer;\n            int  pageNumber = int.MaxValue;\n\n            if (fixedTextPointer != null)\n            {\n                if (fixedTextPointer.CompareTo(((ITextContainer)this).Start) == 0)\n                {\n                    pageNumber = 0;\n                }\n                else if (fixedTextPointer.CompareTo(((ITextContainer)this).End) == 0)","sourceCodeStart":302,"sourceCodeEnd":338,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Documents/FixedTextContainer.cs#L302-L338","documentation":"FixedTextContainer.VerifyPosition also requires the ITextPointer to be a FixedTextPointer (the container's concrete pointer type). Passing a valid ITextPointer from another implementation throws ArgumentException(SR.BadFixedTextPosition) because only FixedTextPointer instances can be used with this container.","triggerScenarios":"Calling a FixedTextContainer API with an ITextPointer implemented by another text stack (e.g. a FlowDocument's TextPointer or a custom ITextPointer).","commonSituations":"Code written against ITextContainer/ITextPointer abstractions that mixes FlowDocument and FixedDocument pointers; unit tests passing mock pointers; refactoring that changed pointer types.","solutions":["Create pointers via the FixedTextContainer (CreatePointerAtOffset/CreateStaticPointer) instead of other text stacks.","Cast/convert the argument to FixedTextPointer within the owning container before use.","Add an assertion/guard that the pointer type matches before calling.","Catch ArgumentException and fall back to creating a container-native pointer."],"exampleFix":"// before\nITextPointer p = flowDoc.ContentStart; // wrong implementation\nfixedContainer.VerifyPosition(p); // throws\n// after\nFixedTextPointer p = fixedContainer.CreatePointerAtOffset(0, LogicalDirection.Forward);\nfixedContainer.VerifyPosition(p);","handlingStrategy":"type-guard","validationCode":"bool IsFixedPointer(ITextPointer p) => p is FixedTextPointer;","typeGuard":"FixedTextPointer AsFixedPointer(ITextPointer p) => p as FixedTextPointer; // null check before use","tryCatchPattern":"try { container.VerifyPosition(pointer); }\ncatch (ArgumentException) { pointer = container.CreatePointerAtOffset(offset, direction); }","preventionTips":["Program against the concrete FixedTextPointer when working with fixed documents.","Avoid mixing FlowDocument and FixedDocument pointer types.","Add debug assertions checking pointer type at API boundaries."],"tags":["wpf","documents","argument-exception","type-mismatch"],"backgroundTag":"type-mismatch","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"}