Consulting

Results 1 to 5 of 5

Thread: Multidimensional Array Initialization

  1. #1
    VBAX Newbie
    Joined
    May 2005
    Posts
    4
    Location

    Multidimensional Array Initialization

    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

  2. #2
    Knowledge Base Approver
    The King of Overkill!
    VBAX Master
    Joined
    Jul 2004
    Location
    Rochester, NY
    Posts
    1,727
    Location
    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

  3. #3
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Quote Originally Posted by skawky
    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 }]

  4. #4
    Knowledge Base Approver
    The King of Overkill! VBAX Master
    Joined
    Jul 2004
    Location
    Rochester, NY
    Posts
    1,727
    Location
    Good point xld, I forgot about the semi-colon

  5. #5
    VBAX Newbie
    Joined
    May 2005
    Posts
    4
    Location

    Very nice

    The semi-colon.. some c-link syntax.. i'm home!

    Thanks
    Skawky

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •