{"record":{"id":"9259a42be6bc1b24","repo":"stride3d/stride","slug":"cannot-call-setitem-on-a-sortedobservablecollection","errorCode":null,"errorMessage":"Cannot call SetItem on a SortedObservableCollection","messagePattern":"Cannot call SetItem on a SortedObservableCollection","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"sources/presentation/Stride.Core.Presentation/Collections/SortedObservableCollection.cs","lineNumber":171,"sourceCode":"        int i = GetIndex(item, true);\n        base.InsertItem(i, item);\n    }\n\n    /// <inheritdoc/>\n    protected override void MoveItem(int oldIndex, int newIndex)\n    {\n        throw new InvalidOperationException(\"Cannot call MoveItem on a SortedObservableCollection\");\n    }\n\n    protected void ObservableCollectionMoveItem(int oldIndex, int newIndex)\n    {\n        base.MoveItem(oldIndex, newIndex);\n    }\n\n    /// <inheritdoc/>\n    protected override void SetItem(int index, T item)\n    {\n        throw new InvalidOperationException(\"Cannot call SetItem on a SortedObservableCollection\");\n    }\n}\n","sourceCodeStart":153,"sourceCodeEnd":174,"githubUrl":"https://github.com/stride3d/stride/blob/96fad776d210c221682aac1ccdf4c79dc046fc38/sources/presentation/Stride.Core.Presentation/Collections/SortedObservableCollection.cs#L153-L174","documentation":"SortedObservableCollection positions items by its comparer, so replacing an item in place via SetItem could break the sort invariant. SetItem is overridden to always throw InvalidOperationException. Mutate the item or remove/re-add it instead.","triggerScenarios":"Calling the collection indexer setter, e.g. collection[i] = newItem, or any API that internally calls SetItem on a SortedObservableCollection.","commonSituations":"Updating an element's value in-place assuming ObservableCollection semantics; MVVM code assigning selected items by index.","solutions":["Remove the old item and Insert/SortAdd the new one so the comparer places it correctly","Mutate the existing item if it implements INotifyPropertyChanged so re-sorting hooks can react","Use a plain ObservableCollection if index-based replacement is required"],"exampleFix":"// before\nsortedCollection[i] = newItem;\n// after\nsortedCollection.RemoveAt(i);\nsortedCollection.Add(newItem); // inserted in sorted position","handlingStrategy":"validation","validationCode":"if (collection is SortedObservableCollection<T>) { collection.RemoveAt(index); collection.Add(newItem); } else collection[index] = newItem;","typeGuard":"bool SupportsSetItem(object c) => c is not SortedObservableCollection<T>;","tryCatchPattern":"try { collection[index] = newItem; } catch (InvalidOperationException) { collection.RemoveAt(index); collection.Add(newItem); }","preventionTips":["Treat sorted collections as append/sort-only","Use Remove+Add for replacement","Expose comparer-driven updates instead of indexer writes"],"tags":["csharp","collections","unsupported-operation"],"backgroundTag":"unsupported-operation","analyzedSha":"96fad776d210c221682aac1ccdf4c79dc046fc38","analyzedAt":"2026-09-14T02:59:31.279Z","contentChangedAt":"2026-09-14T02:59:31.279Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}