开发者

How to pass integer as unsigned parameter in VB.NET?

I'm using a library call, setInstance(ByVal instance As UInteger), in my VB.NET code. The parameter I need to pass is an Integer. Is t开发者_如何学Chere anything I need to do to convert the integer parameter to an unsigned integer? The number is guaranteed to be positive and less than 10.


Like so...

Dim MyInt As Int32 = 10
Dim MyUInt As UInt32 = CUInt(MyInt)
setInstance(MyUInt)


CUInt or CType(x, UInt) allow converting a positive integer.

It throws an exception when x is negative.

To use Int as Uint, you can use some tricks:

  dim bb() = System.BitConverter.GetBytes(myInt)
  dim MyUint = System.BitConverter.ToUInt32(bb, 0)

Also with System.Buffer.BlockCopy for arrays.

If you configure the compiler to disable Check Integer Overflow (default for C#). Then you can use CUInt with negative values with no check - not exception.


You can call CUint to convert a variable to a UInteger.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜