Search Tools Links Login

Arrays Again

Posted: 2002-06-01
By: ArchiveBot
Viewed: 79

Filed Under:

VB6 Code Cache

No attachments for this post


Clearing out the cupboards, here a tut on Arrays.

Original Author: Tony G

Code


Visual Basic Tutorial - Arrays


Visual
Basic Tutorial:
color=#0000a0>
Form and App
Objects
color=#008000>
By: href="mailto:metadrummer@hotmail.com">Robert Klieger




Table of
Contents


  • face=Verdana color=#0000a0 size=3>Declaring Arrays
  • face=Verdana color=#000080 size=3>Accessing Elements of an Array for
    Input/Output

  • face=Verdana color=#000080 size=3>Getting the Size of an Array
  • face=Verdana color=#000080 size=3>Multidimensional Arrays
  • face=Verdana color=#000080 size=3>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:


Dimsize=3> ArrayName(LowerValue To HigherValue) size=3>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



face=Arial size=4>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 color=#000080>As String
Dim Name2 color=#000080>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.




face=Arial size=4>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 href="http://www.geocities.com/alpha_productions2/vb_tutorial5.htm#Dynaming">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 href="http://www.geocities.com/alpha_productions2/vb_tutorial5.htm#Mulitidimen">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 = color=#000080>LBound(Names) color=#008000>'Get the lower boundry number.
UpperVal = color=#000080>UBound(Names)color=#008000> 'Get the upper boundry number.

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

color=#000080>Next



face=Arial size=4>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

   face=Verdana>iArray(1,1)   face=Verdana>iArray(1,2)   face=Verdana>iArray(1,3)
2   face=Verdana>iArray(2,1)   face=Verdana>iArray(2,2)   face=Verdana>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.




color=#000000 size=4>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:
face=Verdana>


Dimface="Courier New"> ArrayName() color=#000080>As color=#000080>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:


color=#000080>ReDim ArrayName(LowerValue color=#000080>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() color=#000080>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.
face=Verdana>


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 color=#000080>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.



Comments on this post

No comments have been added for this post.

You must be logged in to make a comment.