Understanding ampersand 0&, &H, x&

10-22-2006, 08:27 AM
Ok, I believe that the answer to my question is probably very simple, however, I have had a difficult time trying to understand what 0& does in code. I do know that it can be used in VB 6, but VB .NET 2005 doesn't understand what it means.

Also, since I'm on the topic of the ampersand symbol, I am pretty certain that the following usage means the Hex value of 1:
Public Const FILE_SHARE_READ As Long = &H1

So, what I'm really trying to understand is how the following line operates. I feel that by understanding what 0& does, I will finally understand API command operates.

Public Declare Function DeviceIoControl Lib "kernel32" _
(ByVal hDevice As Long, _
ByVal dwIoControlCode As Long, _
lpInBuffer As Any, ByVal _
nInBufferSize As Long, _
lpOutBuffer As Any, _
ByVal nOutBufferSize As Long, _
lpBytesReturned As Long, _
lpOverlapped As Any) As Long

ByVal 0&, _
0&, _
GetDiskGeometry, _
Len(GetDiskGeometry), _
bytesReturned, _
ByVal 0&

Lastly, what, if any, is the difference between the following two lines (at least as far as VB interpreting the values)?


Thank you,

10-22-2006, 08:42 AM
0 is a number.
Unfortunately, there are several number data types - Integer, Long, Single, Double, Currency, etc.

With no data type identifier after the 0, it is implied to be of the native Integer (the default). Therefore this 0 is expressed as an Integer, which makes it a 16-bit 0.

However, many API functions use Longs and you should not pass an Integer 0 into a Long (that's like putting M&Ms in a cookie jar!)
You should pass a Long 0, which will be a 32-bit 0, as the function requests a 32-bit long long number.
So, therefore you add a &:

An & after a number or a variable means that it is a Long (which is 32-bits).
0& is a 32-bit 0.
x& is a 32-bit variable (this is obsolete because it is better to Dim x As Long rather than Dim x& - the x As Long is clearly a long, and the x& is not obvious)

&H is hexadecimal notation - used when functions are trying to show or set specific bit values - this is used when it would not be efficient to express a value in base 10 notation.

For example: 0011 0000 1000 1101 (16-bit integer) these are often used to set bit flags.
You can use a simple lookup table to convert this to hexadecimal
&H308D (if you're like me, you've memorized it - it's only 16 numbers)

It takes much more time to convert this to decimal.

And you should be able to answer the last question on your own now. :)

P.S. ByVal 0& is passed to an API parameter declared as Any to specify that you don't want to put anything there.

10-22-2006, 01:37 PM
Thank you. It's so frustrating to think that something must be so common and basic knowledge, and yet not be able to figure it out. I was leaning towards 0& as an alternate representation of Null or Nothing. I am certain I've run into the description long ago in the past, but I've never used it so it wasn't very important to me. I can understand how it is important in API programming to have the correct number of bits.

I am still curious about ByVal 0&, however. If I understand correctly, ByVal means to pass the value of a variable (or in this case, a Long 0), while ByRef means to pass the reference to the variable. Why do I need to explecitly pass the value ByVal for some values and not for others? So I guess I was partially correct when I determined that ByVal 0& meant Nothing or Null.

Let me give you some background about me. I've programmed VBA in Access for several years, and recently (within the last year or so) started to get my feet wet in REAL VB. I've done a few small things in VB, but my current project is ambitious, at least, for me. It requires APIs, of which I have a little experience, and I have one example of code which may demonstrate to me how to do what I need to do. Unfortunately, I don't feel that the programming in this example is very well.... organized, so I'm spending lots of time trying to clean it up and organize out the BS so I can concentrate on the meat of the program and understand the API calls I need for my program.

I appreciate your help, and I know I will be asking more questions in the near future.

10-22-2006, 04:04 PM
Yes I know what you mean... I despise code that hasn't been commented or explained somehow! Comments make a world of difference, especially as the project grows bigger. But anyway...

ByVal 0& is an exception, sort of a way to work with arguments that are declared As Any.

In languages such as C, C++, etc, they use pointers, which are references to objects in memory. Therefore the actual value of the pointer is an address in memory, which would be a 32-bit Long. However, you rarely use the number in there, as you don't care where it is in memory - all that you care about is that the value that the pointer references is what you want to reference.

Now, if you don't want a pointer to reference anything, you make it refer to the address 0, which makes it a null reference - it doesn't refer to anything.

In VB, when you pass things ByRef, you pass a reference to the object, which means you pass the address of the object in memory (the address is again a 32-bit Long).
On the surface, all you need to know is that it's passing the reference to the object you were using earlier.

To make a reference refer to nothing, you make the address 0 (it's a long, so you have to say 0&). But, since the argument itself is ByRef, if you just pass a 0&, it would try to find the reference for a constant (which won't exist as it's not a variable). To fix this, you have to say ByVal 0&, so you pass the address 0 (making it null), effectively passing nothing to the argument - an appropriate way of ignoring the argument.

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum