{"record":{"id":"b2f069ac632e19c1","repo":"dotnet/wpf","slug":"sr-format-sr-indifferenttextcontainers-start-end","errorCode":null,"errorMessage":"SR.Format(SR.InDifferentTextContainers, \"start\", \"end\")","messagePattern":"SR\\.Format\\(SR\\.InDifferentTextContainers, \"start\", \"end\"\\)","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Documents/Span.cs","lineNumber":92,"sourceCode":"        /// Creates a new Span instance covering existing content.\n        /// </summary>\n        /// <param name=\"start\">\n        /// Start position of the new Span.\n        /// </param>\n        /// <param name=\"end\">\n        /// End position of the new Span.\n        /// </param>\n        /// <remarks>\n        /// start and end must both be parented by the same Paragraph, otherwise\n        /// the method will raise an ArgumentException.\n        /// </remarks>\n        public Span(TextPointer start, TextPointer end)\n        {\n            ArgumentNullException.ThrowIfNull(start);\n            ArgumentNullException.ThrowIfNull(end);\n            if (start.TextContainer != end.TextContainer)\n            {\n                throw new ArgumentException(SR.Format(SR.InDifferentTextContainers, \"start\", \"end\"));\n            }\n            if (start.CompareTo(end) > 0)\n            {\n                throw new ArgumentException(SR.Format(SR.BadTextPositionOrder, \"start\", \"end\"));\n            }\n\n            start.TextContainer.BeginChange();\n            try\n            {\n                start = TextRangeEditTables.EnsureInsertionPosition(start);\n                Invariant.Assert(start.Parent is Run);\n                end = TextRangeEditTables.EnsureInsertionPosition(end);\n                Invariant.Assert(end.Parent is Run);\n\n                if (start.Paragraph != end.Paragraph)\n                {\n                    throw new ArgumentException(SR.Format(SR.InDifferentParagraphs, \"start\", \"end\"));\n                }","sourceCodeStart":74,"sourceCodeEnd":110,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Documents/Span.cs#L74-L110","documentation":"The Span(TextPointer start, TextPointer end) constructor throws ArgumentException with SR.InDifferentTextContainers (\"start\" and \"end\" as parameter names) when the two TextPointers belong to different TextContainers (i.e. different documents/text stores). A Span must cover a range within a single text container, so positions from two different documents cannot define one.","triggerScenarios":"new Span(startPointer, endPointer) where startPointer and endPointer come from two different FlowDocument/TextEditor/TextContainer instances (start.TextContainer != end.TextContainer).","commonSituations":"Copying TextPointers between two RichTextBox/FlowDocument instances and assuming they are interchangeable; caching a pointer from a document that was reloaded so the new pointer pairs with an old-document pointer; diffing or copying ranges across documents.","solutions":["Ensure both TextPointers come from the same document/container before constructing the Span.","If ranges span documents, compute equivalent positions by offset (e.g. ContentStart.GetOffsetToPosition) in the target document instead.","Guard with a check that start.TextContainer == end.TextContainer and throw a clearer domain error first.","Catch ArgumentException and map it to a user-facing 'selection spans documents' message."],"exampleFix":"// before\nvar span = new Span(doc1Start, doc2End);\n// after\nif (doc1Start.TextContainer != doc2End.TextContainer)\n    throw new InvalidOperationException(\"start and end must belong to the same document.\");\nvar span = new Span(doc1Start, doc1End);","handlingStrategy":"validation","validationCode":"if (start == null || end == null) throw new ArgumentNullException(start == null ? nameof(start) : nameof(end));\nif (start.TextContainer != end.TextContainer)\n    throw new ArgumentException(\"start and end must belong to the same TextContainer.\");","typeGuard":"static bool SameContainer(TextPointer a, TextPointer b) => a != null && b != null && a.TextContainer == b.TextContainer;","tryCatchPattern":"try { span = new Span(start, end); }\ncatch (ArgumentException ex) { throw new InvalidOperationException(\"Cannot create a range across two different documents.\", ex); }","preventionTips":["Never mix TextPointers from different FlowDocument/RichTextBox instances","Invalidate cached pointers when a document is reloaded","Convert cross-document ranges by text offset, not by reusing pointers"],"tags":["wpf","documents","textpointer","argument"],"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"}