I have an external system which gives me an object value (I know that this value always a boxed integer type). I would like to increment it in the usual way: int value += otherIntValue
, but I get an error from the compiler:
Operator '+=' cannot be applied to operands of type
For example:
//source values i cannot to change it
object targetInt = 100;
int incrementedValue = 20;
//usual way - not works
targetInt += incrementedValue;
//ugly workaround
targetInt = ((int) targetInt) + incrementedValue;
Is there a way to increment instances of int and object with targetInt += incrementedValue;
?
Just don't change your code. It's perfectly fine to cast your object
to an integer so it is possible to make the addition with another integer.
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