How to pass Double() array in as ParamArray?

L

Levan Jgarkava

Hi!

I want to be able to pass variable number of Double() arrays to function.
How can I do it in VB/VBA?

Following example shows what I want to do:
------
Public Sub SomeFunction(p_array() As Double, ParamArray params()) As Boolean
' p_array is an array and I can use it as:
Dim b As Double
b = p_array(6)

Dim other
For Each other In params
' Can I use other as array? for example:
Dim a As Double
a = other(5) ' How I can perform something like this but
workable?
Next
End Sub
------

So, what will it be like to work properly?

I'll be greateful for any help.

Best Regards,
Levan Jgharkava
 
R

Rick Rothstein

I want to be able to pass variable number of Double() arrays to
function.
How can I do it in VB/VBA?

Following example shows what I want to do:
------
Public Sub SomeFunction(p_array() As Double, ParamArray params()) As Boolean
' p_array is an array and I can use it as:
Dim b As Double
b = p_array(6)

Dim other
For Each other In params
' Can I use other as array? for example:
Dim a As Double
a = other(5) ' How I can perform something like this but
workable?
Next
End Sub

I'm a little confused at what you want to do. Are you trying to pass in
several different **arrays** into the ParamArray or are you just trying
to treat the values passed into the function after the p_arrays as if
they were an array? My confusion comes from this part of your code...
For Each other In params
' Can I use other as array? for example:
Dim a As Double
a = other(5)
Next

First off, we are not talking about VB.NET, right? Move the Dim
statement out of the loop (put it at the top of the procedure); VB.NET
treats declarations inside of loops differently, VB6 and earlier
doesn't. Now, if you passed in a list of Double values (not arrays) into
params, then you can address params as the array directly

a = params(5)

you wouldn't need the abstraction of going through an intermediate
variable ('a' in you example). If this is not what you mean, please try
to clarify your question.

Rick - MVP
 
T

Tony Proctor

Is this what you had in mind Levan:


Public Sub SomeSub(ParamArray params())
Dim iArray As Integer, iElem As Integer

For iArray = 0 To UBound(params)
Debug.Print "Array " & CStr(iArray) & ":"
For iElem = 0 To UBound(params(iArray))
Debug.Print vbTab & "(" & CStr(iElem) & ") = " &
CStr(params(iArray)(iElem))
Next iElem
Next iArray
End Sub

Private Sub Form_Load()
Dim fArr2(0 To 2) As Double
Dim fArr3(0 To 3) As Double
Dim fArr4(0 To 4) As Double

fArr2(0) = 20: fArr2(1) = 21: fArr2(2) = 22
fArr3(0) = 30: fArr3(1) = 31: fArr3(2) = 32: fArr3(3) = 33
fArr4(0) = 40: fArr4(1) = 41: fArr4(2) = 42: fArr4(3) = 43: fArr4(4) =
44

SomeSub fArr2, fArr3, fArr4
End Sub


Give it a try in a Form.

Tony Proctor
 
L

Levan Jgarkava

Hi!
Is this what you had in mind Levan:
.......
Give it a try in a Form.
Yes. I mean this but here's one problem: I tried to pass param(x) arrays to
another function with parameter arr() as Double and when I was trying to
build compilator warned that I can't pass array of variant as array of
double(something like this). I suppose, that param(x) is treated as array of
variants.
How can I change type of param from array of "Variant"s to array of
"Double"s?
Thanks.
 
L

Levan Jgarkava

Hi!
I'm a little confused at what you want to do. Are you trying to pass in
several different **arrays** into the ParamArray or are you just trying
to treat the values passed into the function after the p_arrays as if
they were an array?
Yes, I'm trying to pass several different arrays :)
My confusion comes from this part of your code...
First off, we are not talking about VB.NET, right? Move the Dim
statement out of the loop (put it at the top of the procedure); VB.NET
treats declarations inside of loops differently, VB6 and earlier
doesn't.
I know, but at this moment it's not critical. We are talking not about it.
I'm a C++ programmer in common and it was instinctive :)
Now, if you passed in a list of Double values (not arrays) into
params, then you can address params as the array directly

a = params(5)

you wouldn't need the abstraction of going through an intermediate
variable ('a' in you example). If this is not what you mean, please try
to clarify your question.
I mean, I want to pass several different arrays to my function and also to
be able to pass these arrays down to another function as a "Double" arrays.
I did it with ParamArray p_array(), but it was treated as arrays of Variants
and compilator has objection about it :)

Best Regards,
Levan Jgarkava
 
L

Larry Serflaten

Levan Jgarkava said:
Yes. I mean this but here's one problem: I tried to pass param(x) arrays to
another function with parameter arr() as Double and when I was trying to
build compilator warned that I can't pass array of variant as array of
double(something like this). I suppose, that param(x) is treated as array of
variants.
How can I change type of param from array of "Variant"s to array of
"Double"s?

You can't change them in place, you'd need to create a new array...

But the problem you mention stems from the fact that like a Collection,
the ParamArray is an array of variants. Each member of the array is
a variant, even if you passed them as arrays.

So, don't use a parameter like arr() As Double, use arr As Variant,
and just treat it like it was an array....


LFS
 
L

Levan Jgarkava

Hi,
You can't change them in place, you'd need to create a new array...

But the problem you mention stems from the fact that like a Collection,
the ParamArray is an array of variants. Each member of the array is
a variant, even if you passed them as arrays.

So, don't use a parameter like arr() As Double, use arr As Variant,
and just treat it like it was an array....
Thanks you for help. I think that it's good resolution of my task.


Best Regards,
Levan Jgarkava
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top