Arrays Again
Posted: 2002-06-01
By: ArchiveBot
Viewed: 79
Filed Under:
No attachments for this post
Clearing out the cupboards, here a tut on Arrays.
Original Author: Tony G
Code
Visual Table of When declaring an Array in VB, you are telling it Dimsize=3> ArrayName(LowerValue To HigherValue) size=3>As DataType If you declare an array in a Form, use Dim or Dim sTestArray(0 To is the same as... Dim sTestArray(10) The code below declares nArray, with 1 as the first Dim nArray(1 To 30) Assigning a value to an element of an array is as ArrayName(Element) = Value Reading from an array is just as easy. You can use iArray(3) = iArray(7) 'Takes So what data types can you use with an array? With One great thing about arrays in any programing Dim Name1 color=#000080>As String The problem with this is, you would have to write If you were atempting to access for Name1, and iNum Msgbox Names(iNum) By refering to size, we mean the upper and lower UBound(ArrayName, Dimension is an optional integer representing the LBound(ArrayName, Pretty simple, huh? Heres an example that fills a LowerVal = color=#000080>LBound(Names) color=#008000>'Get the lower boundry number. Now that you know what an array is, figuring out All the elements list out are: 1 2 3 1 Most of the time you will be using one dimensional Arrays because of their uniqeness can consum Dim MyArray (10000) Will require 40,004 bytes of memory. That is 10 x 10,001 When you declare a dynamic array, you don't Dimface="Courier New"> ArrayName() color=#000080>As color=#000080>DataType Every thing here is the same as before, Dim color=#000080>ReDim ArrayName(LowerValue color=#000080>To HigherValue) You can use this either in a procedure or Dim Names() color=#000080>As String This code assigns 10 elements to Names when However, when using the ReDim statement, any ReDim color=#000080>Preserve ArrayName(LowerValue To HigherValue) If the array has grown, there will be a Thanks for spending the time to read this Visual Basic Tutorial, since all of
Basic Tutorial:color=#0000a0>
Form and App
Objectscolor=#008000>
By: href="mailto:metadrummer@hotmail.com">Robert Klieger
Contents
Input/Output
Declaring Arrays
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:
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,
10) As String
As String
element, 30 as the last element, all of which stores Integers.
As Integerface=Arial size=4>Reading/Writing to Arrays
easy as filling the value of a normal integer or string. Here's an example of
the syntax:
it in several different ways. Here are a few examples:
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
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.
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 Name2 color=#000080>As String
Dim Name3 As
String
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)
= 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:This would access the element of Names
equaled to iNum.face=Arial size=4>Getting the Size of an Array
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.
Dimension) 'ArrayName = the name
of the array
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:
Dimension)
list box with the values of all the items in Names.
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>Nextface=Arial size=4>Multidimensional Arrays
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)
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:
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)
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
alot of memory if your not carefull. For example:
As
Long
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.
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>
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:
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:
Sub
Form1_Load()
ReDim Names(1 To
10)
End Sub
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>
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:
number of blank array spaces at the end of the array. If the array has shrunk,
you will lose the end items.
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.