

The data type cannot be changed from its initial declaration when using the ReDim keyword. (Numeric data types to 0, variable-length strings to "" (a zero-length string), fixed-length strings filled with zeros, and variants to empty.) Without it all elements will return to their default values. Preserve is an optional keyword that forces Visual Basic to retain all existing elements' values.ReDim is the keyword that denotes we are redimensioning an array.Without this keyword all your old CDs would be lost and you'd be stuck with just Charlotte Church. Notice the Preserve keyword: it forces Visual Basic to retain all existing elements' values. This snippet has two more lines, the first redimensioning the array one element larger and the second setting this element's value. ReDim Preserve strCDRack(0 to 3) As String Now that you've rebuilt the structure of your CD rack, allowing for expansion, it is time to expand: This is OK it tells VB that your array is a dynamic array, meaning that you can change its size with ReDim. Notice that the subscript of the Dim statement is missing. What have you done? Nothing wrong, you've just dimensioned your array another way that allows for expansion. You don't want to throw away any of your CDs to make room for the new one so you decide to use your ultimate building skills to attach another slot. The new Charlotte Church CD came out but your rack only has three slots. Subscript is the dimensions of the array.Using Public will make the array available throughout the project. Private has the same effect and should be used only at the modular level. Using it in the General Declarations section will make it available to all procedures in that module. Using Dim in a procedure will make the array only available from within that procedure. Dim, Public, and Private declare the array and its scope.This is like numbering the slots on your CD rack starting at 0 up to 2 and then inserting a CD into each slot.ĭim|Public|Private ArrayName( Subscript) As DataType Notice that each of the three new lines starts off with the variable name and then gives an element number before having a value assigned. You've just made a variable 'strCDRack' that contains three slots (#0, #1, and #2) and is of a String data type.

So you declare your CD rack as having three slots: To fit all of these in your rack, the rack must contain at least three slots. Pretend you have three CDs - one by the Deftones, another by Tool, and a third by Disturbed. There are a limited number of slots you can slide CDs into. There are two types of Visual Basic arrays: fixed-size and dynamic.Ī fixed-size array most closely matches our CD rack anology. You know: one of those rectangular boxes with slots to slide CDs in, each above another. If you aren't able to find what you need here, you can check out theĪn array is a lot like a CD rack. This tutorial applies to all versions of Visual Basic, however, versions before Visualīasic 6.0 do not include the split and join function.īelow is a list of the major topics that will be discussed.
#VISUAL BASIC TUTORIAL HOW TO#
In this tutorial you will learn the differences between a fixed-size and dynamic array, how to properly declareĮach one, how to access them, how to loop through them, how to erase them, and a few other things. Visual Basic Array Tutorial By Adam Wehmann
