0

This is driving me nuts... Incoming hex strings from a Dictionary object - they all seem to be ok as 'strings', but I've tried several methods to return the values in those fields... Ended up with this expanded code to find the issue.

for (UInt16 i = 0; i < Convert.ToUInt16(addressList["NUM_ALIAS"],16); i++)
{
    // loop through all blocked values
    UInt16 blockSize = Convert.ToUInt16(addressList["LEN_ALIAS_FROM"], 16);
    UInt16 blockBase = Convert.ToUInt16(addressList["ALIASFROM"], 16);
    UInt16 blockAddr = blockBase + (i * blockSize);

    // use the values
    :::
}

The syntax checker indicates an error -underlining- >>>blockBase + (i * blockSize)<<<

ERROR: Cannot implicitly convert type int to ushort

Thoughts appreciated - and I can learn something! FLAGGED AS DUPLICATE? I can't see it anywhere else with my issue. ((Can mod please link to the duplicate so I/we can see it ?)) Aah - ok the duplicate was shown up the top after I opened it for editing and saved it.

M C
  • 61
  • 7
  • please add addressList values as well in question. – Ahmar Dec 24 '16 at 04:39
  • 2
    `short * short` evaluates to an `int` for the same reason `short + short` evaluates to an `int`, which is explained in [Integer summing blues, short += short problem](http://stackoverflow.com/q/4343624/3744182). You need to do `UInt16 blockAddr = checked((short)(blockBase + (i * blockSize)));`. The [`checked`](https://msdn.microsoft.com/en-us/library/74b4xzyw.aspx) makes sure you don't have an overflow. – dbc Dec 24 '16 at 04:58

0 Answers0