{"record":{"id":"e06b2a6850d3774c","repo":"dotnet/wpf","slug":"sr-rect-cannotcallmethod","errorCode":null,"errorMessage":"SR.Rect_CannotCallMethod","messagePattern":"SR\\.Rect_CannotCallMethod","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/WindowsBase/System/Windows/Rect.cs","lineNumber":582,"sourceCode":"\n        /// <summary>\n        /// Union - Return the result of the union of rect and point.\n        /// </summary>\n        public static Rect Union(Rect rect, Point point)\n        {\n            rect.Union(new Rect(point, point));\n            return rect;\n        }\n\n        /// <summary>\n        /// Offset - translate the Location by the offset provided.\n        /// If this is Empty, this method is illegal.\n        /// </summary>\n        public void Offset(Vector offsetVector)\n        {\n            if (IsEmpty)\n            {\n                throw new System.InvalidOperationException(SR.Rect_CannotCallMethod);\n            }\n\n            _x += offsetVector._x;\n            _y += offsetVector._y;\n        }\n\n        /// <summary>\n        /// Offset - translate the Location by the offset provided\n        /// If this is Empty, this method is illegal.\n        /// </summary>\n        public void Offset(double offsetX, double offsetY)\n        {\n            if (IsEmpty)\n            {\n                throw new System.InvalidOperationException(SR.Rect_CannotCallMethod);\n            }\n\n            _x += offsetX;","sourceCodeStart":564,"sourceCodeEnd":600,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/WindowsBase/System/Windows/Rect.cs#L564-L600","documentation":"Rect.Offset(Vector) throws InvalidOperationException when the rect is Empty. There is no defined position to offset for the NaN-based empty sentinel, and Offset would overwrite X/Y with NaN-derived values, so WPF declares the operation illegal on Empty (the XML docs state 'If this is Empty, this method is illegal').","triggerScenarios":"Calling rect.Offset(vector) or rect.Offset(dx, dy) when rect == Rect.Empty — e.g. panning/moving a rect that came from an empty hit-test or intersection result.","commonSituations":"Viewport panning code moving a selection rect that was never set; animation updating offset each frame while the rect starts as Empty; chaining Offset after Intersect without checking overlap.","solutions":["Guard with if (!rect.IsEmpty) before calling Offset.","If the rect should exist, initialize it with a real Rect(x, y, w, h) instead of Rect.Empty/default.","Assign a new rect at the target position: rect = new Rect(newPoint, rect.Size) once non-empty."],"exampleFix":"// before\nviewport.Offset(panVector); // InvalidOperationException when viewport is Empty\n// after\nif (!viewport.IsEmpty) { viewport.Offset(panVector); } else { viewport = new Rect(panVector, initialSize); }","handlingStrategy":"validation","validationCode":"if (!rect.IsEmpty) { rect.Offset(offsetVector); }","typeGuard":"static bool CanOffset(Rect r) => !r.IsEmpty;","tryCatchPattern":"try { rect.Offset(v); }\ncatch (InvalidOperationException) { rect = new Rect((Point)((Vector)v), new Size(0, 0)); }","preventionTips":["Always check IsEmpty before Offset/IntersectWith-style mutations.","If accumulating bounds, use Rect.Union which tolerates Empty operands.","Initialize viewport/selection rects with real geometry before panning logic runs."],"tags":["wpf","rect","invalid-state","empty-rect","offset"],"backgroundTag":"invalid-state-transition","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"}