Results 1 to 10 of 10

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

  1. #1

    Thread Starter
    PowerPoster joaquim's Avatar
    Join Date
    Apr 2007
    Posts
    3,515

    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?
    VB6 2D Sprite control

    To live is difficult, but we do it.

  2. #2
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,543

    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.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  3. #3

    Thread Starter
    PowerPoster joaquim's Avatar
    Join Date
    Apr 2007
    Posts
    3,515

    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?
    VB6 2D Sprite control

    To live is difficult, but we do it.

  4. #4
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,543

    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)
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  5. #5

    Thread Starter
    PowerPoster joaquim's Avatar
    Join Date
    Apr 2007
    Posts
    3,515

    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
    VB6 2D Sprite control

    To live is difficult, but we do it.

  6. #6

    Thread Starter
    PowerPoster joaquim's Avatar
    Join Date
    Apr 2007
    Posts
    3,515

    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?
    VB6 2D Sprite control

    To live is difficult, but we do it.

  7. #7
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,543

    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?
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  8. #8
    PowerPoster wqweto's Avatar
    Join Date
    May 2011
    Posts
    3,027

    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>

  9. #9

    Thread Starter
    PowerPoster joaquim's Avatar
    Join Date
    Apr 2007
    Posts
    3,515

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

    thank you so much to all.
    it works like a charm.. thank you
    VB6 2D Sprite control

    To live is difficult, but we do it.

  10. #10
    Lively Member
    Join Date
    Jan 2020
    Posts
    121

    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
  •  



Click Here to Expand Forum to Full Width