stride3d/stride · error · ArgumentException

Invalid half format.

Error message

Invalid half format.

What it means

HalfConverter.ConvertFrom parses a single Half value from a string; it requires the split on the culture list separator to yield exactly 1 item (i.e. no list separator present). Extra separators or grouped input throw ArgumentException('Invalid half format.').

Solutions

  1. Pass a single numeric value with no list separator: '1.5'
  2. Strip stray separators/whitespace before conversion
  3. Validate the string is a plain float (float.TryParse with the target culture) before calling
  4. Catch ArgumentException and coerce multi-value input by taking/averaging components deliberately

Example fix

// before
converter.ConvertFromString(null, culture, "1.0, 2.0")
// after
converter.ConvertFromString(null, culture, "1.0")
Defensive patterns

Strategy: validation

Validate before calling

if (!float.TryParse(s.Trim(), NumberStyles.Float, culture, out _))
    throw new FormatException("Half expects a single numeric value");

Try / catch

try { h = (Half)converter.ConvertFrom(s); }
catch (ArgumentException) { /* coerce or reject multi-value input */ }

Prevention

When it happens

Trigger: ConvertFrom/ConvertFromString called with a multi-component string like '1.0, 2.0' where a scalar Half is expected; or strings containing the culture's list separator anywhere, including trailing '1.0,'.

Common situations: Pasting a vector into a scalar Half field; regional list separators appearing inside the value; copy-paste whitespace/separator mistakes in editors.

Understand the failure class

Background: "Invalid ... format", "must be in format X", "does not look like a ..." — invalid argument format errors across CLI tools and libraries — this error's family across 17 libraries.

Related errors


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

Appendix: source

Thrown at sources/core/Stride.Core.Design/TypeConverters/HalfConverter.cs:108

    /// Converts the given object to the type of this converter, using the specified context and culture information.
    /// </summary>
    /// <param name="context">A <see cref="T:System.ComponentModel.ITypeDescriptorContext" /> that provides a format context.</param>
    /// <param name="culture">A <see cref="T:System.Globalization.CultureInfo" />. If <c>null</c> is passed, the current culture is assumed.</param>
    /// <param name="value">The <see cref="T:System.Object" /> to convert.</param>
    /// <returns>An <see cref="T:System.Object" /> that represents the converted value.</returns>
    public override object? ConvertFrom(ITypeDescriptorContext? context, CultureInfo? culture, object value)
    {
        culture ??= CultureInfo.CurrentCulture;
        if (value is not string str)
        {
            return base.ConvertFrom(context, culture, value);
        }
        str = str.Trim();
        char[] separator = [culture.TextInfo.ListSeparator[0]];
        string[] stringArray = str.Split(separator);
        if (stringArray.Length != 1)
        {
            throw new ArgumentException("Invalid half format.");
        }
        float h = (float)TypeDescriptor.GetConverter(typeof(float)).ConvertFromString(context, culture, stringArray[0])!;
        return new Half(h);
    }

    /// <summary>
    /// Converts the given value object to the specified type, using the specified context and culture information.
    /// </summary>
    /// <param name="context">A <see cref="T:System.ComponentModel.ITypeDescriptorContext" /> that provides a format context.</param>
    /// <param name="culture">A <see cref="T:System.Globalization.CultureInfo" />. If <c>null</c> is passed, the current culture is assumed.</param>
    /// <param name="value">The <see cref="T:System.Object" /> to convert.</param>
    /// <param name="destinationType">A <see cref="T:System.Type" /> that represents the type you want to convert to.</param>
    /// <returns>An <see cref="T:System.Object" /> that represents the converted value.</returns>
    public override object? ConvertTo(ITypeDescriptorContext? context, CultureInfo? culture, object? value, Type destinationType)
    {
#if NET6_0_OR_GREATER
        ArgumentNullException.ThrowIfNull(destinationType);
#else

View on GitHub (pinned to 96fad776d2)