Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

# dimensions in an array?

0 views
Skip to first unread message

r...@susq.com

unread,
Sep 17, 1998, 3:00:00 AM9/17/98
to
How can one determine the number of dimensions of an
array in VB? I am writing some functions that
are specific to vectors, and I want to make sure
that the input argument is a 1D array.

Vivek Rao

-----== Posted via Deja News, The Leader in Internet Discussion ==-----
http://www.dejanews.com/rg_mkgrp.xp Create Your Own Free Member Forum

Jim Mack

unread,
Sep 17, 1998, 3:00:00 AM9/17/98
to
VB has no native way to do this... the usual method is to turn on error
trapping and attempt an access in some dimension. If it fails, the array
doesn't have that many dimensions.

The information is available to other-language DLLs, however, because VB
passes a SAFEARRAY, which is an OLE data construct that contains this
information. So you can write this in C or some other language and get what
you need.

Our Stamina library, for example, has the function DxArrayDimmed, which
reports the number of dimensions for an array. It was designed to detect
arrays with no dimensions, e.g. Dim Ary() As Type, which will return 0, but
it does this by getting the current number of dimensions. Stamina is a
library of 350+ functions for VB and VBA written in 32-bit assembly
language.

--
Jim Mack
MicroDexterity, Inc
PO Box 5372
Plymouth, MI 48170-5372

http://www.microdexterity.com

Sales: 888-891-0700 sa...@mdxi.com
Support: +1 734-453-5872 sup...@mdxi.com
Fax: +1 734-453-8942


r...@susq.com wrote in message <6trcef$6qt$1...@nnrp1.dejanews.com>...

Joachim Flodqvist

unread,
Sep 18, 1998, 3:00:00 AM9/18/98
to
r...@susq.com wrote:
>
> How can one determine the number of dimensions of an
> array in VB? I am writing some functions that
> are specific to vectors, and I want to make sure
> that the input argument is a 1D array.

I've already posted this a few times, but I hope you'll excuse me if I
do it again...

This procedure is geared towards arrays that are storable in variants,
but it's not that common with unknown UDT-arrays, so I think this is
sufficient. Also, I haven't looked at the code since I first wrote it
so there may be things in it that could be optimized. If so, go ahead
and optimize away, and please mail me a copy.

If you want to know the details behind it, check out the SAFEARRAY,
SAFEARRAYBOUND and VARIANT structs in your local COM reference (Visual
C++ help will do).

' Need to declare this once for arrays
Private Declare Function VarPtrArray Lib "msvbvm50.dll" _
Alias "VarPtr" (Ptr() As Any) As Long
Private Declare Sub CopyMemory Lib "kernel32" _
Alias "RtlMoveMemory" (pDest As Any, pSrc As Any, _
ByVal ByteLen As Long)

' ABSTRACT:
' Returns the number of dimensions of
' an array. Very useful for determining
' if the array can be used with LBound()
' and UBound().
'
' Normally, the VB function IsArray()
' returns true for any non-variant array
' even if LBound() and UBound() will fail.
' With this function, you get a 0 for the
' dimensions if the array is Erase()d.
'
' INPUT:
' Any array
'
' OUTPUT:
' The number of dimensions of the array.
' This is typically 0 for Erase()d arrays,
' or arrays that haven't been ReDimmed yet.
'
' NOTES:
' I constructed this function after getting
' tired of not being able to check if an
' array of Long() could be manipulated or not.
'
' HISTORY:
' 971119: JFK; First version
'
Public Function afnArrayDims(ByRef varArray As Variant) As Long
On Error GoTo Err_afnArrayDims

Dim pArray As Long
Dim ppArray As Long
Dim ppVarStruct As Long
Dim intVarType As Integer
Dim intDims As Integer
Dim lngMaskedType As Long

intVarType = VarType(varArray)

' Check so it's actually an array
If intVarType And vbArray Then

' Get the pointer to the variant
ppVarStruct = VarPtr(varArray)

' Get the pointer to the data *inside* the variant
' The VARTYPE is 2 bytes, and each of the three
' reserved words are also 2 bytes -> 8 bytes
CopyMemory ppArray, ByVal ppVarStruct + 8, 4

' Just a precaution. Hasn't happened yet, but...
If ppArray = 0 Then Exit Function

lngMaskedType = intVarType And Not vbArray
' Check the type of the array.
If lngMaskedType = vbVariant Then
' Evidently, this is a variant array.

' We've already dereferenced the variant and
' gotten to its data - a SafeArray.

' As the first part of a safearray is the
' cDims-part, we just need to copy the cDims
' descriptor

CopyMemory intDims, ByVal ppArray, Len(intDims)

afnArrayDims = intDims
Else
' This is a safearray of some standard datatype
' that has been wrapped in a variant by the
' casting in the function call.

' We access the SafeArray struct pointed at by
' the Variant
CopyMemory pArray, ByVal ppArray, 4

If pArray Then
' The first part of the SafeArray struct is
' the cDims descriptor. Return it.
CopyMemory intDims, ByVal pArray, Len(intDims)
afnArrayDims = intDims
End If
End If
End If

Exit_afnArrayDims:
Exit Function

Err_afnArrayDims:
Err.Raise Err.Number, CLASS_NAME & ".afnArrayDims()", Err.Description

End Function

> Vivek Rao

Regards
/Joachim

--
Ubi signo?
joa...@ecs.se


0 new messages