dotnet/wpf · error · ArgumentException

SR.Size3D_DimensionCannotBeNegative

Error message

SR.Size3D_DimensionCannotBeNegative

What it means

The Rect3D(x, y, z, sizeX, sizeY, sizeZ) constructor validates that no size dimension is negative; a Rect3D's extent along any axis cannot be less than zero. If any of sizeX, sizeY, or sizeZ is negative it throws ArgumentException with Size3D_DimensionCannotBeNegative.

Solutions

  1. Clamp each size with Math.Max(0, size) before constructing the Rect3D.
  2. Fix the underlying computation so sizes are max - min of the correct endpoints.
  3. Order the two corner points first (take Min/Max per axis) and derive sizes from the ordered corners.
  4. Validate input data and reject or sanitize negative dimensions upstream.

Example fix

// before
var r = new Rect3D(p.X, p.Y, p.Z, a.X - b.X, a.Y - b.Y, a.Z - b.Z); // may be negative
// after
var r = new Rect3D(p.X, p.Y, p.Z, Math.Abs(a.X - b.X), Math.Abs(a.Y - b.Y), Math.Abs(a.Z - b.Z));
Defensive patterns

Strategy: validation

Validate before calling

if (sizeX < 0 || sizeY < 0 || sizeZ < 0) throw new ArgumentException("Rect3D dimensions cannot be negative");
var r = new Rect3D(x, y, z, sizeX, sizeY, sizeZ);

Type guard

static bool IsValidSize(double sx, double sy, double sz) => sx >= 0 && sy >= 0 && sz >= 0;

Try / catch

try { var r = new Rect3D(x, y, z, sx, sy, sz); }
catch (ArgumentException ex) when (ex.Message.Contains("negative"))
{
    var r = new Rect3D(x, y, z, Math.Max(0, sx), Math.Max(0, sy), Math.Max(0, sz));
}

Prevention

When it happens

Trigger: new Rect3D(x, y, z, sizeX, sizeY, sizeZ) where any size argument is < 0, e.g. new Rect3D(0,0,0, -5, 10, 10).

Common situations: Computing a size by subtracting points in the wrong order (max - min inverted); passing uninitialized/negative computed widths from layout math; data-driven dimensions from files or user input that can be negative.

Related errors


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

Appendix: source

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

            }
            Debug.Assert(size.IsEmpty == IsEmpty);
        }

        /// <summary>
        /// Constructor which sets the initial values to the values of the parameters.
        /// SizeX, sizeY, sizeZ must be non-negative.
        /// </summary>
        /// <param name="x">Value of the X location coordinate of the new rectangle.</param>
        /// <param name="y">Value of the X location coordinate of the new rectangle.</param>
        /// <param name="z">Value of the X location coordinate of the new rectangle.</param>
        /// <param name="sizeX">Size of the new rectangle in X dimension.</param>
        /// <param name="sizeY">Size of the new rectangle in Y dimension.</param>
        /// <param name="sizeZ">Size of the new rectangle in Z dimension.</param>
        public Rect3D(double x, double y, double z, double sizeX, double sizeY, double sizeZ)
        {
            if (sizeX < 0 || sizeY < 0 || sizeZ < 0)
            {
                throw new System.ArgumentException(SR.Size3D_DimensionCannotBeNegative);
            }

            _x = x;
            _y = y;
            _z = z;
            _sizeX = sizeX;
            _sizeY = sizeY;
            _sizeZ = sizeZ;
        }

        /// <summary>
        /// Constructor which sets the initial values to bound the two points provided.
        /// </summary>
        /// <param name="point1">First point.</param>
        /// <param name="point2">Second point.</param>
        internal Rect3D(Point3D point1, Point3D point2)
        {
            _x = Math.Min(point1._x, point2._x);

View on GitHub (pinned to 81131a70a4)