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

I am using the setInstance(ByVal instance As UInteger) library setInstance(ByVal instance As UInteger) in my VB.NET . The parameter I need to pass is Integer . Does anything need to be done to convert an integer parameter to an unsigned integer? The number guaranteed will be positive and less than 10.

+6
parameters unsigned
source share
3 answers

Same...

 Dim MyInt As Int32 = 10 Dim MyUInt As UInt32 = CUInt(MyInt) setInstance(MyUInt) 
+7
source share

CUInt or CType (x, UInt) allows you to convert 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 configured the compiler to disable Check Integer Overflow (default for C #). Then you can use CUInt with negative values ​​without checking - no exception.

+3
source share

You can call CUint to convert the variable to UInteger .

+2
source share

All Articles