The number of dimensions of an array

The number of dimensions of an array

Using “pure” VB, the only way to build a generic routine that returns the number of dimensions of an array passed as an argument is using a loop that repeatedly tests the LBound (o UBound) function until it fails:

Function ArrayDims(arr As Variant) As Integer    Dim i As Integer, bound As Long    On Error Resume Next    For i = 1 To 60        bound = LBound(arr, i)        If Err Then            ArrayDims = i - 1            Exit Function        End If    NextEnd Function

You can write a faster routine by peeking at the memory location where Visual Basic holds the number of dimensions of any array:

Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (dest As _    Any, source As Any, ByVal bytes As Long)Function ArrayDims(arr As Variant) As Integer    Dim ptr As Long    Dim VType As Integer        Const VT_BYREF = &H4000&        ' get the real VarType of the argument    ' this is similar to VarType(), but returns also the VT_BYREF bit    CopyMemory VType, arr, 2        ' exit if not an array    If (VType And vbArray) = 0 Then Exit Function        ' get the address of the SAFEARRAY descriptor    ' this is stored in the second half of the    ' Variant parameter that has received the array    CopyMemory ptr, ByVal VarPtr(arr) + 8, 4        ' see whether the routine was passed a Variant    ' that contains an array, rather than directly an array    ' in the former case ptr already points to the SA structure.    ' Thanks to Monte Hansen for this fix        If (VType And VT_BYREF) Then        ' ptr is a pointer to a pointer        CopyMemory ptr, ByVal ptr, 4    End If        ' get the address of the SAFEARRAY structure    ' this is stored in the descriptor        ' get the first word of the SAFEARRAY structure    ' which holds the number of dimensions    ' ...but first check that saAddr is non-zero, otherwise    ' this routine bombs when the array is uninitialized    ' (Thanks to VB2TheMax aficionado Thomas Eyde for    '  suggesting this edit to the original routine.)    If ptr Then        CopyMemory ArrayDims, ByVal ptr, 2    End IfEnd Function

Share the Post:
XDR solutions

The Benefits of Using XDR Solutions

Cybercriminals constantly adapt their strategies, developing newer, more powerful, and intelligent ways to attack your network. Since security professionals must innovate as well, more conventional endpoint detection solutions have evolved

AI is revolutionizing fraud detection

How AI is Revolutionizing Fraud Detection

Artificial intelligence – commonly known as AI – means a form of technology with multiple uses. As a result, it has become extremely valuable to a number of businesses across

AI innovation

Companies Leading AI Innovation in 2023

Artificial intelligence (AI) has been transforming industries and revolutionizing business operations. AI’s potential to enhance efficiency and productivity has become crucial to many businesses. As we move into 2023, several

data fivetran pricing

Fivetran Pricing Explained

One of the biggest trends of the 21st century is the massive surge in analytics. Analytics is the process of utilizing data to drive future decision-making. With so much of

kubernetes logging

Kubernetes Logging: What You Need to Know

Kubernetes from Google is one of the most popular open-source and free container management solutions made to make managing and deploying applications easier. It has a solid architecture that makes

ransomware cyber attack

Why Is Ransomware Such a Major Threat?

One of the most significant cyber threats faced by modern organizations is a ransomware attack. Ransomware attacks have grown in both sophistication and frequency over the past few years, forcing

data dictionary

Tools You Need to Make a Data Dictionary

Data dictionaries are crucial for organizations of all sizes that deal with large amounts of data. they are centralized repositories of all the data in organizations, including metadata such as