Keyboard Face

when you find QWERTY imprinted on your cheek - it's time to go to bed.

Tags

asp CSS css menus dreamweaver flash gallery2 hosting iis7 Javascript jquery jquery ui left mac mssql os9 php printer rdp SEO sql sql server windows wordpress

VBScript Array Length

November 21, 2005 by Tom 12 Comments

Apparently there is no direct method to determine the length of an array in VBScript. The best way I could find is to use UBound(ArrayName). This will return the upper limit of the array. Unfortunately, it doesn’t account for empty items in the array. In the example below Length will be equal to 10, not 1.

Dim NewArray(10)
NewArray(0) = "Apple"
Length = UBound(NewArray) 'Length is equal to 10, not 1

Here is a good link to VBScript Array functions.

http://www.shocknet.org.uk/defpage.asp?pageID=30

If you know of a better way to determine the length, please let me know.

Filed Under: Classic ASP, VBScript

Comments

  1. MrBester says

    May 26, 2006 at 4:47 am

    It depends what you mean by “length”.
    If you define an array in JavaScript (var arr=[];) and then set the 4th element to something (arr[3]=’something’;) then the length property will report 4 in the same way as VBScript. Are they both wrong?
    If you mean length to be “number of non-empty elements” then:
    Function Length(ByRef vArray)
    Dim llngIndex
    Dim llngCount
    llngCount = 0
    For llngIndex = 0 To UBound(vArray)
    If Not(IsNull(vArray(llngIndex))) And Not(IsEmpty(vArray(llngIndex))) And Not(vArray(llngIndex) Is Nothing) Then
    llngCount = llngCount + 1
    End If
    Next
    Length = llngCount
    End Function

    should do the trick, unless you count an element containing vbNullString (or “”) as empty as well, which then gets you into an argument about what “empty” means.
    I’ve always worked on the principle that an array length is the number of elements within it, regardless of the content of those elements because that is what properties called “length” return to me.

    “Size” rears its ugly head as an even trickier beast. Does “size” mean number of elements, bytelength of contents, memory consumed by the object?

  2. Tom says

    May 26, 2006 at 9:43 am

    Wow – thanks! That’s a good function. You brought up some good points in regards “emptyness.”

    I originally was searching for the solution to find array length after using the split command (because you wouldn’t have explicitly defined the length of the array), but this could go along nicely with my script.

  3. Dave S says

    February 7, 2008 at 12:03 pm

    You could always create a counter, loop thru the array & increment as you go. It may be ugly but it works

    Dim counter
    For Each x in myArray
    counter = counter + 1
    Next

  4. 0xG says

    September 10, 2008 at 7:49 am

    What is even more perplexing is how to determine number if elements in a dynamic array. For instance:

    Dim Dynamic()
    WScript.Echo “Array size is ” & UBound(Dynamic)

    >Microsoft VBScript runtime error: Subscript out of range

  5. dummy says

    October 27, 2008 at 4:08 am

    Give your dynamic array a size of 0:

    Dim Dynamic(0)

    Then it won’t error because it will have a length.

  6. Mike Irving says

    February 26, 2009 at 10:02 am

    The loop counter is very useful, shame there is now built in loop property.

  7. Frederic Duplessis says

    March 19, 2009 at 2:01 pm

    Dynamic arrays can NOT be defined as “dummy” was suggesting Dim dynamic (0).

    In VBScript, the subscript in Dim statement really is what will be returned via UBound function, which is namely the highest subscript one may use to refer to an element in the array and NOT the size of it. Thus this statement Dim dynamic (0) would really create an fixed-size, non-dynamic, array variable containing one element accessible with subscript 0.

    Dave’s solution seems the only avenue here.

  8. chaitanya says

    April 13, 2009 at 2:44 am

    Hi every one,

    I need to find the number of bytes occupied by a array variable.Can any one help me please.

  9. chaitanya says

    April 13, 2009 at 2:59 am

    Size means bytelength of content

  10. LAYGO says

    December 9, 2009 at 9:10 am

    Frederic is right.

    If you want a true dynamic array:

    Dim DynamicArray()

    For x = 0 To n
    ReDim DynamicArray(x) Preserve
    ‘ some processing here
    DynamicArray(x) = “results of some processing here”
    Next

  11. Amar says

    January 8, 2010 at 6:37 pm

    Problem in above solution is, every time when Array is redimed previous value will be get flushed out, so only last value will be stored

  12. Alex says

    March 15, 2010 at 7:14 am

    @Amar
    Not so. The Preserve keyword on the ReDim keeps all the existing data in the array intact. Although if memory serves me correctly the syntax is with Preserve directly following the ReDim statement.

    ReDim Preserve DynamicArray(10)

    @0xG
    Another way I found of getting around the error generated is to ReDim your dynamic array right after declaring it. That then gives you a ubound to work with from the start.

    Dim Dynamic()
    ReDim Dynamic(0)

    WScript.Echo “Array size is ” & UBound(Dynamic)

    You can then go on to do your other processing too as per the example in LAYGO’s loop.

Leave a Reply Cancel reply

Your email address will not be published.

Pages

  • About Me
  • WordPress Permalinks in IIS using Custom 404 Redirect

Archives

  • May 2019
  • February 2019
  • January 2018
  • May 2016
  • April 2016
  • December 2015
  • November 2015
  • May 2015
  • April 2015
  • January 2013
  • March 2012
  • February 2012
  • December 2011
  • September 2011
  • August 2011
  • November 2010
  • October 2010
  • June 2010
  • April 2010
  • March 2010
  • February 2010
  • January 2010
  • December 2009
  • November 2009
  • September 2009
  • August 2009
  • July 2009
  • April 2009
  • March 2009
  • February 2009
  • January 2009
  • November 2008
  • September 2008
  • July 2008
  • June 2008
  • May 2008
  • April 2008
  • March 2008
  • February 2008
  • December 2007
  • October 2007
  • September 2007
  • August 2007
  • July 2007
  • June 2007
  • May 2007
  • January 2007
  • November 2006
  • October 2006
  • September 2006
  • August 2006
  • July 2006
  • June 2006
  • May 2006
  • April 2006
  • March 2006
  • February 2006
  • January 2006
  • December 2005
  • November 2005
  • October 2005
  • September 2005
  • May 2005

ASP.NET Resources

  • Keven Roth’s Code Library
  • Lemon Law

Blogroll

  • Arin Morfopoulos
  • Jamie
  • Physical Therapists

Code Search Engines

  • Koders
  • Krugle

CSS & Design Resources

  • CSS Basics

Other

  • Amazon
  • Car Repair Questions
  • Cool Quizzes
  • Delphi LA
  • Physical Therapy Clinic Directory
  • Quickbooks Questions
  • Secret Santa Game

Site\'s I\'ve Designed or Helped With

  • Area Rugs
  • Dental Implants
  • Dental Website Design
  • FHA Loans
  • Secret Santa Gift Ideas