Outline

 Finding the array limits
 Split and Join
 

Your current position:
Home 
  Visual Basic
    Programming Basics
      Visual Basic Arrays

Visual Basic Arrays

First off, exactly what is an array? The English dictionary provides the following definition:
array

_n.
1  an imposing or well-ordered series or display.
2  an ordered arrangement, esp. of troops (battle array).
3  poet. an outfit or dress (in fine array).
4  a Math. an arrangement of quantities or symbols in rows and columns; a matrix. b Computing an ordered set of related elements.
5  Law a list of jurors empanelled.
_v.tr.
1  deck, adorn.
2  set in order; marshal (forces).
3  Law empanel (a jury).

What is the difference between a variable and a variable array?

A variable holds one bit of information under a name. A variable array holds more bits of information under a name.

How can I access the information stored in a array?
Array data is accessed through index. Example:

We will declare an array of variables: 

 Dim Countries(4) As String 
 Countries(0) = “Romania”
 Countries(1) = “USA”
 Countries(2) = “China”
 Countries(3) = “England”
 Countries(4) = “Russia”  

Obs: in Visual Basic arrays counting is zero-based. This means that if you declare an array of 2 you will have an array of three elements, and so on.

Now the variable Countries contains 5 country names. We will now access the third country from this array. 

 MsgBox Countries(2)

Why can’t I use more variables instead of arrays?
By using an numeric value instead of a variable name at run time you can do:

  • You can dynamically access data.
  • By using an resizable arrays you can store data provided by user.
  • You can use them in loops(for..next, do..loop)

Finding the limits of an array

Now, there are two very important functions that go hand in hand with arrays. These function are UBound and LBound (Upper boundary and Lower boundary).

Let’s return now to our Countries array.
How these two functions can help me use this array?

  • The UBound function returns the last index you can use in a array.
  • The LBound function returns the first index you can use in a array.

At a first look these function do not help very much. But imagine that you use an resizable array. You will find that is very hard too keep track of the last index of this array. Here is where these functions really help you.

 

Split and Join function

Obs: Split and Join functions are available only for Visual Basic version 6.

 

The Split function is used for breaking a string into an array. Example:

 

We have the following string:

“We are going to school”

we will split the first string using space as the splitter.

 

 Dim Words() As String

 Words = Split(“We are going to school”,” ”)

 debug.print Words(2)

 debug.print Words(3)  

This code splits the string in an array of words and prints the word 3 and 4 (the array is zero-based) in the imediate window of Visual Basic.

The Join function is just the “antonym” of Split function. This function does the exact opposite of split.

Example: We will now make form the “Words” array a single variable.

 Dim FullText As String
    FullText = Join(Words) 
 Debug.Print FullText