On last.fm: Check Out Radiohead Music, Events & Pics

Gain data storage flexibility in VB6 with dynamic arrays

Tags: Microsoft development tools, Microsoft Visual Basic 6.0, data storage, Peter Aitken, storage, dynamic array, Visual Basic Tips Newsletter

  • Save
  • Print
  • Digg This
  • 1

Takeaway: Dynamic arrays make it easy to change the size of arrays in a VB6 program while it's running. See how useful this can be when you don't know ahead of time how much data the array will store.

Advance your scripting skills to the next level with TechRepublic's free Visual Basic newsletter, delivered each Friday. Automatically sign up today!

The most commonly used array in VB6 programs is a static array. It has a fixed size, which is assigned when the array is declared, and that size remains the same throughout program execution. Another type of array, called a dynamic array, lets you change its size while the program is running. This can be very useful when you don't know ahead of time how much data the array will store.

To create a dynamic array, use the Dim statement; however, be sure that the parentheses are left empty. This statement declares a dynamic array of type Integer:

Dim MyArray() As Integer

Before using the array, you must set its size using the ReDim statement:

ReDimMyArray(100)

Now you can use the dynamic array just like any other array. When you need to change its size, use ReDim again. For instance:

ReDimMyArray(250)

By default, changing the size of a dynamic array erases all the data in the array. If you want to preserve the data, include the Preserve keyword:

ReDim Preserve MyArray(250)

This will preserve data in the original elements of the array--elements 0 through 100 in this example. If you reduce the size of the array, data in elements that are cut off are not preserved.

You can use dynamic arrays with any VB data type and with multi-dimensional as well as one-dimensional arrays.

  • Save
  • Print
  • Digg This
  • 1

Print/View all Posts Comments on this article

too simplifiedtjc_tek@...  | 07/01/05
OK.... Don't waste everyone's timePChem  | 09/09/05
RE: Gain data storage flexibility in VB6 with dynamic arraystechrep2@...  | 07/19/07
RE: Gain data storage flexibility in VB6 with dynamic arrayszhuan_ckc@...  | 08/06/07

What do you think?

Article Categories

Security
Security Solutions, IT Locksmith
Networking and Communications
E-mail Administration NetNote, Cisco Routers and Switches
CIO and IT Management
Project Management, CIO Issues, Strategies that Scale
Desktops, Laptops & OS
Windows 2000 Professional, Microsoft Word, Microsoft Excel, Microsoft Access, Windows XP,
Data Management
Oracle, SQL Server
Servers
Windows NT, Linux NetNote, Windows Server 2003
Career Development
Geek Trivia
Software/Web Development
Web Development Zone, Visual Basic, .NET
advertisement
Click Here