{"record":{"id":"40fb11cbd25e1529","repo":"stride3d/stride","slug":"the-given-value-must-implement-ienumerable","errorCode":null,"errorMessage":"The given value must implement IEnumerable","messagePattern":"The given value must implement IEnumerable","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"sources/presentation/Stride.Core.Presentation.Wpf/ValueConverters/CountEnumerable.cs","lineNumber":25,"sourceCode":"using Stride.Core.Annotations;\n\nnamespace Stride.Core.Presentation.ValueConverters\n{\n    /// <summary>\n    /// This converter will take an enumerable as input and return the number of items it contains.\n    /// </summary>\n    public class CountEnumerable : OneWayValueConverter<CountEnumerable>\n    {\n        /// <inheritdoc/>\n        [NotNull]\n        public override object Convert(object value, Type targetType, object parameter, CultureInfo culture)\n        {\n            if (value == null)\n                return 0;\n\n            var enumerable = value as IEnumerable;\n            if (enumerable == null)\n                throw new ArgumentException(@\"The given value must implement IEnumerable\", nameof(value));\n\n            return (value as ICollection)?.Count ?? enumerable.Cast<object>().Count();\n        }\n    }\n}\n","sourceCodeStart":7,"sourceCodeEnd":31,"githubUrl":"https://github.com/stride3d/stride/blob/96fad776d210c221682aac1ccdf4c79dc046fc38/sources/presentation/Stride.Core.Presentation.Wpf/ValueConverters/CountEnumerable.cs#L7-L31","documentation":"CountEnumerable.Convert converts a binding value to the number of items it contains. If the value is neither null nor an IEnumerable, the converter cannot count it and throws ArgumentException. This guards against binding to non-collection values (e.g. a scalar or string-bound property of incompatible type).","triggerScenarios":"Calling Convert(value, ...) with a non-null value that does not implement IEnumerable, e.g. Convert(42, typeof(int), null, CultureInfo.InvariantCulture) or binding a scalar property to a control using this converter.","commonSituations":"XAML data-binding mistakes where a scalar property is bound to ItemsControl-like consumers expecting a collection; refactors that changed a property from List<T> to a plain value type; unit tests passing arbitrary objects directly to the converter.","solutions":["Make the bound property implement IEnumerable (e.g. expose an ObservableCollection<T> or List<T>).","If a null/empty value is expected, return null instead of a non-enumerable so the converter returns 0.","Wrap the binding in a fallback or use a different converter if counting non-collections makes no sense.","Catch ArgumentException in code that calls the converter directly with unknown values."],"exampleFix":"// before\n_myViewModel.Count = 5; // bound with CountEnumerable converter\n// after\n_myViewModel.Items = new ObservableCollection<int>(Enumerable.Range(0, 5));","handlingStrategy":"validation","validationCode":"bool isCountable(object? v) => v is null || v is System.Collections.IEnumerable;","typeGuard":"static bool IsEnumerable(object? v, out System.Collections.IEnumerable? e) { e = v as System.Collections.IEnumerable; return e != null || v == null; }","tryCatchPattern":"try { count = converter.Convert(value, typeof(int), null, CultureInfo.InvariantCulture); } catch (ArgumentException ex) { /* log; fall back to 0 */ }","preventionTips":["Bind only collection-typed properties to this converter","Return null (not scalars) for 'no data' states","Add unit tests that pass representative binding values to Convert"],"tags":["wpf","value-converter","argument-exception","data-binding"],"backgroundTag":"invalid-argument-value","analyzedSha":"96fad776d210c221682aac1ccdf4c79dc046fc38","analyzedAt":"2026-09-14T02:59:31.279Z","contentChangedAt":"2026-09-14T02:59:31.279Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}