{"record":{"id":"e74e3ceb3bb68646","repo":"MaterialDesignInXAML/MaterialDesignInXamlToolkit","slug":"value-must-be-positive","errorCode":null,"errorMessage":"Value must be positive","messagePattern":"Value must be positive","errorType":"validation","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"src/MahMaterialDragablzMashUp/MahViewModel.cs","lineNumber":24,"sourceCode":"\npublic class MahViewModel : INotifyPropertyChanged\n{\n    public event PropertyChangedEventHandler? PropertyChanged;\n\n    public ObservableCollection<GridRowData> GridData { get; }\n\n    private int _UpDownValue;\n    public int UpDownValue\n    {\n        get\n        {\n            return _UpDownValue;\n        }\n        set\n        {\n            if (value < 0)\n            {\n                throw new Exception(\"Value must be positive\");\n            }\n            if (_UpDownValue != value)\n            {\n                _UpDownValue = value;\n                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(UpDownValue)));\n            }\n        }\n    }\n\n    public MahViewModel()\n    {\n        GridData = new ObservableCollection<GridRowData> {\n            new GridRowData {\n                IsChecked = false,\n                Text = \"Mars\",\n                EnumValue = EnumValues.ValueA,\n                IntValue = 4879\n            },","sourceCodeStart":6,"sourceCodeEnd":42,"githubUrl":"https://github.com/MaterialDesignInXAML/MaterialDesignInXamlToolkit/blob/98edec3a0b96ef272c587b14bcd67ef5f928a7ed/src/MahMaterialDragablzMashUp/MahViewModel.cs#L6-L42","documentation":"A demo ViewModel (`MahViewModel`, part of the MahApps/MaterialDesign mash-up sample) throws a base `System.Exception` from the `UpDownValue` setter whenever the incoming value is negative. It is sample code meant to illustrate validation in an integer up/down control bound through change notification; the value is expected to stay non-negative.","triggerScenarios":"Assigning a negative integer to `MahViewModel.UpDownValue` (e.g. from a NumericUpDown control whose minimum was not clamped, or programmatically setting the property below zero).","commonSituations":"Removing/loosening the Minimum on the bound NumericUpDown control, driving the property from a slider or test code that goes below 0, or copying this sample pattern into production where user input can be negative.","solutions":["Clamp the bound control's Minimum to 0 so the setter never receives a negative value.","If negatives are now legitimate, relax the guard (remove the `if (value < 0)` block) or raise it to a domain-correct bound.","Replace the base `throw new Exception(...)` with `throw new ArgumentOutOfRangeException(nameof(UpDownValue), value, \"Value must be positive\")` so callers can catch a specific type.","For input validation prefer WPF `IDataErrorInfo`/`INotifyDataErrorInfo` over throwing from a setter, which breaks two-way binding."],"exampleFix":"// before\nif (value < 0)\n{\n    throw new Exception(\"Value must be positive\");\n}\n\n// after\nvalue = Math.Max(0, value);\nif (_UpDownValue != value)\n{\n    _UpDownValue = value;\n    PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(UpDownValue)));\n}","handlingStrategy":"validation","validationCode":"// Before assigning, ensure non-negative\nint candidate = GetValueFromControl();\nif (candidate < 0) candidate = 0;\nvm.UpDownValue = candidate;","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Bind the control's Minimum to 0 so negative input is impossible.","Prefer INotifyDataErrorInfo over throwing from setters in MVVM.","Use ArgumentOutOfRangeException instead of the base Exception when invalid input must throw."],"tags":["wpf","mvvm","validation","setter","sample-code"],"backgroundTag":null,"analyzedSha":"98edec3a0b96ef272c587b14bcd67ef5f928a7ed","analyzedAt":"2026-08-13T14:31:19.111Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}