I need some input.
Say you have an int-based Enum with values 1 through 10. If you then have a variable that is, say, value corresponding to 7, how can you easiest set it to next value in the given range without going out of bounds? If the value reaches the limit, it should reset itself to first in the range.
I want a one-liner solution to this. I don't want to do ++ and then check and reset value, plus it has to work in both C# and JavaScript. I suppose something in the Math object might be of help, I don't know...
thanks
Since integers, individualistically, are not iterable, when we try to do a for x in 7 , it raises an exception stating TypeError: 'int' object is not iterable .
The first method to iterate through digits of a number is the use of iter() function. It accepts the string value as the argument. Therefore you have to first typecast the integer value and then pass it into it.
To loop through a set of code a specified number of times, we can use the range() function, The range() function returns a sequence of numbers, starting from 0 by default, and increments by 1 (by default), and ends at a specified number.
Increment, subtract 1, then modulo, then add 1 (since your Enum is 1-based).
((++i - 1) % N + 1
(N=10, the maximum value your Enum can take on.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With