# Thread: VB6 - how create a function with 2D array on parameter?

1. ## VB6 - how create a function with 2D array on parameter?

i need a 2D array parameter on a function:
Code:
```Private Type Position3D
X As Double
Y As Double
Z As Double
End Type

Private Sub DrawLine2(Vectors() As Position3D)```
the Line have a Origin and Destiny coordenates, that's why the 'Vectors' must be 2D)... how can i test:
- if the Vectors is 2D?
- how many lines have on array?

2. ## Re: VB6 - how create a function with 2D array on parameter?

It's your function, can't you ensure you only pass 2D arrays to that sub?
As far as how many array elements: UBound(Vectors, 1) & UBound(Vectors, 2) for the 1st and 2nd dimension respectively

Maybe the problem is the user-defined type? Make the sub Friend vs. Private and declare the user-defined type Public in a module.

3. ## Re: VB6 - how create a function with 2D array on parameter?

if UBound(Vectors, 1) >0 and UBound(Vectors, 2)>0 and UBound(Vectors, 3)=0 then it's 2D... right?

4. ## Re: VB6 - how create a function with 2D array on parameter?

No, it's 3D. Maybe an example to illustrate
Code:
```Dim Vectors(0 To 10, 0 To 100) ' 2D
Debug.Print LBound(Vectors, 1); UBound(Vectors, 1), LBound(Vectors, 2); UBound(Vectors, 2)

Dim Vectors2(0 To 10, 0 To 100, 0 To 50) ' 3D
Debug.Print LBound(Vectors2, 1); UBound(Vectors2, 1), LBound(Vectors2, 2); UBound(Vectors2, 2), LBound(Vectors2, 3); UBound(Vectors2, 3)```

5. ## Re: VB6 - how create a function with 2D array on parameter?

i did these function:
Code:
```Private Function GetDimensionsArray(arrays() As Position3D) As Integer
Dim ArrayDimensionsCount As Integer
ArrayDimensionsCount = 1
Do
If (UBound(arrays, ArrayDimensionsCount) > 0) Then
ArrayDimensionsCount = ArrayDimensionsCount + 1
Else
Exit Do
End If
Loop
GetDimensionsArray = ArrayDimensionsCount
End Function```
i did the test for 1D array and works.. thanks for all

6. ## Re: VB6 - how create a function with 2D array on parameter?

but when i do:
Code:
```Dim test(2, 3) As Position3D
MsgBox CStr(GetDimensionsArray(test()))```
i get only '1', why?

7. ## Re: VB6 - how create a function with 2D array on parameter?

Actually I'm surprised you get anything - should get an error at some point. You cannot test for an array bounds that does not exist. So if the array was 1D, and ArrayDimensionsCount is incremented to 2, then when "UBound(arrays, ArrayDimensionsCount) > 0" is executed -- error.

Are you saying that the passed array can be 1D or 2D or 3D? If so, we can get the number of dimensions without using trial & error via its SafeArray structure. But I am not on a VB machine and won't be until tomorrow morning. Maybe others will show you before then?

8. ## Re: VB6 - how create a function with 2D array on parameter?

I get "Subscript out of range" on this line

If (UBound(arrays, ArrayDimensionsCount) > 0) Then

Code:
```Option Explicit

Private Declare Function ArrPtr Lib "msvbvm60" Alias "VarPtr" (Ptr() As Any) As Long
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)

Private Type Position3D
X As Double
Y As Double
Z As Double
End Type

Dim test(2, 3, 4) As Position3D
MsgBox GetDimensionsArray(test())
End Sub

Private Function GetDimensionsArray(arrays() As Position3D) As Integer
Dim lPtr            As Long
Dim nRetVal         As Integer

Call CopyMemory(lPtr, ByVal ArrPtr(arrays), 4)
If lPtr <> 0 Then
Call CopyMemory(nRetVal, ByVal lPtr, 2)
End If
GetDimensionsArray = nRetVal
End Function```
cheers,
</wqw>

9. ## Re: VB6 - how create a function with 2D array on parameter?

thank you so much to all.
it works like a charm.. thank you

10. ## Re: VB6 - how create a function with 2D array on parameter?

Code:
```Sub studentScores(ByVal name As String, ByVal ParamArray scores() As String)
Debug.WriteLine("Scores for " & name & ":" & vbCrLf)
' Use UBound to determine largest subscript of the array.
For i As Integer = 0 To UBound(scores, 1)
Debug.WriteLine("Score " & i & ": " & scores(i))
Next i
End Sub```

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•