I've got 3 variables all declared as type 'Int16', yet this code is refusing to work.
private Int16 _cap; // Seat Capacity
private Int16 _used; // Seats Filled
private Int16 _avail; // Seats Available
public Int16 SeatsTotal {
get {
return _cap;
}
set {
_cap = value;
_used = _cap - _avail;
}
}
Except the part where I have _used = _cap - _avail;
is throwing this error, Error
1 Cannot implicitly convert type 'int' to 'short'. An explicit conversion exists (are you missing a cast?)
Yes, that's because there's no subtraction operator for short
(Int16
). So when you write:
_cap - _avail
that's effectively:
(int) _cap - (int) _avail
... with an int
result.
You can, of course, just cast the result:
_used = (short) (_cap - _avail);
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