PDA

View Full Version : Two dimensional arrays



krishnak
12-07-2007, 10:29 AM
Hi All,

I am working on a script, where I want to hold two sets of data in an array or any other form. The first one is a variable integer and the second one is a string. Can I use a two-dimensional array? How do I declare and assign the variables to each element?
I am now using two one-dimensional arrays.

- Krishna

figment
12-07-2007, 11:12 AM
you would declare a two dimentional array like this

dim a(0 to 1,0 to 65) as String

but it can only have one varible type. so if you need two diffent types, then you best bet is to use two arrays.

storing values to your array is dont like

a(0,44)="b"

retreaving data would be

C = a(0,44)

you can go as many dimentions as you need.

Bob Phillips
12-07-2007, 11:39 AM
but it can only have one varible type. so if you need two diffent types, then you best bet is to use two arrays.

That is not so



Sub Load2DArray()
Dim myArray(1 To 2, 1 To 2)
Dim i As Long

myArray(1, 1) = "Bob"
myArray(1, 2) = 300
myArray(2, 1) = "figment"
myArray(2, 2) = 500

For i = 1 To 2
MsgBox myArray(i, 1) & " - " & myArray(i, 2)
Next i

End Sub

Norie
12-07-2007, 11:40 AM
Krishna

Declare the array as Variant then it can hold both numbers and strings.

Even if you declared it as String then you could still work with numbers.

They would be stored in the array as strings so if you wanted to perform any calculations you would need to convert them to numbers.

A little more work but perfectly feasible - VBA has various conversion functions. eg Val, CInt, CDbl etc

krishnak
12-11-2007, 08:49 AM
Hi All,

Thanks for all the suggestions. I am now working with two arrays but I'll give it a try with a single array declared as variant.

- Krishna