{"record":{"id":"7abc81b48855450f","repo":"stride3d/stride","slug":"argumentoutofrangeexception-visualscriptviewmodel","errorCode":null,"errorMessage":"ArgumentOutOfRangeException","messagePattern":"ArgumentOutOfRangeException","errorType":"exception","errorClass":"ArgumentOutOfRangeException","httpStatus":null,"severity":"error","filePath":"sources/editor/Stride.Assets.Presentation/ViewModel/VisualScriptViewModel.cs","lineNumber":99,"sourceCode":"        {\n            switch (e.ChangeType)\n            {\n                case ContentChangeType.None:\n                case ContentChangeType.CollectionAdd:\n                    {\n                        var function = (Method)e.NewValue;\n                        Methods.Insert(e.Index.Int, new VisualScriptMethodViewModel(this, function));\n                        break;\n                    }\n                case ContentChangeType.CollectionRemove:\n                    {\n                        var viewModel = Methods[e.Index.Int];\n                        viewModel?.Destroy();\n                        Methods.RemoveAt(e.Index.Int);\n                        break;\n                    }\n                default:\n                    throw new ArgumentOutOfRangeException();\n            }\n        }\n    }\n}\n","sourceCodeStart":81,"sourceCodeEnd":104,"githubUrl":"https://github.com/stride3d/stride/blob/96fad776d210c221682aac1ccdf4c79dc046fc38/sources/editor/Stride.Assets.Presentation/ViewModel/VisualScriptViewModel.cs#L81-L104","documentation":"VisualScriptViewModel.MethodsContentChanged reacts to collection-change events on the Methods collection and switches on the change action. Any action it does not explicitly handle (e.g. Reset, Move, Replace, or an Add case missing from this handler) falls through to `throw new ArgumentOutOfRangeException()`. The message is empty because the exception is used purely as an unhandled-case guard.","triggerScenarios":"Raising a NotifyCollectionChangedAction on the Methods collection other than the handled actions (e.g. Reset from Clear(), Move, Replace, or Add/Remove with an out-of-range e.Index such as Reset, whose Index is -1 while the Remove branch does Methods[e.Index.Int]).","commonSituations":"Calling Methods.Clear() (produces Reset) from custom editor code; bulk-loading methods with a Replace/Reset notification; index mismatches after the collection changed elsewhere between the event and the handler; data-binding operations firing Move.","solutions":["Avoid operations that emit Reset on Methods while this handler is subscribed; clear methods individually instead of Clear().","Extend the switch in MethodsContentChanged to handle Reset/Move/Replace actions explicitly.","Validate e.Index before indexing (skip or rebuild when Index is None/-1, as with Reset).","Check for competing writers to Methods (multiple subscriptions or background threads) and serialize the mutations."],"exampleFix":"// before\nthrow new ArgumentOutOfRangeException();\n// after\ncase NotifyCollectionChangedAction.Reset:\n    RebuildMethodViewModels();\n    break;\ndefault:\n    Log.Warning(\"Unhandled collection action: {Action}\", e.Action);\n    break;","handlingStrategy":"try-catch","validationCode":"if (e.Action == NotifyCollectionChangedAction.Reset) { RebuildMethodViewModels(); return; }\nif (e.NewStartingIndex < 0 || e.NewStartingIndex >= Methods.Count) return;","typeGuard":null,"tryCatchPattern":"try\n{\n    var vm = Methods[e.Index.Int];\n    vm?.Destroy();\n    Methods.RemoveAt(e.Index.Int);\n}\ncatch (ArgumentOutOfRangeException)\n{\n    RebuildMethodViewModels(); // resync view models with the collection\n}","preventionTips":["Handle Reset/Move/Replace explicitly instead of relying on a default throw.","Avoid Clear() on observed collections; remove items individually.","Guard all e.Index accesses against NotifyCollectionChangedAction.Reset (Index is -1)."],"tags":["collection-changed","switch-default","wpf","visual-scripting","argument-out-of-range"],"backgroundTag":"argument-out-of-range","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"}