0

I've got some incredibly simple code. Three UInt16 variables. I can do simple assignments but it won't let me do simple arithmetic.

Here's the code

    UInt16 A = 1; // This is OK
    UInt16 B = 2; // This is OK
    UInt16 C = A * B; // Does NOT like this.
    C = A * 2; // Does NOT like this.

It highlights the A * B and reports that the A * B part Cannot implicitly convert type "int" to "ushort". An explicit conversion exists. Are you missing a cast?

Likewise for the A * 2 part;

What am I doing wrong? What do I need to correct it?

General Grievance
  • 4,555
  • 31
  • 31
  • 45
Roger Garrett
  • 349
  • 1
  • 7
  • 13
  • Well, `A * B` is of type `Int32` (`int`). You have to cast: `UInt16 C = (UInt16) (A * B);` and `C = (UInt16)(A * 2);` – Dmitry Bychenko Feb 01 '21 at 21:47
  • 4
    Does this answer your question? [Why is ushort + ushort equal to int?](https://stackoverflow.com/questions/10065287/why-is-ushort-ushort-equal-to-int) – Chayim Friedman Feb 01 '21 at 21:48

0 Answers0