-
Jul 14th, 2020, 04:50 PM
#1
Thread Starter
PowerPoster
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?
-
Jul 14th, 2020, 04:56 PM
#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.
Last edited by LaVolpe; Jul 14th, 2020 at 05:00 PM.
-
Jul 14th, 2020, 05:00 PM
#3
Thread Starter
PowerPoster
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?
-
Jul 14th, 2020, 05:16 PM
#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)
-
Jul 14th, 2020, 05:23 PM
#5
Thread Starter
PowerPoster
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
-
Jul 14th, 2020, 05:29 PM
#6
Thread Starter
PowerPoster
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?
-
Jul 14th, 2020, 05:46 PM
#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?
-
Jul 15th, 2020, 05:26 AM
#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
Try this instead
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
Private Sub Form_Load()
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>
Last edited by wqweto; Jul 15th, 2020 at 05:38 AM.
-
Jul 15th, 2020, 01:53 PM
#9
Thread Starter
PowerPoster
Re: VB6 - how create a function with 2D array on parameter?
thank you so much to all.
it works like a charm.. thank you
-
Jul 21st, 2020, 10:26 PM
#10
Lively Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|