[AccessD] [EXTERNAL] Re: Parameter (argument) order

Stuart McLachlan stuart at lexacorp.com.pg
Fri Feb 17 15:17:48 CST 2023


That articleis incorrect for VBA when it says:
"The default in Visual Basic is to pass arguments by value. "

Consider the following previous example where the array is clearly passed BYREF 
since the function changes a value in it.

Function Fillarr(arr() As String, idx As Long, sVal As String) As Long
   arr(idx) = sVal
End Function

Function paramtest() As Long
Dim a(10) As String
Fillarr a(), 3, "Hello"
Debug.Print a(3)  ' Array must have been passed BYREF since its content has changed
End Function

Or another
FUNCTION Adder() AS LONG
DIM x as LONG
x = 5
Incr x
DEBUG.PRINT X
END SUB

SUB Incr(n AS LONG)
n= n+1
END SUB

IOW, parameters are passed BYREF by default, you only need to stipuate passing method 
if you want to pass BYVAL.


On 17 Feb 2023 at 6:48, Rocky Smolin wrote:

> Can't you use byref in the function for the parameters?
> 
> Passing Arguments by Value and by Reference - Visual Basic | Microsoft
> Learn
> <https://learn.microsoft.com/en-us/dotnet/visual-basic/programming-gui
> de/language-features/procedures/passing-arguments-by-value-and-by-refe
> rence>
> 
> r
> 
> On Thu, Feb 16, 2023 at 10:24 PM Arthur Fuller
> <fuller.artful at gmail.com> wrote:
> 
> > Thanks for trying, Stuart. You know that I love your mind and
> > intellect. But apparently I am still failing to pose my question
> > properly. Let me try once more. I want to pass two arrays to a
> > function, and then do whatever I want with them. One simple
> > possibility is to compare them. Another is to concatenate them and
> > return the result. A third is to "subtract" them and return the
> > difference as a tiny array. Another is to perform a
> > matrix-multiplication on them, and return that result.
> >
> > the template is simple:
> > Function arrManipulate(a1,a2) as <whatever>    'not sure how to
> > declare the args; as Variant? Do Somestuff with a1 and a2       '
> > they are both arrays Return SomeResult End Function
> >
> > Maybe I'm missing something in VBA, but so far I can't seem to get
> > from here to there. It's easy in C and C++, but I have not yet
> > arrived at the equivalent method in VBA.
> >
> > On Fri, Feb 17, 2023 at 1:08 AM Stuart McLachlan
> > <stuart at lexacorp.com.pg> wrote:
> >
> > > On 17 Feb 2023 at 5:07, Shane Groff via AccessD wrote:
> > >
> > > > https://learn.microsoft.com/en-us/office/vba/language/concepts/g
> > > > etting -started/understanding-parameter-arrays
> > > >
> > > > A ParamArray is not for passing an array as an argument, it is
> > > > for allowing you to pass a dynamic number of arguments as an
> > > > array:
> > > >
> > > > If you declare:
> > > > Function CalcSum(ParamArray args() as Variant) As Double
> > > >
> > > > You can call it like so:
> > > >
> > > > x = CalcSum(34, 47, 99, 12)
> > > > or
> > > > x = CalcSum(1, 1)
> > > >
> > > > and it can add all the arguments together (it receives all the
> > > > arguments as a single array).
> > > >
> > > > It must be the last argument in the declaration, since all the
> > > > arguments at the end of the caller will be 'consumed' to create
> > > > the array, so there can't be any additional arguments.
> > > >
> > >
> > > Another way to pass a series of arguments as an array :)
> > > Function PassArray() As Long
> > > Dim v As Variant
> > > v = Array("Some text", 2, 3)
> > > UseArray v
> > > End Function
> > >
> > > Function UseArray(v As Variant) As Long
> > > Debug.Print Mid$(v(0), v(1), v(2))
> > > End Function
> > > --
> > > AccessD mailing list
> > > AccessD at databaseadvisors.com
> > > https://databaseadvisors.com/mailman/listinfo/accessd
> > > Website: http://www.databaseadvisors.com
> > >
> >
> >
> > --
> > Arthur
> > --
> > AccessD mailing list
> > AccessD at databaseadvisors.com
> > https://databaseadvisors.com/mailman/listinfo/accessd
> > Website: http://www.databaseadvisors.com
> >
> -- 
> AccessD mailing list
> AccessD at databaseadvisors.com
> https://databaseadvisors.com/mailman/listinfo/accessd
> Website: http://www.databaseadvisors.com
> 




More information about the AccessD mailing list