Google Mail Calendar Documents Reader Web more »
Recently Visited Groups | Help | Sign in
Google Groups Home
Message from discussion Array Size

View Parsed - Show only message text

From: "Mike D Sutton" <Mike.Sut...@btclick.com>
References: <Ovl$v1juCHA.2296@TK2MSFTNGP09> <3o032vgq944rm04lccuo31ranqbknrae7h@4ax.com>
Subject: Re: Array Size
Date: Sun, 12 Jan 2003 20:51:25 -0000
Lines: 93
Organization: EDais
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2800.1123
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1123
Message-ID: <eW8Z9wnuCHA.2668@TK2MSFTNGP12>
Newsgroups: microsoft.public.vb.general.discussion
NNTP-Posting-Host: host213-123-174-117.in-addr.btopenworld.com 213.123.174.117
Path: archiver1.google.com!news1.google.com!sn-xit-02!sn-xit-03!sn-xit-01!sn-xit-08!supernews.com!telocity-west!DIRECTV!204.94.211.44!enews.sgi.com!msrnewsc1!TK2MSFTNGP08!TK2MSFTNGP12

> The generic formula is
>
> UBound(MyArray) - LBound(MyArray) + 1
>
> So, if the array is zero-based then the number of elements becomes
>
> Ubound(MyArray) + 1
>
> If the array is 1-based then it is just UBound(MyArray).

This doesn't take into account multi-dimensional or un-initialised arrays
though, for that you'd need something like this:
http://www.mvps.org/vbnet/code/helpers/getarraydims.htm
Here's another solution for you:

'***
Private Type typArrayDim
    adLow As Long
    adHigh As Long
End Type

Private Function ArrSize(ByRef inArr As Variant, _
    ByRef outArr() As typArrayDim) As Long
    Dim TempDim As Long

    Do
        TempDim = -1
        On Error Resume Next
        TempDim = LBound(inArr, ArrSize + 1)
        On Error GoTo 0

        If (TempDim <> -1) Then
            ReDim Preserve outArr(ArrSize) As typArrayDim

            With outArr(ArrSize)
                .adLow = TempDim
                .adHigh = UBound(inArr, ArrSize + 1)
            End With

            ArrSize = ArrSize + 1
        End If
    Loop While (TempDim <> -1)
End Function

Private Sub SayArrayDim(ByRef inArr As Variant)
    Dim ArrDims() As typArrayDim
    Dim NumDims As Long
    Dim LoopDims As Long
    Dim SayDims As String

    NumDims = ArrSize(inArr, ArrDims())
    SayDims = TypeName(inArr)

    If (NumDims) Then
        SayDims = Left$(SayDims, Len(SayDims) - 1)

        For LoopDims = 0 To NumDims - 1
            With ArrDims(LoopDims)
                SayDims = SayDims & .adLow & " To " & .adHigh & _
                    IIf(LoopDims < (NumDims - 1), ", ", "")
            End With
        Next LoopDims

        SayDims = SayDims & ")"
    End If

    MsgBox SayDims
End Sub

Private Sub Form_Load()
    Dim MyArr(1 To 2, 3 To 4, 5 To 6, 7 To 8, 9 To 10) As Long
    Dim EmptyArr() As Long
    Dim MyVar As Long

    Call SayArrayDim(MyArr)
    Call SayArrayDim(EmptyArr)
    Call SayArrayDim(MyVar)
End Sub
'***

Hope this helps,

    Mike


 -- EDais --

 - Microsoft Visual Basic MVP -
WWW: Http://EDais.earlsoft.co.uk/
Work E-Mail: ED...@btclick.com
Other E-Mail: Mike.Sut...@btclick.com



Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy
©2009 Google