|
|
|
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: |
Click here to copy the following block | 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 Next End Function |
You can write a faster routine by peeking at the memory location where Visual Basic holds the number of dimensions of any array: |
Click here to copy the following block | 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& CopyMemory VType, arr, 2 If (VType And vbArray) = 0 Then Exit Function CopyMemory ptr, ByVal VarPtr(arr) + 8, 4 If (VType And VT_BYREF) Then CopyMemory ptr, ByVal ptr, 4 End If If ptr Then CopyMemory ArrayDims, ByVal ptr, 2 End If End Function |
|
|
|
Submitted By :
Nayan Patel
(Member Since : 5/26/2004 12:23:06 PM)
|
|
|
Job Description :
He is the moderator of this site and currently working as an independent consultant. He works with VB.net/ASP.net, SQL Server and other MS technologies. He is MCSD.net, MCDBA and MCSE. In his free time he likes to watch funny movies and doing oil painting. |
View all (893) submissions by this author
(Birth Date : 7/14/1981 ) |
|
|