Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 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.

like image 235
CodeFusionMobile Avatar asked Jul 16 '10 16:07

CodeFusionMobile


People also ask

How do I declare a Windows function with unsigned parameters?

In the Declare statement, use UInteger, ULong, UShort, or Byte as appropriate for each parameter with an unsigned type. Consult the documentation for the Windows function you are calling to find the names and values of the constants it uses. Many of these are defined in the WinUser.h file. Declare the necessary constants in your code.

How to pass arguments to a function in Visual Basic?

There are two ways to pass arguments to a function in Visual Basic: ByVal : This refers to a copy of the argument is passed to the function. ByRef : In ByRef procedure the actual argument itself is passed to the funtion. In this article we discuss about the ByVal reference, We take a example and shows how to pass the value by ByVal reference.

How do you declare a variable with an unsigned type?

In the Declare statement, use UInteger, ULong, UShort, or Byte as appropriate for each parameter with an unsigned type. Consult the documentation for the Windows function you are calling to find the names and values of the constants it uses. Many of these are defined in the WinUser.h file.

How do you force a parameter to be passed by value?

If a parameter is declared with ByRef, the calling code can force the mechanism to ByVal by enclosing the argument name in parentheses in the call. For more information, see How to: Force an Argument to Be Passed by Value. The default in Visual Basic is to pass arguments by value.


3 Answers

Like so...

Dim MyInt As Int32 = 10
Dim MyUInt As UInt32 = CUInt(MyInt)
setInstance(MyUInt)
like image 145
Carter Medlin Avatar answered Sep 19 '22 13:09

Carter Medlin


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.

like image 45
x77 Avatar answered Sep 17 '22 13:09

x77


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

like image 23
SLaks Avatar answered Sep 20 '22 13:09

SLaks