To convert from an unsigned int to a signed int, the code would read
something like this:
If unsignedInt > 32767 Then
signedInt = unsignedInt - 65535
Else
signedInt = unsignedInt
End If
I would like to do the above conversion by typecasting so I don't have
to call a function like above every time I read in a value. I would
rather do something like:
signedInt = CShort(unsignedInt)
but I get an overflow error.
Casting in VB usually implies a conversion. I assume from your original
that you want to do something like sx=(SHORT)ux from the good/bad old days
which does no conversion. The CShort() call is failing because it can't
convert (not cast) 65000 into a Short.
If you want economy and legibility of source code, call a function.
If you really want to do an old C style cast in VB, then you probably have
to make the equivalent of a union, eg
<Runtime.InteropServices.StructLayout(Runtime.InteropServices.LayoutKind.Explicit)> Public Structure Union
<Runtime.InteropServices.FieldOffset(0)> Public xShort As Short
<Runtime.InteropServices.FieldOffset(0)> Public xUShort As UShort
<Runtime.InteropServices.FieldOffset(0)> Public xInteger As Integer
<Runtime.InteropServices.FieldOffset(0)> Public xIntPtr As IntPtr
<Runtime.InteropServices.FieldOffset(0)> Public xByte As Byte
etc as with as much as you want.....
End Structure
Then you can have one of these things laying around, eg
Dim z as Union
and cast like this
dim u as UShort
dim s as Short = whatever
z.xShort = s : u = z.xUShort ' no function calls, but two assignments
I personally don't like this kind of thing in VB, and I think the function
call is the better answer. But I think this gets you as close as possible in
VB to the cast you are looking for.