stride3d/stride · error · ArgumentException

obj is not the same type as this instance.

Error message

obj is not the same type as this instance.

What it means

NodeIndex implements IComparable, and the explicit IComparable.CompareTo(object) overload requires the boxed argument to be a NodeIndex. Because the non-generic IComparable interface accepts any object, the library validates the runtime type and throws ArgumentException when the caller passes a different type. The strongly-typed CompareTo(NodeIndex) overload never throws this.

Solutions

  1. Pass an actual NodeIndex instance (obtain it from the node/graph API) rather than a raw int, Guid, or other index representation
  2. Check the runtime type with 'obj is NodeIndex' before invoking CompareTo via an IComparable reference
  3. Prefer the strongly-typed CompareTo(NodeIndex) overload or the ==/!= operators, which are statically typed and cannot hit this throw
  4. If comparing distinct index representations, convert the foreign value to a NodeIndex first (the library constructs NodeIndex values internally, e.g. via NodeIndex.Empty or node APIs)

Example fix

// before
IComparable cmp = nodeIndex;
int r = cmp.CompareTo(42); // ArgumentException
// after
if (other is NodeIndex ni)
    int r = cmp.CompareTo(ni);
else
    int r = nodeIndex.CompareTo(NodeIndex.Empty); // or handle type explicitly
Defensive patterns

Strategy: type-guard

Validate before calling

if (obj is not NodeIndex) throw new InvalidOperationException("CompareTo requires a NodeIndex");

Type guard

static bool IsNodeIndex(object? obj) => obj is NodeIndex;

Try / catch

try { result = comparable.CompareTo(candidate); }
catch (ArgumentException ex) when (ex.ParamName == "obj")
{ /* handle non-NodeIndex argument: skip or convert */ }

Prevention

When it happens

Trigger: Calling the explicit IComparable.CompareTo(object) implementation (e.g. via a non-generic IComparable reference, sorting APIs, or reflection) with an argument whose runtime type is not NodeIndex, such as null, an int, or another index-like struct.

Common situations: Passing a NodeIndex into a legacy non-generic sort/collection API that boxes values as object; comparing NodeIndex to an integer index or a Guid by mistake; using ArrayList or IComparer non-generic pipelines where the item type was assumed.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


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

Appendix: source

Thrown at sources/presentation/Stride.Core.Quantum/NodeIndex.cs:101

    /// <inheritdoc/>
    public override readonly bool Equals(object? obj)
    {
        if (ReferenceEquals(null, obj)) return false;
        return obj is NodeIndex nodeIndex && Equals(nodeIndex);
    }

    /// <inheritdoc/>
    public override readonly int GetHashCode()
    {
        return Value?.GetHashCode() ?? 0;
    }

    /// <inheritdoc/>
    readonly int IComparable.CompareTo(object? obj)
    {
        if (obj is not NodeIndex nodeIndex)
            throw new ArgumentException("obj is not the same type as this instance.", nameof(obj));

        return CompareTo(nodeIndex);
    }

    public static bool operator ==(NodeIndex a, NodeIndex b)
    {
        return Equals(a.Value, b.Value);
    }

    /// <inheritdoc/>
    public static bool operator !=(NodeIndex a, NodeIndex b)
    {
        return !Equals(a.Value, b.Value);
    }
}

View on GitHub (pinned to 96fad776d2)