Question on VB with array

  • Thread starter Thread starter mike
  • Start date Start date
M

mike

Hi all,

How do I create an Array that holds 5 values. From the
created Array, how do I assign the values to the array.

For example:

I have 5 values {1,2,3,4,5}

I need to created an array that holds the 5 values with
array member of 0 is assign to 1, and array member 0f 1 is
assing to 2 and so on. With the For loop?

Thanks for the help.

mike
 
I have 5 values {1,2,3,4,5}

I need to created an array that holds the 5 values with
array member of 0 is assign to 1, and array member 0f 1 is
assing to 2 and so on. With the For loop?

A two minute silence here for the DATA statement -- whatever happened to it
anyway?

The easiest way here is to use the Array() function, but this only works
using an array-in-a-variant:

Public Sub TestArray()

Dim a As Variant

Let a = Array(1, 2, 3, 4)
Debug.Print a(2)

End Sub

If you have to use a real Array structure, then you must assign each member
separately:

Dim a(4)
a(0) = 1
a(1) = 2
a(2) = 3
a(3) = 4
a(4) = 5

Hope that helps


Tim F
 
"Tim Ferguson" replied:
A two minute silence here for the DATA statement -- whatever happened to it
anyway?

LOL! What a READ!

Tim, your antiquity is showing. :-)
 
Ahhh, thank you Allen.

As requested, I was silent for two minutes but didnt really understand why
....
Actually, I still dont fully understand. What is the DATA statement?



Immanuel Sibero
 
Long ago, at the birth of the computer age, data was stored on magnetic tape
or even punched paper. In that era - forgotten by all but the most aged
individuals - there existed a quaint language called BASIC: Beginner's
All-purpose Symbolic Instruction Code. The VBA we use in Access (Visual
Basic for Applications) descended from this dinasour.

To store your data *in* the software itself, BASIC included a DATA
statement. Typically these were placed at the end of the program, on
numbered lines, e.g.:
30000 DATA 7, 8, 2, 22, ...
Further up in the program you would READ each value of the data you had to
work with, to get a total or whatever you needed to do.

These were the days before libraries and recordsets, before Get and Put,
before LCD screens or even CRT screens. Anyone who remembers such things has
wasted too much of their life sitting at a terminal. :-)
 
To store your data *in* the software itself, BASIC included a DATA
statement. Typically these were placed at the end of the program, on
numbered lines, e.g.:
30000 DATA 7, 8, 2, 22, ...
Further up in the program you would READ each value of the data you
had to work with, to get a total or whatever you needed to do.

The point is that the values were easily accessible as well as static. You
could run a program with one set of parameters, quickly change the DATA
statements and rerun it with another.

There just is no way of doing that now. CONST statements do roughly the
same thing, but inevitably end up being buried in one of several modules,
and in any case the simple

Data 1,2,3,4
For n = 0 to 4
Read aryTest(n)
Next n

turns into

Const c_wFirst = 1
Const c_wSecond = 2
Const c_wThird = 3
Const c_wFourth = 4
Const c_wFifth = 5

aryTest(0) = c_wFirst
aryTest(1) = c_wSecond
aryTest(2) = c_wThird
aryTest(3) = c_wFourth
aryTest(4) = c_wFifth


Yueuch! It would not be so bad if you could use Const with arrays, like
other languages:

Const a_wTest() = {0,1,2,3,4}

but it doesn't exist. So we have to bury important constants deep within
executing code, which is A Bad Thing. Think of this:

Dim va_wMonthLens As Variant

va_wMonthLens = Array(31,28,31,30,31,30,30,31,30,31,30,31)

now that is one hard bug to figure out later! This might be in the middle
of hundreds of lines of code. And it should be avoidable.

Best wishes


Tim "I remember Telcomp 2 and I'm proud of it" F
 
Back
Top