Click to See Complete Forum and Search --> : Can anyone please explain to me what's array and how to use it...Thank You


AndyK
November 29th, 1999, 04:31 PM
Thank You

Chris Eastwood
November 29th, 1999, 05:03 PM
You can think of an array as a list of values. The list can be of a pre-determined size or you can set its size dynamically.

Eg.

Say you wanted a list of 4 strings :



Dim sList(4) as string

sList(1) = "Chris"
sList(2) = "Was"
sList(3) = "Here"
sList(4) = "Late!"




You can now reference each element in the list using it's index, eg:



MsgBox sList(1)
'
' or
'
for l = 1 to 4
MsgBox sList(l)
next





The fun doesn't stop there though, you can have 'dynamic' arrays where you can change the size :


Dim sList() as string ' no predetermined size

Redim sList(4) ' make it hold 4 items (*)

sList(1) = "Chris"
sList(2) = "Was"
sList(3) = "Here"
sList(4) = "Late!"
'
' Resize it
'
Redim Preserve sList(6) ' make it 6 in length

sList(5) = "Drinking"
sList(6) = "Beer!"




You can get the size of the array using the UBound

keyword.

That's not all though, VB has the option Explicit

declaration and also a option Base x

statement where 'x' tells VB where to start it's array index from. This is set as '0' as a default so your array could start at 'sList(0)'.

To find the dimensions of your array in a loop, you could use :



for l = LBound(sList) to UBound(sList)
MsgBox sList(l)
next




.... And then of course you can have multi dimensional arrays......


Dim sList(2,2) as string

sList(1,1) = "Chris"
sList(1,2) = "Was
'
' etc....
'




Hope that's got you started.



Chris Eastwood

CodeGuru - the website for developers
http://codeguru.developer.com/vb