2

I tried to google but didn't find any class, maybe im using the wrong keywords.

Im looking for a class which has a "CurrentValue" and "MaxValue" (int) Property.

Everytime the CurrentValue is set, it should be checked and clamped to the MaxValue. For example if the MaxValue is 10 and CurrentValue is set to 20, it should be set to 10 instead (but not throw a exception!)

Currently I wrote my own class which his behavior, but i was wondering if there is any built in C# class with this kind of "clamping"?

R1PFake
  • 410
  • 2
  • 8
  • Can't you just write the logic in the setter of a property? – Sweeper May 14 '18 at 08:20
  • 1
    You're on the right way by writing your own class/struct – Tim Schmelter May 14 '18 at 08:20
  • 1
    There is no built-in "clamp" function even, not to mention such class. – Evk May 14 '18 at 08:20
  • Thanks for the fast comments. @Sweeper i need the logic for multiple values, that's why i made a small class, because i didn't want to copy/paste the check in every property – R1PFake May 14 '18 at 08:23
  • @R1PFake you don't need to copy past. Write a generic class. In the duplicated link you can find a lot of examples that use a generic type. – CodeNotFound May 14 '18 at 08:26
  • Slightly fancier then just writing your own setters with appropirate logic, is writing any class that Implements INotifyPropertyChanged. A class can registers it's own PropertyChanged events and do work with it. It is a very old Interface, that has seen a massive increase in use with WPF. Including a few additions to C# to allow it to be used easier (CallerMemberName was virtually added for it). – Christopher May 14 '18 at 08:43

0 Answers0