Monday, August 20, 2012

0& in VBA Parlance


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.

No comments:

Post a Comment