PDA

View Full Version : [SOLVED] Multidimensional Array Initialization



skawky
05-24-2005, 07:52 AM
I suppose the problem I have can be summarized as being that of initializing/assigning multidimensional arrays with distinct values.. jagged arrays also, (I mean not having rows of equal length)

There has to be something better than the mess i've produced below.

I was hoping for a method similar to:
arrayname={{#,#,#},{#,#,#,#,#},{#,#,#}}

but it seems that only works in VB .Net (I found a few articles); however, i'm working in VB6.. This is how my code looks right now :(

'This is REALLY ugly, but couldn't find any other way to do it in VBA! ARGH!
If weightType = 0 Then 'ANO/Unit 0 Op.
weights(1, 1) = 5
weights(1, 2) = 4
weights(1, 3) = 4
weights(1, 4) = 5
weights(1, 5) = 3
weights(1, 6) = 3
weights(1, 7) = 1
weights(2, 1) = 2
weights(2, 2) = 5
weights(2, 3) = 3
weights(2, 4) = 5
weights(2, 5) = 3
weights(2, 6) = 4
weights(2, 7) = 4
weights(2, 8) = 5
weights(3, 1) = 5
weights(3, 2) = 4
weights(3, 3) = 5
weights(3, 4) = 5
weights(3, 5) = 3
weights(3, 6) = 2
weights(3, 7) = 2
weights(3, 8) = 5
weights(3, 9) = 4
weights(3, 10) = 3
weights(4, 1) = 3
weights(4, 2) = 4
weights(4, 3) = 5
weights(5, 1) = 5
weights(5, 2) = 3
weights(5, 3) = 5
weights(5, 4) = 2
weights(5, 5) = 3
weights(5, 6) = 3
Else 'Shift Manager
weights(1, 1) = 5
weights(1, 2) = 4
weights(1, 3) = 4
weights(1, 4) = 5
weights(1, 5) = 3
weights(1, 6) = 5
weights(2, 1) = 2
weights(2, 2) = 5
weights(2, 3) = 5
weights(2, 4) = 3
weights(2, 5) = 4
weights(2, 6) = 4
weights(2, 7) = 4
weights(2, 8) = 5
weights(3, 1) = 5
weights(3, 2) = 4
weights(3, 3) = 5
weights(3, 4) = 5
weights(3, 5) = 3
weights(3, 6) = 2
weights(3, 7) = 2
weights(3, 8) = 5
weights(3, 9) = 4
weights(3, 10) = 3
weights(3, 11) = 5
weights(3, 12) = 4
weights(3, 13) = 5
weights(4, 1) = 3
weights(4, 2) = 5
weights(4, 3) = 3
weights(5, 1) = 5
weights(5, 2) = 3
weights(5, 3) = 5
weights(5, 4) = 3
weights(5, 5) = 2
weights(5, 6) = 3
End If

thank you for your help,
Skawky

mvidas
05-24-2005, 08:04 AM
Hi Skawky,

This probably isn't exactly as you were looking for, but if you use:

If weighttype = 0 Then 'ANO/Unit 0 Op.
weights = Array(Array(5, 4, 4, 5, 3, 3, 1), Array(2, 5, 3, 5, 3, 4, 4, 5), _
Array(5, 4, 5, 5, 3, 2, 2, 5, 4, 3), Array(3, 4, 5), Array(5, 3, 5, 2, 3, 3))
Else 'Shift Manager
weights = Array(Array(5, 4, 4, 5, 3, 5), Array(2, 5, 5, 3, 4, 4, 4, 5), Array( _
5, 4, 5, 5, 3, 2, 2, 5, 4, 3, 5, 4, 5), Array(3, 5, 3), Array(5, 3, 5, 3, 2, 3))
End If

This will create an array of arrays. Instead of using weights(3, 1) you would use weights(3)(1) though this is better if you're looking for the jagged arrays.

Otherwise I think you're stuck the way you have it!
Matt

ps. You can enclose your code (like above) with [ vba ] (no spaces) before, and [ /vba ] (no spaces) after your code, to get it formatted as I have mine above. Makes for easier reading!
Matt

Bob Phillips
05-24-2005, 08:07 AM
I suppose the problem I have can be summarized as being that of initializing/assigning multidimensional arrays with distinct

Strange, already used this today


ary = [{"Bob","M", 123;"Lynne","F",898;"Amy","F",543}]

so you would use


weights = [{5,4,4,5,3,1;1,2,5,3,5,4,4l.... etc }]

mvidas
05-24-2005, 08:14 AM
Good point xld, I forgot about the semi-colon :)

skawky
05-24-2005, 08:17 AM
The semi-colon.. some c-link syntax.. i'm home! :D

Thanks
Skawky