dotnet/wpf · error · InvalidOperationException

SR.Rect3D_CannotModifyEmptyRect

Error message

SR.Rect3D_CannotModifyEmptyRect

What it means

Rect3D.Location cannot be set while the rectangle is Empty (Rect3D.Empty, whose fields are all positive-infinity sentinels). Because an empty rect has no defined position, assigning a location first would leave it inconsistent, so the setter throws InvalidOperationException with Rect3D_CannotModifyEmptyRect.

Solutions

  1. Assign the whole rect instead: rect = new Rect3D(point, size) rather than setting Location on the empty rect.
  2. Replace the empty value with a new non-empty Rect3D before mutating Location.
  3. Check rect.IsEmpty before assigning and construct a fresh instance in that branch.
  4. Track your own 'has bounds' flag and construct the Rect3D from scratch once the first point/size is known.

Example fix

// before
Rect3D r = Rect3D.Empty;
r.Location = new Point3D(1,2,3); // InvalidOperationException
// after
Rect3D r = new Rect3D(new Point3D(1,2,3), new Size3D(0,0,0));
Defensive patterns

Strategy: validation

Validate before calling

if (rect.IsEmpty) rect = new Rect3D(newPoint, new Size3D(0,0,0)); else rect.Location = newPoint;

Type guard

static bool CanMutate(this Rect3D r) => !r.IsEmpty;

Try / catch

try { rect.Location = p; }
catch (InvalidOperationException ex) when (ex.Message.Contains("empty"))
{
    rect = new Rect3D(p, new Size3D(0,0,0));
}

Prevention

When it happens

Trigger: rect.Location = new Point3D(...) where rect == Rect3D.Empty (e.g. created via the default ctor or Rect3D.Empty).

Common situations: Starting from Rect3D.Empty and trying to grow it by assigning Location/Size directly; properties initialized to Rect3D.Empty then mutated; union/bounds results that came back empty being modified.

Understand the failure class

Background: "Invalid state transition" errors: "status must be X, actually Y", "already rejected/charging/uninstalled", "cannot ... while running" — what they mean when a library rejects your call — this error's family across 31 libraries.

Related errors


AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14). Data as JSON: /api/errors/cb84689922deaf84. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media3D/Rect3D.cs:143

            {
                return _sizeX < 0;
            }
        }

        /// <summary>
        /// The point representing the origin of the rectangle.
        /// </summary>
        public Point3D Location
        {
            get
            {
                return new Point3D(_x, _y, _z);
            }
            set
            {
                if (IsEmpty)
                {
                    throw new System.InvalidOperationException(SR.Rect3D_CannotModifyEmptyRect);
                }

                _x = value._x;
                _y = value._y;
                _z = value._z;
            }
        }

        /// <summary>
        /// The size representing the area of the rectangle.
        /// </summary>
        public Size3D Size
        {
            get
            {
                if( IsEmpty )
                    return Size3D.Empty;
                else

View on GitHub (pinned to 81131a70a4)