{"record":{"id":"b5b55361b131891f","repo":"tui-cs/Terminal.Gui","slug":"value-b5b553","errorCode":null,"errorMessage":"value","messagePattern":"value","errorType":"exception","errorClass":"ArgumentOutOfRangeException","httpStatus":null,"severity":"error","filePath":"Terminal.Gui/Views/LinearRange/LinearSelectorT.cs","lineNumber":117,"sourceCode":"        get => SelectedIndices.Count > 0 ? SelectedIndices [0] : null;\n        set\n        {\n            if (value is null)\n            {\n                if (!AllowEmpty)\n                {\n                    return;\n                }\n\n                _value = default;\n                ApplySelectedIndices ([]);\n\n                return;\n            }\n\n            if (Options is null || value < 0 || value >= Options.Count)\n            {\n                throw new ArgumentOutOfRangeException (nameof (value));\n            }\n\n            // Sync indices first so SelectedIndex can select an option whose Data equals the current\n            // _value (e.g. selecting option 0 in a value-type selector where _value is already\n            // default(int)=0 — Value setter would short-circuit on equality, leaving SelectedIndex null).\n            T? newValue = Options [value.Value].Data;\n            T? current = _value;\n            bool valueChanged = !EqualityComparer<T?>.Default.Equals (current, newValue);\n\n            if (valueChanged && RaiseValueChanging (current, newValue))\n            {\n                return;\n            }\n\n            _value = newValue;\n            ApplySelectedIndices ([value.Value]);\n\n            if (valueChanged)","sourceCodeStart":99,"sourceCodeEnd":135,"githubUrl":"https://github.com/tui-cs/Terminal.Gui/blob/2e47b11478db083499917f2ad27c34d30efb0df1/Terminal.Gui/Views/LinearRange/LinearSelectorT.cs#L99-L135","documentation":"Thrown by LinearSelectorT's selected-index logic (ArgumentOutOfRange, param 'value') when the requested index is null in a context requiring a value, or < 0, or >= Options.Count. The setter first syncs the underlying _value from Options[value].Data, so an out-of-range index cannot be mapped. Note the prior branch handles the 'no options' case by clearing selection rather than throwing.","triggerScenarios":"Setting SelectedIndex to a value computed from a stale Options list (list shrunk after the index was captured); setting it before Options is populated; arithmetic that yields -1 (e.g. .IndexOf returning -1) piped straight in.","commonSituations":"Reactive binding that updates selection before the options collection arrives; filtering Options down and forgetting to re-clamp the selection; index from a search/match that returned -1.","solutions":["Re-clamp against the current Options.Count before assigning: selector.SelectedIndex = Options.Count > 0 ? Math.Clamp(idx, 0, Options.Count - 1) : null;","Populate Options before setting the selected index.","Treat a -1 from IndexOf as 'no selection' (assign null) rather than passing it through."],"exampleFix":"// before\nselector.SelectedIndex = currentOptions.IndexOf(match); // -1 -> throws 166\n\n// after\nint idx = currentOptions.IndexOf(match);\nselector.SelectedIndex = idx >= 0 ? idx : null;","handlingStrategy":"validation","validationCode":"int idx = currentOptions.IndexOf(match);\nselector.SelectedIndex = idx >= 0 && idx < currentOptions.Count ? idx : null;","typeGuard":"static bool IsValidIndex(int? i, int count) => !i.HasValue || (i >= 0 && i < count);","tryCatchPattern":null,"preventionTips":["Treat IndexOf == -1 as 'no selection', not a valid index.","Re-clamp selection whenever Options changes.","Populate Options before setting the selected index."],"tags":["linearselector","index","validation","precondition"],"backgroundTag":null,"analyzedSha":"2e47b11478db083499917f2ad27c34d30efb0df1","analyzedAt":"2026-08-13T19:20:08.826Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}