Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 345
Default Array Argument Parenthesis

An array is created in a sub, call it TestAy

it's passed to a call. What is the technical difference between A and B?

call SubA(TestAy) ' no parens follow
call SubB(TestAy()) ' parens follow

Does it matter if TestAy is ReDim'd in the called sub the parens ?
Thanks
--
Neal Z
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 10,593
Default Array Argument Parenthesis

Doesn't the second one error for you, expecting an index number?

It matters if it is re-dimmed, but that may be what you want. If it is
passed ByRef and you redim it., the caller will see the changes. For
instance

Public Sub Test()
Dim ary
ary = Array(1, 2, 3)
MsgBox ary(UBound(ary))
Call Called(ary)
MsgBox ary(UBound(ary))
End Sub

Sub Called(ByRef ary As Variant)
ReDim Preserve ary(UBound(ary) + 1)
ary(UBound(ary)) = 4
End Sub

but if paseed ByVal, it won't. For instance

Public Sub Test()
Dim ary
ary = Array(1, 2, 3)
MsgBox ary(UBound(ary))
Call Called(ary)
MsgBox ary(UBound(ary))
End Sub

Sub Called(ByVal ary As Variant)
ReDim Preserve ary(UBound(ary) + 1)
ary(UBound(ary)) = 4
End Sub

--
HTH

Bob

(there's no email, no snail mail, but somewhere should be gmail in my addy)

"Neal Zimm" wrote in message
...
An array is created in a sub, call it TestAy

it's passed to a call. What is the technical difference between A and B?

call SubA(TestAy) ' no parens follow
call SubB(TestAy()) ' parens follow

Does it matter if TestAy is ReDim'd in the called sub the parens ?
Thanks
--
Neal Z



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 345
Default Array Argument Parenthesis

Thanks Bob, but I asked a crappy question, not homing in on what I wanted
to know, which is if you're processing the entire array in the called Sub, do
the
parenthesis matter, except perhaps for visual documentation in that you know
that you're passing an array in the callER sub and not a variable( ignoring
var naming conventions) BTW there's a new question something you
mentioned in your answer at the end of this writeup.

I'm relatively new to VBA, so, is there some deeper technical issue not in
the MSo Help for: call Two(Ary) versus Call Two(Ary()) when the entire
array is being processed? I'm nearing the end of a large effort where in the
beginning I used
call Two(Ary) with no apparent problems. After seeing Call Two(Ary()) used
in this community and other sources I started using it, again, with no
apparent problems. So, should I go back and re-visit the old code to be
consistant ?

I try whereever practical to develop my code modularly, via calls for
easier testing, debugging, and use by more than 1 macro.

My very typical and VERY abbreviated use is:

Sub One()

Dim Ary() as whatever
' lots more code...... Sub One may or may not ReDim Ary and fill it with
data.

call Sub Two(Ary) ' very very rarely do I use Ary(x) as an argument to pass
'only 1 value to Sub Two
End Sub

That's why I've never errored out in sub two 'expecting' an index.

Sub Two typically processes the entire array and returns all the values to
Sub One
where it's processed further, and perhaps by other calls in Sub One.

I don't remember the specifics, but I think I remember getting an error when
I tried to use a byval array in an argument, so I was 'surprised' by your
question.

Can you pass an array byval as an argument?? I've not tested or tried this
on purpose. My byval use was an unintended typo in the code.
Sorry for being so long winded here.
thanks for your help.






--
Neal Z


"Bob Phillips" wrote:

Doesn't the second one error for you, expecting an index number?

It matters if it is re-dimmed, but that may be what you want. If it is
passed ByRef and you redim it., the caller will see the changes. For
instance

Public Sub Test()
Dim ary
ary = Array(1, 2, 3)
MsgBox ary(UBound(ary))
Call Called(ary)
MsgBox ary(UBound(ary))
End Sub

Sub Called(ByRef ary As Variant)
ReDim Preserve ary(UBound(ary) + 1)
ary(UBound(ary)) = 4
End Sub

but if paseed ByVal, it won't. For instance

Public Sub Test()
Dim ary
ary = Array(1, 2, 3)
MsgBox ary(UBound(ary))
Call Called(ary)
MsgBox ary(UBound(ary))
End Sub

Sub Called(ByVal ary As Variant)
ReDim Preserve ary(UBound(ary) + 1)
ary(UBound(ary)) = 4
End Sub

--
HTH

Bob

(there's no email, no snail mail, but somewhere should be gmail in my addy)

"Neal Zimm" wrote in message
...
An array is created in a sub, call it TestAy

it's passed to a call. What is the technical difference between A and B?

call SubA(TestAy) ' no parens follow
call SubB(TestAy()) ' parens follow

Does it matter if TestAy is ReDim'd in the called sub the parens ?
Thanks
--
Neal Z




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
using array as argument in VBA sub DRA Excel Programming 5 November 11th 06 08:50 PM
VBA function for "Mean" using Array as argument ASokolik Excel Worksheet Functions 21 March 28th 06 10:05 PM
Function (array argument, range argument, string argument) vba Witek[_2_] Excel Programming 3 April 24th 05 03:12 PM
Need help passing an array as an argument blc[_3_] Excel Programming 3 August 4th 04 10:35 PM
Array as Argument in User-Defined Functions Tushar Mehta Excel Programming 0 May 19th 04 04:29 AM


All times are GMT +1. The time now is 03:38 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"