Send a suggestion!

We're building a brand new version of the site, and we'd love to hear your ideas

Members

Technology Zones

IBM Learning Center

Articles

Hosted By

MaximumASP

Info

Rated
Read 68,800 times

Contents

Related Categories

Arrays - Dynamic Arrays

Dynamic Arrays

When using arrays you must be careful not to consume too much memory. For example

Dim MyArray (10000) As Long

Because each element is declared as long, and a long variable occupies 4 bytes of memory, the MyArray requires 40 004 (10 001 x4) bytes of memory. This may not sound like much, but when you have 10 such arrays in your program these arrays consume 400 040 (40 004 x 10) bytes of memory. Therefore, it is wise to always try to set the size of your arrays to the minimum your program requires. Sometimes, however, it is only possible to detemine this during runtime. In these cases you can use the ReDim statement to change the size of an array. An array that changes its size during runtime is called a dynamic array.

When you declare a dynamic array, you do not declare it like a fixed array. When you declare a dynamic array the size is not specified. Instead you use the following syntax:

Dim ArrayName() As DataType

Dim is the scope of the array. If you declare it in a Form, use Dim, if you declare it in a Module and you want every procedure to access it, declare it as Global, otherwise use Public. If you declare the array in a procedure use Dim. ArrayName is the name of the array. DataType is a valid datatype. Normally Integer (-32 768 to 32 767), String (A string of characters), Boolean (True or False).

You then use the ReDim statement in your procedures or functions, using the following syntax:

ReDim ArrayName(LowerValue To HigherValue)

In fact, it is almost identical to a normal declaration for a fixed array, except that 1), it is not a declaration, as it is executed at runtime, 2) it uses ReDim, and 3) there is no datatype declaration (this cannot be changed).

So, the following code declares a dynamic array called gArray, and then sets the size during runtime:

Dim sStringArray() As String
Sub Form1_Load()
    ' Initialise array
    ReDim sStringArray(1 To 10)
End Sub

This code assigns 10 elements to gArray when Form1 loads. Note: When using dynamic arrays, you must set the size of an array using the ReDim statement, before filling the array.

However, when using the ReDim statement, any values already in the array (if it has been resized previously), will be deleted. In some cases, this is not what you would want! So, you use the Preserve keyword:

ReDim Preserve ArrayName(LowerValue To HigherValue)

If the array has grown, there will be a number of blank array spaces at the end of the array. If the array has shrunk, you will lose the end items.

James first started writing tutorials on Visual Basic in 1999 whilst starting this website (then known as VB Web). Since then, the site has grown rapidly, and James has written numerous tutorials, articles and reviews on VB, PHP, ASP and C#. In October 2003, James formed the company Developer Fusion Ltd, which owns this website, and also offers various development services. In his spare time, he's a 3rd year undergraduate studying Computer Science in the UK. He's also a Visual Basic MVP.

Comments

  • Re: [19] Arrays

    Posted by elyssa83 on 15 May 2007

    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 ...

  • Re: Array constants and assigning to all values

    Posted by pcmattman on 20 Dec 2006

    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.

  • Thanks James

    Posted by csm1yy on 17 Nov 2003

    Thanks James

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

  • Here ya go.

    Posted by STmindfulORM on 29 Apr 2003

    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...

  • Array constants and assigning to all values

    Posted by HyperHacker on 03 Apr 2003

    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? ...