Library tutorials & articles

Arrays

Getting the size of the array

Sometimes, you will need to find the upper and lower limits of an array. To do this you can use the LBound and UBound functions. This is especially useful if you have used the ReDim statment (see dynamic arrays). To find the upper bound of an array (ie the ... To number part) use the UBound Function, using the following syntax:

UBound(ArrayName, Dimension)

ArrayName is the name of your array. Dimension is an optional integer representing the dimension number for use when using multidimensional arrays (discussed later in this tutorial). To find the lower bound of an array (ie the number To ... part of the array) use the LBound function, using the following syntax:

LBound(ArrayName, Dimension)

ArrayName is the name of your array. Dimension is an optional integer representing the dimension for use when using multiple dimensional arrays. The following code fills a list box with the values of all the items in gArray.

' Get the lower and upper values of gArray
LowerVal = LBound(gArray)
UpperVal = UBound(gArray)
For i = LowerVal To UpperVal
    ' Add a new item to the list, containing the
    ' next item in the array
    List1.AddItem gArray(i)
Next

Comments

  1. 15 May 2007 at 09:02

    I'm a bit confuse. I would like user to insert how many data they want to put in the array.

    When I declare the array like this

    Dim subjArr( ) As DataType

    How should I write the code so array size is according to user input. This is the code for user to insert how many records should be inserted.

    Dim SubArr() As DataType
    Dim subjNo As String
    subjNo = InputBox("Enter the no of subjects to calculate: ")
    MsgBox (subjNo + " subjects will be calculated")




  2. 20 Dec 2006 at 10:37
    It would work if you used Dim instead of Const. A constant array is not possible in VB and I really don't see the point. Just use a normal array and make sure that it is not modified.
  3. 17 Nov 2003 at 16:35

    Thanks James


    This is a good Array Tutorial for VB Newbies like me.


  4. 29 Apr 2003 at 23:49
    These guys have a perfect working example. The randomizer isn't truly random but completely adequate for basic purposes. I couldn't get it to work as a function, but it worked great when I simply used to function code in-line, so I know it works.

    http://www.4guysfromrolla.com/webtech/110800-1.shtml

    I really just added this in case anyone comes across this in a search as I did

    +++++++++++++++

    check it: FLEETING IMAGE
  5. 03 Apr 2003 at 16:16
    How do I make a constant array? When I try "const data(1 to 72) as byte" it doesn't work, where it would if I used Dim rather than const. Also, isn't there some way to set a value to the whole thing? Like with one command making data(1) = 4, data(2) = 7 etc.
  6. 01 Nov 2002 at 23:17

    No, I believe he means something like this:


    Say you had a string array with 6 elements, beginning with zero.


    The values were arranged like this:


    strArray(0) = "Step 1"
    strArray(1) = "Step 2"
    strArray(2) = "Step 3"
    strArray(3) = "Step 4"
    strArray(4) = "Step 5"
    strArray(5) = "Step 6"


    He wants to randomly arrange the elements' contents, like this:


    strArray(0) = "Step 4"
    strArray(1) = "Step 3"
    strArray(2) = "Step 5"
    strArray(3) = "Step 1"
    strArray(4) = "Step 6"
    strArray(5) = "Step 2"


    I hope this is correct, and/or clear.


  7. 14 Jun 2002 at 15:33

    Are you speaking of randomly pulling one of the values as in say a random name generator?

  8. 30 May 2002 at 08:19

    Can anyone tell me if it's possible to randomly sort the elements in an array?

  9. 20 May 2002 at 19:07

    I'm not familiar to 3d arrays, but could you use something similar to:

    Code:
    Array2 = Split ( Join ( Array1 , "," ) "," )

    I use that to copy 1d arrays.
    Not the best piece of code but meh.

  10. 13 May 2002 at 17:09
    Does anyone know a way of transferring the content of an array into another array WITHOUT doing this?:
    Public Sub  MoveArray()
       For  Z = 0 To  10
           Array2(Z) = Array1(Z)
       Next  Z
    End Sub

    I am dealing with some very large (3 dimensional) arrays and the above kind of operation seems a little slow for the user to sit around waiting for.  (Especially when it will need to be used in the Undo Function)
    Any suggestions will be gratefully received.
    couling@fsnet.co.uk">pcouling@fsnet.co.uk
  11. 01 Jan 1999 at 00:00

    This thread is for discussions of Arrays.

Leave a comment

Sign in or Join us (it's free).

James Crowley James first started this website when learning Visual Basic back in 1999 whilst studying his GCSEs. The site grew steadily over the years while being run as a hobby - to a regular monthly audience ...
AddThis

Related discussion

Related podcasts

  • Christian Beauclair

    14 mai 2008 (�mission #0074) ::.Christian Beauclair: Stratégies de migration VB6 vers .NET Nous discutons avec Christian Beauclair des stratégies de migration VB6 vers .NET. Entre autres, nous discutons comment utiliser le "VB 6 Code Advisor" et le "Interop Forms Toolkit" pour ajouter la puiss...

We'd love to hear what you think! Submit ideas or give us feedback