article

Arrays Again

Email
Submitted on: 1/24/2015 3:48:00 AM
By: TonyGG (from psc cd)  
Level: Beginner
User Rating: By 7 Users
Compatibility: VB 6.0
Views: 487
 
     Clearing out the digital cupboards, here is a tutorial on Arrays.

 
				
Visual Basic Tutorial - Arrays


Visual Basic Tutorial:
Form and App Objects
By: Robert Klieger


Table of Contents

  • Declaring Arrays
  • Accessing Elements of an Array for Input/Output
  • Getting the Size of an Array
  • Multidimensional Arrays
  • Dynamic Arrays

Declaring Arrays

When declaring an Array in VB, you are telling it three things. First, the name of the array. This is what you will use to access the array later in your program. Second, how many elements it can store, this is inside the parenthesis. And last, the data type, Integer, String, etc... If you declare the array in the general declarations section of a form, the array is visible to all procedures and functions in that form. If you declare the array as Public in a Module it will be visible to every procedure and function in your project. To declare an array use the following syntax:

Dim ArrayName(LowerValue To HigherValue) As DataType

If you declare an array in a Form, use Dim or Private, if you declare it in a Module and you want every procedure to access it, declare it as Public. If you declare the array in a procedure use Dim. ArrayName is the name of the array. LowerValue is the first element in the array. HigherValue is the last element in the array. DataType is a valid datatype (ie String)/ You don't have to specify the LowerValue, but if you don't, the array will start from 0. For example,

Dim sTestArray(0 To 10) As String

is the same as...

Dim sTestArray(10) As String

The code below declares nArray, with 1 as the first element, 30 as the last element, all of which stores Integers.

Dim nArray(1 To 30) As Integer


Reading/Writing to Arrays

Assigning a value to an element of an array is as easy as filling the value of a normal integer or string. Here's an example of the syntax:

ArrayName(Element) = Value

Reading from an array is just as easy. You can use it in several different ways. Here are a few examples:

iArray(3) = iArray(7) 'Takes whatever is in element (3) and copys it into (7)
Msgbox iArray(5) 'Takes whatever is in element (5) and puts it in a message box

So what data types can you use with an array? With Visual Basic, you can use any data type you could use in a regular variable. This means Boolean values will work to. With many previous programing vanguages, this is not possible. Just one more reason I perfer using VB for many tasks.

One great thing about arrays in any programing language, and this was the whole idea behind them, is that you only need to specify one name for many values. Each value then has an index number assigned to it, or more commonly know, an element number. Say you wanted to collect three names. You could either define three variables:

Dim Name1 As String
Dim Name2 As String
Dim
Name3 As String

The problem with this is, you would have to write three diffrent codes to access them. If you wanted to do same task for each name, you would probley have to use a for statement and a select case statement. Imagine if you had 500 names! Writing that much code would be a pain in the butt. Plus you would have to declare that many strings. You are limited to what you declare. For example:

Msgbox (Name & iNum)

If you were atempting to access for Name1, and iNum = 1, using Name & iNum would not work. This would simply show whatever was in Name followed by whatever iNum equaled. With using an Array, you can simply do this:

Msgbox Names(iNum)

This would access the element of Names equaled to iNum.


Getting the Size of an Array

By refering to size, we mean the upper and lower limits of an array in terms of elements. Anotherwords, how many variables are in an array. This is simply done using the LBound and UBound functions (upper & lower boundrys). Why would you want to know this? If you wanted to fill a list box with all the names in an array but are not sure how many names their are. You would use these functions. This is especially useful if you have used the ReDim statment (see dynamic arrays). Here is the syntax for finding the upper boundries.

UBound(ArrayName, Dimension) 'ArrayName = the name of the array

Dimension is an optional integer representing the dimension number for use when using multidimensional arrays found towards the end of this tutorial. To find the lower boundries of an array use the LBound function syntax:

LBound(ArrayName, Dimension)

Pretty simple, huh? Heres an example that fills a list box with the values of all the items in Names.

LowerVal = LBound(Names) 'Get the lower boundry number.
UpperVal = UBound(Names) 'Get the upper boundry number.

For i = LowerVal To UpperVal
List1.AddItem Names(i) 'Add each name from array according to how many stored in the Array
Next


Multidimensional Arrays

Now that you know what an array is, figuring out what a multidimensional array is will be a little easier. A multidimensional array is an array that looks like a table. If you have ever used Microsoft Excel, you would know what I'm talking about. It can also be refered to an array of arrays. That is many arrays using the same name. Here is an example of a two-dimensional array.

Static iArray(1 To 2, 1 To 3)

All the elements list out are:
iArray(1,1), iArray(1,2), iArray(1,3), iArray(2,1), iArray(2,2), iArray(2,3)
If it were in a table it would look like this:

1

2

3

1

iArray(1,1) iArray(1,2) iArray(1,3)
2 iArray(2,1) iArray(2,2) iArray(2,3)

Most of the time you will be using one dimensional arrays but some of the time it will be helpful to use 2 or even 3. More then 3 is not always a good idea because it tends to be hard to debug. The usefulness of VB will allow you to use as many as you want, each seperated by commas. Keep in mind the more you use, the more computer memory you use up. This can get real dangerous real fast. If you have some free time someday go ahead and make and 14-15 dimensional array. Make sure you close out and save everything else before hand. See how well your computer runs. Be prepared to have to restart.


Dynamic Arrays

Arrays because of their uniqeness can consum alot of memory if your not carefull. For example:

Dim MyArray (10000) As Long

Will require 40,004 bytes of memory. That is 10 x 10,001 because long variables take up 4 bytes of memory. This might not be alot now, but if you start using 10 of these array, they consume 4,000,400 bytes. This is why most of the time it is wise you set large arrays to minimum at the start, and later resize them during runtime. Yes, you can do this! This can be done using the ReDim function. Arrays that change size during runtime are called dynamic arrays. Its that simple.

When you declare a dynamic array, you don't need to declare it like a fixed array. When you declare a dynamic array the size is not specified. Instead you use the following syntax:

Dim ArrayName() As DataType

Every thing here is the same as before, Dim the array (can set to Global if using in a module, or public if in procedure), ArrayName is the name, and DataType is as said before. Heres how to use the ReDim function:

ReDim ArrayName(LowerValue To HigherValue)

You can use this either in a procedure or function. Its almost like when you declare it except you use ReDim, don't use a datatype, and are not declareing it. That is, it must already be declared. Here is an example of how to do this:

Dim Names() As String

Sub Form1_Load()
ReDim Names(1 To 10)
End Sub

This code assigns 10 elements to Names when Form1 loads. Note: When using dynamic arrays, you must set the size of an array using the ReDim statement, before filling the array.

However, when using the ReDim statement, any values already in the array (if it has been resized previously), will be deleted. In some cases, this is not what you would want! So, you use the Preserve keyword:

ReDim Preserve ArrayName(LowerValue To HigherValue)

If the array has grown, there will be a number of blank array spaces at the end of the array. If the array has shrunk, you will lose the end items.


Thanks for spending the time to read this Visual Basic Tutorial, since all of these tutorials were hand-written, there might possibly be some errors in our technical accuracy, if you have any comments or find one of the errors, please contact us at admin@alphavb.com. We stand by our tutorials and will help you the best we can with any Visual Basic questions also.


Other 4 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this article (in the Beginner category)?
(The article with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular article, please click here instead.)
 

To post feedback, first please login.