The Array in Visual Basic and How to Use It
Auto Beauty Business Culture Dieting DIY Events Fashion Finance Food Freelancing Gardening Health Hobbies Home Internet Jobs Law Local Media Men's Health Mobile Nutrition Parenting Pets Pregnancy Products Psychology Real Estate Relationships Science Seniors Sports Technology Travel Wellness Women's Health

The Array in Visual Basic and How to Use It

An array is a set of related pieces of data or values that is indexed. This articel describes how to write code that uses arrays in Visual Basic

Arrays are used by programmers to refer and to recall different values of the same class of data (in the example below I’ll be using countries, but it could be, names, addresses, test results or any other piece of information). They are basically lists, each item or row on the list is given a unique number called an index (or a subscript – depending on the programmer and the books they read). Arrays simplify the code needed, making it easier for a programmer to write a loop that can work powerfully with large numbers of elements. It is possible to not use arrays, and to declare each element as a variable, but the amount of coding this would entail would slow down a program immensely.

An array is a set of related pieces of data or values that is indexed; they are used to group and index to a series of values for recall within a program. Each of the values contained by an array is called an element.

An element is one piece of data, as a computer has no concept of what countries are, or the words we use to label, them a coder has to tell the computer that there will be a table or list of words referred to as “countries”. Once the space for the countries has been allowed for the code writer can then tell computer that England, Wales, Greece, Gorge and Qatar are all countries, and the countries are indexed or subscripted so that country 1 is England, country 2 is Wales and so on.

Types of Array

Visual Basic Arrays can be dynamic or fixed-sized. A fixed-sized array will always have the same number of elements during the execution of the program. It’s useful for static lists like the months in a year, or any other list that will be unchanging. Dynamic arrays can change during run time and use information imported from a file or manually entered while the program executes.

Arrays are also linear or multidimensional, so there are fixed-sized linear arrays, fixed-sized multidimensional arrays, and dynamic linear or dynamic multidimensional arrays. Linear arrays contain a single list of variables, such as the countries above. A multidimensional array uses more than one set of indexes or subscripts, 2-dimentional arrays can be thought of as a table with columns and rows. This could hold the countries name in one column, the phone code in a second, and a postal abbreviation a third column.

Array Declaration Statements

Arrays are declared in programs the same way as a variable is, they need a declaration statement. As with variables the place in the code, and the way they are declared will affect the scope that an array can be used in. To declare an array 4 pieces of information are needed; the name of the array, what data type it is, the number of dimensions in the array, and the number of elements.

A fixed-size array, storing the Countries England, Wales, Greece and Qatar could be declared:

      Dim strCountries(4) As String

The “Dim” declares that there will be an array with the title “strCountries” and this array can hold 5 Countries. Visual Basic counts the index starting from 0, the spaces allocated by the programmer need to be minus 1 to allow for that. One number is in the parentheses so this is a one-dimensional or linear array.

Next the countries are indexed (or given the subscripts) 0 to 4.

      strCountries(0) = "England"

      strCountries(1) = "Wales"

      strCountries(2) = "Greece" and so on.

An array index can’t be any longer than a Long Data Type, but the long data type has a value range of -9,223,372,036,854,775,808 through to 9,223,372,036,854,775,807 (9.2...E+18). There's probably enough wriggle room there for most projects.

To declare an array that two-dimensional array the parentheses would hold a second number split by a comma:

      Dim strCountries(4,1) As String

This snippet declares an array called strCountries which could contain 5 countries and their 5 corresponding dialling codes; 5 rows, with two columns in each row.

Example of a Dynamic Array

Code to declare the linear array dynamically would read

      Dim strCountries() As String

Leaving the parentheses empty creates a dynamic array, but it can’t contain any data because the computer hasn’t been told to keep any memory space for the elements.

The array needs to be redimensioned – redimensioning tells the computer to allocate a certain amount of memory space. To redimension the strCountries array to hold 5 Countries would read:

      ReDim strCountries(4)

If a further country is added while the program is running more space will be needed. The ReDim command alone would write over the data already being stored so to add a 6th country (Ireland in this example) the programmer would use the “Preserve” command.

      ReDim Preserve strCountries(5)

      strCountries(5) = “Ireland”

Multidimensional Array Usage

Arrays in VB can have up to 32 dimensions, but it is unusual for code writers to use more than 3 (very useful for producing 3D images) or 4 (for calcuations using movement through space and time). More than four… well, then we’re into the field of mathematicians much cleverer than myself…

Looking for programming tools? Save when shopping online by using the latet programming tools coupons and pay less at popular retailers and brands. Find deals for popular programming tools stores using community-sourced coupons and deals.
Need an answer?
Get insightful answers from community-recommended
in Computer Programming & Languages on Knoji.
Would you recommend this author as an expert in Computer Programming & Languages?
You have 0 recommendations remaining to grant today.
Comments (5)

Very informative and useful write! Sorry I'm out of votes for today but will defiantely buzz this up!

very informative

I have encountered these terms when I tried to study Visual Basic. This certainly helped me understand better.

An array is a lot like a CD rack. You know: one of those rectangular boxes with slots to slide CDs in, each above another. There are two types of Visual Basic arrays: fixed-size and dynamic. You declare an array variable the same way as any other variable, by using the Dim statement. You follow the variable name with one or more pairs of parentheses to indicate that it is to hold an array rather than a scalar (a variable that contains a single value). The format for declaring an array is: Dim|Public|Private ArrayName(Subscript) As DataType

Interesting! I think i am not smart enough to learn programing in general. :/