Results 1 to 11 of 11

Thread: Array Information

Threaded View

  1. #2
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    10,918

    Re: Array Information

    For everything but BSTR arrays and a Variant containing an array, I just use the following:

    Code:
    
    Option Explicit
    '
    Public Declare Function ArrPtr Lib "msvbvm60" Alias "VarPtr" (a() As Any) As Long
    Public Declare Function GetMem2 Lib "msvbvm60" (ByRef Source As Any, ByRef Dest As Any) As Long ' Always ignore the returned value, it's useless.
    Public Declare Function GetMem4 Lib "msvbvm60" (ByRef Source As Any, ByRef Dest As Any) As Long ' Always ignore the returned value, it's useless.
    '
    
    Public Function ArrayDims(pArray As Long) As Integer
        ' Won't work with BSTR arrays nor a Variant containing an array.
        ' Works with arrays of variants, of all other intrinsic types, of objects, and of fixed length strings.
        ' Example:  Debug.Print ArrayDims(ArrPtr(SomeArray()))
        ' Returns 0 if not dimensioned.
        '
        Dim pSA As Long
        If pArray = 0& Then Exit Function
        GetMem4 ByVal pArray, pSA                                       ' De-reference.
        If pSA = 0& Then Exit Function                                  ' Dynamic undimensioned array.
        GetMem2 ByVal pSA, ArrayDims
    End Function
    
    
    On BSTR arrays and a Variant containing an array, different approaches must be taken.

    -----

    Here's some test code that can be thrown into a Form1:
    Code:
    
    Option Explicit
    
    Private Sub Form_Load()
        Dim a() As Long
    
        Debug.Print "a()", ArrayDims(ArrPtr(a))
        ReDim a(1, 2, 3, 4, 5)
        Debug.Print "a()", ArrayDims(ArrPtr(a))
    
    End Sub
    
    
    Last edited by Elroy; Mar 18th, 2022 at 12:48 AM.
    Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.

Tags for this Thread

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