PC Review


Reply
Thread Tools Rate Thread

Array Argument Parenthesis

 
 
=?Utf-8?B?TmVhbCBaaW1t?=
Guest
Posts: n/a
 
      16th Apr 2007
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 re: the parens ?
Thanks
--
Neal Z
 
Reply With Quote
 
 
 
 
Bob Phillips
Guest
Posts: n/a
 
      16th Apr 2007
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" <(E-Mail Removed)> wrote in message
news:B7D869FC-2C85-4570-A25B-(E-Mail Removed)...
> 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 re: the parens ?
> Thanks
> --
> Neal Z



 
Reply With Quote
 
=?Utf-8?B?TmVhbCBaaW1t?=
Guest
Posts: n/a
 
      17th Apr 2007
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 re: 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" <(E-Mail Removed)> wrote in message
> news:B7D869FC-2C85-4570-A25B-(E-Mail Removed)...
> > 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 re: the parens ?
> > Thanks
> > --
> > Neal Z

>
>
>

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
using array as argument in VBA sub =?Utf-8?B?RFJB?= Microsoft Excel Programming 5 11th Nov 2006 08:50 PM
Function (array argument, range argument, string argument) vba Witek Microsoft Excel Programming 3 24th Apr 2005 03:12 PM
Need help passing an array as an argument =?Utf-8?B?Ymxj?= Microsoft Excel Programming 3 4th Aug 2004 10:35 PM
Commandline Argument Array Help =?Utf-8?B?amNyb3VzZQ==?= Microsoft VB .NET 5 28th May 2004 08:51 PM
Array Formula - Use of OFFSET function with array argument Alan Microsoft Excel Worksheet Functions 2 11th Feb 2004 09:38 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 04:30 PM.