converting from c# to vb.net byte

i had tried to convert c# code to vb.net code.

the c# code is :

int m = 300;
byte h = (byte)m;

my vb code is :

dim m as integer = 300
dim h as byte = cbyte(m)

the error is:

Message=Arithmetic operation resulted in an overflow.

the expected value is:

 h value is 44 byte.

1 answer

  • answered 2019-11-13 20:38 SouXin

    Your size cannot be more than 255

    Byte.MinValue (0) through Byte.MaxValue (255) (unsigned); fractional parts are rounded.1

    Starting with Visual Basic 15.8, Visual Basic optimizes the performance of floating-point to byte conversion with the CByte function; see the Remarks section for more information. See the CInt Example section for an example.