Dim and erase a public array at runtime

Posted on 2005-04-11
Last Modified: 2010-04-23
I am sure this may be quite basic, but am just returning to coding after many years and am stuck on the following issue, so any help would be much appreciated. I am writing a application that requires a large multi dimensional array which I have initially setup as a public array.

In order to release the memory used, I want to "erase" the array after use. However I may need to reuse the array, so how can I "dim" the array again at runtime, still making it public. I would not need any data retained.

For instance if the array is xx(100,200,50)  is the best way for me to publically dim xx in the design phase and then after use at runtime release its memory using a redim xx(0,0,0) and then redim back to xx(100,200,50) if I need to reuse xx again.

Or is there a much easier way ?

Many thanks for your help

Question by:swtiley
    LVL 13

    Expert Comment

    You can only redim the last dimension of an array IIRC.

    .NET now has ArrayList which is a one dimensional array that automatically updates it's size.

    To use a traditional array you could try this

    Public SomeArray() as String

    Sub CreateLargeArray ()
    Dim LargeArray (100,200,50)

    'Some code to fill large array

    SomeArray = LargeArray

    end sub

    after the sub is run LargeArray is Garbage Collected and you can empty SomeArray at any time using

    SomeArray = Nothing

    LVL 14

    Expert Comment

    I would use
      xx = Nothing
    to release the resource the array used, then vb .net's  garbage collection function will release the memory.

    When you need to use xx again, Dim xx(...) as Array
    LVL 2

    Expert Comment

    Hi swtiley,

    Expanding on Corey2 example:

    Friend SomeArray(,,) as String <<-multidimentional array made available to all

    Sub CreateLargeArray ()

    Redim LargeArray (100,200,50)

    'Some code to fill large array

    'Empty the array and free ALL memory associated with the array

    Erase LargeArray

    'Fill it back up...
    Redim LargeArray (200,300,50)

    'Fill it with more data
    Expand the array but 'Preserve' the current data
    Redim Preserve  LargeArray (300,400,100)

    ' add mopde data

    end sub

    Hope this helps,

    LVL 13

    Accepted Solution

    From my former post
    "You can only redim the last dimension of an array IIRC. "

    I looked it up and I was partially right this is only the case if you want to preserve the data

    For example This is OK

    Dim LargeArray () as String

    Redim LargeArray (300,200,50)
    'do something
    Redim LargeArray (400,300,60) 'this will remove all data from the array and resize it

    But in the case that you want to preserve the data in the array you can only change the last dimension of the array For example

    Dim LargeArray () as String

    Redim LargeArray (300,200,50)
    'do something
    'the following will throw an error
    Redim Preserve LargeArray (400,300,60)
    'but the following will work
    Redim Preserve  LargeArray (300,200,200)

    for more information on ReDim look here




    Featured Post

    How to improve team productivity

    Quip adds documents, spreadsheets, and tasklists to your Slack experience
    - Elevate ideas to Quip docs
    - Share Quip docs in Slack
    - Get notified of changes to your docs
    - Available on iOS/Android/Desktop/Web
    - Online/Offline

    Join & Write a Comment

    This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
    It’s quite interesting for me as I worked with Excel using for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
    Internet Business Fax to Email Made Easy - With eFax Corporate (, you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
    Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

    733 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now