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 there 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.
Asked
Active
Viewed 6,213 times
6

Peter Mortensen
- 30,738
- 21
- 105
- 131

CodeFusionMobile
- 14,812
- 25
- 102
- 140
3 Answers
7
Like so...
Dim MyInt As Int32 = 10
Dim MyUInt As UInt32 = CUInt(MyInt)
setInstance(MyUInt)

Carter Medlin
- 11,857
- 5
- 62
- 68
3
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.

Peter Mortensen
- 30,738
- 21
- 105
- 131

x77
- 737
- 1
- 4
- 12
1
You can call CUint
to convert a variable to a UInteger
.

SLaks
- 868,454
- 176
- 1,908
- 1,964
-
As answer to values above zero ok. But have you tried to CUInt() or DirectCast(value,UInt32) a negative integer to UInt32 (eg. -460819684)? Very easy in C# but completely unintuitive VB.Net. – Bernhard Apr 13 '18 at 06:48