stride3d/stride · error · ArgumentOutOfRangeException

There must be four and only four input values for Vector4.

Error message

There must be four and only four input values for Vector4.

What it means

The Vector4(float[] values) constructor requires exactly four elements to fill X, Y, Z, W. When values.Length != 4 it throws ArgumentOutOfRangeException with 'There must be four and only four input values for Vector4.' A null array throws ArgumentNullException separately.

Solutions

  1. Validate the array length is 4 before constructing
  2. Pad 3-component data explicitly with a fourth value (e.g. alpha = 1f) instead of relying on the constructor
  3. Slice the buffer precisely: new Vector4(buffer.Skip(offset).Take(4).ToArray()) after checking bounds

Example fix

// before
var v = new Vector4(rgbArray); // length 3
// after
var v = rgbArray.Length == 3
    ? new Vector4(rgbArray[0], rgbArray[1], rgbArray[2], 1f)
    : new Vector4(rgbArray);
Defensive patterns

Strategy: validation

Validate before calling

if (values == null || values.Length != 4)
    throw new ArgumentException("Vector4 requires exactly 4 float values", nameof(values));
var v = new Vector4(values);

Type guard

static bool IsVector4Tuple(float[] values) => values is { Length: 4 };

Try / catch

try
{
    var v = new Vector4(values);
}
catch (ArgumentOutOfRangeException)
{
    // pad or reslice to exactly 4 elements before retrying
}

Prevention

When it happens

Trigger: new Vector4(someArray) where someArray has fewer or more than 4 elements — e.g. a 3-element RGB array, an empty array, or a concatenated buffer.

Common situations: Building a Vector4 from color data stored as 3 components (RGB without alpha); parsing flat buffers into vectors with the wrong stride; off-by-one slicing of a float buffer.

Related errors


AI-assisted analysis of stride3d/stride@96fad776d2 (2026-09-14). Data as JSON: /api/errors/7d907148a7c2ffd9. Report an issue: GitHub.

Appendix: source

Thrown at sources/core/Stride.Core.Mathematics/Vector4.cs:167

    public Vector4(Vector2 value, float z, float w)
    {
        X = value.X;
        Y = value.Y;
        Z = z;
        W = w;
    }

    /// <summary>
    /// Initializes a new instance of the <see cref="Vector4"/> struct.
    /// </summary>
    /// <param name="values">The values to assign to the X, Y, Z, and W components of the vector. This must be an array with four elements.</param>
    /// <exception cref="ArgumentNullException">Thrown when <paramref name="values"/> is <c>null</c>.</exception>
    /// <exception cref="ArgumentOutOfRangeException">Thrown when <paramref name="values"/> contains more or less than four elements.</exception>
    public Vector4(float[] values)
    {
        ArgumentNullException.ThrowIfNull(values);
        if (values.Length != 4)
            throw new ArgumentOutOfRangeException(nameof(values), "There must be four and only four input values for Vector4.");

        X = values[0];
        Y = values[1];
        Z = values[2];
        W = values[3];
    }

    /// <summary>
    /// Gets a value indicting whether this instance is normalized.
    /// </summary>
    public readonly bool IsNormalized
    {
        get { return MathF.Abs((X * X) + (Y * Y) + (Z * Z) + (W * W) - 1f) < MathUtil.ZeroTolerance; }
    }

    /// <summary>
    /// Gets or sets the component at the specified index.
    /// </summary>

View on GitHub (pinned to 96fad776d2)