Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3
Default Optional arguments

I found some code by Chip Pearson which is mostly self-explanatory
except this portion:

"Optional ByVal LB As Long = -1&, _"

What does the "&" after the -1 mean?
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 829
Default Optional arguments

"KD" wrote:
"Optional ByVal LB As Long = -1&, _"
What does the "&" after the -1 mean?


It is completely unnecessary in this context.

For every data type, there is a character that implicitly assigns a type to
a variable name or number when used as a prefix.

-1& says -1 should be treated as type Long. It is unnecessary here because
you are simply assigning -1 to a type Long variable.

It is also unnecessary if you specify a number larger than 32767, e.g.
33000. VBA knows to treat that as Long since it is larger than type
Integer.

The "&" suffix becomes important when the data type of an expression is
ambiguous. For example:

Dim n As Long
n = 30000 + 1000

results in an overflow error since 30000 and 1000 are both interpreted as
type Integer. Consequently, VBA tries to perform type Integer arithmetic
(addition). The following fixes the problem:

n = 30000& + 1000

In contrast, the following works without the use of "&":

n = 33000 + 1000

because 33000 is type Long; ergo, 1000 is "converted" to type Long, and type
Long arithmetic is performed.

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 84
Default Optional arguments

As joeu2004 has explained the & defines the value -1 as a Long.

Also as explained in this context it's not necessary, however predefining as
a Long reserves matching 32bit/4-byte spaces in memory which means
ultimately no coercion is required in assigning the value to the variable.

In theory it makes the code more efficient though in practice unlikely to be
noticeable in modern machines. Even if only by convention it's normal to
fully define constants, eg

Const cNum as Long = 123&

Chip Pearson is a stickler for detail :-)

Regards,
Peter T


"KD" wrote in message
I found some code by Chip Pearson which is mostly self-explanatory
except this portion:

"Optional ByVal LB As Long = -1&, _"

What does the "&" after the -1 mean?


  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 538
Default Optional arguments

joeu2004 wrote:

The "&" suffix becomes important when the data type of an expression is
ambiguous. For example:

Dim n As Long
n = 30000 + 1000

results in an overflow error since 30000 and 1000 are both interpreted
as type Integer. Consequently, VBA tries to perform type Integer
arithmetic (addition).


Minor detail: 30000 + 1000 won't (normally) result in overflow (unless you're
using a data type that can't handle the result, i.e. Byte, and then only when
assigning the result to the variable, not during the actual addition). Max
value of an Integer is 32767, and 32767 (30000 + 1000). The overflow *does*
occur with 32000 + 1000 (or any equation that adds up to more than &h7FFF).

--
He'd like you to think he's got a heart of stone,
but really, he's got a heart of marshmallow.
  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 829
Default Optional arguments

"Auric__" wrote:
joeu2004 wrote:
Dim n As Long
n = 30000 + 1000

[....]
30000 + 1000 won't (normally) result in overflow

[....]
The overflow *does* occur with 32000 + 1000


Yes, and that was my intent. A typo. Thanks for catching it.


  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3
Default Optional arguments

Thks guys!
  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3
Default Optional arguments

Thanks guys!
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Optional Arguments in Custom Fuctions eggman Excel Programming 5 September 27th 07 07:22 PM
Optional arguments and IsMissing pinkfloydfan Excel Programming 2 March 18th 07 11:00 PM
optional args in UDF masterphilch Excel Programming 4 March 3rd 06 12:23 PM
Optional Linking Vin81 Excel Discussion (Misc queries) 0 February 21st 06 12:56 AM
Representation of optional parameters in Function arguments window compound[_5_] Excel Programming 0 January 17th 06 11:32 PM


All times are GMT +1. The time now is 05:35 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"