{"record":{"id":"4e749b982863c3ed","repo":"tui-cs/Terminal.Gui","slug":"setting-values-directly-is-not-allowed","errorCode":null,"errorMessage":"Setting Values directly is not allowed.","messagePattern":"Setting Values directly is not allowed\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"Terminal.Gui/Views/Selectors/OptionSelectorTEnum.cs","lineNumber":38,"sourceCode":"        base.Values = Enum.GetValues<TEnum> ().Select (f => Convert.ToInt32 (f)).ToList ().AsReadOnly ();\n\n        // Set labels, which triggers CreateSubViews + UpdateChecked.\n        Labels = Enum.GetNames<TEnum> ();\n    }\n\n    /// <summary>\n    ///     Gets or sets the value of the selected option.\n    /// </summary>\n    public new TEnum? Value\n    {\n        get => base.Value.HasValue ? (TEnum)Enum.ToObject (typeof (TEnum), base.Value.Value) : null;\n        set => base.Value = value.HasValue ? Convert.ToInt32 (value.Value) : null;\n    }\n\n    /// <summary>\n    ///     Prevents calling the base Values property setter with arbitrary values.\n    /// </summary>\n    public override IReadOnlyList<int>? Values { get => base.Values; set => throw new InvalidOperationException (\"Setting Values directly is not allowed.\"); }\n\n    /// <summary>\n    ///     Raised when <see cref=\"Value\"/> has changed. Provides the new value as <typeparamref name=\"TEnum\"/>?.\n    /// </summary>\n    public new event EventHandler<EventArgs<TEnum?>>? ValueChanged;\n\n    /// <summary>\n    ///     Called when <see cref=\"Value\"/> has changed. Raises the generic <see cref=\"ValueChanged\"/> event.\n    /// </summary>\n    protected override void OnValueChanged (int? value, int? previousValue)\n    {\n        base.OnValueChanged (value, previousValue);\n\n        TEnum? newValue = value.HasValue ? (TEnum)Enum.ToObject (typeof (TEnum), value.Value) : null;\n\n        ValueChanged?.Invoke (this, new EventArgs<TEnum?> (newValue));\n    }\n","sourceCodeStart":20,"sourceCodeEnd":56,"githubUrl":"https://github.com/tui-cs/Terminal.Gui/blob/2e47b11478db083499917f2ad27c34d30efb0df1/Terminal.Gui/Views/Selectors/OptionSelectorTEnum.cs#L20-L56","documentation":"Thrown by the OptionSelectorTEnum.Values override setter unconditionally — the enum-typed selector deliberately blocks direct writes to the inherited int-based Values collection. For an enum selector, valid values come from the enum members, so allowing arbitrary int lists would break the TEnum mapping. The getter still delegates to base.","triggerScenarios":"Assigning optionSelectorEnum.Values = someList; in code, or via reflection/serialisation/binder that targets the Values property. Any write attempt fails by design.","commonSituations":"Generic/binding code that sets Values across selector types without special-casing enums; config/deserialisation trying to populate Values directly; refactor that routed selection through the base property.","solutions":["Do not set Values on the enum selector — the values are derived from TEnum automatically when you set Options/Source.","If you need a custom value set, use the non-generic OptionSelector (int-based) instead of OptionSelectorTEnum<TEnum>.","Exclude this property from serialisers/binders (make it read-only in the binding map)."],"exampleFix":"// before\nvar sel = new OptionSelectorTEnum<MyEnum>();\nsel.Values = new[] { 0, 1, 2 }; // throws 171\n\n// after\nvar sel = new OptionSelectorTEnum<MyEnum>();\nsel.Value = MyEnum.Second; // set selection via the typed Value\n// and for custom value sets use the int selector:\nvar intSel = new OptionSelector { Values = [0,1,2] };","handlingStrategy":"type-guard","validationCode":"// Do not assign Values on an enum selector; set Value or use the int-based OptionSelector\nif (selector is OptionSelectorTEnum<_>) { /* use .Value only */ }","typeGuard":"static bool ValuesIsReadOnly(SelectorBase s) => s is OptionSelectorTEnum<_>;","tryCatchPattern":null,"preventionTips":["Never set Values on OptionSelectorTEnum — it is intentionally read-only.","Use the non-generic OptionSelector when you need custom value sets.","Exclude Values from serialisers/binders for enum selectors."],"tags":["optionselector","enum","api-design","precondition"],"backgroundTag":null,"analyzedSha":"2e47b11478db083499917f2ad27c34d30efb0df1","analyzedAt":"2026-08-13T19:20:08.826Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}