Results 1 to 8 of 8

Thread: Assigning values for multidimensional array

  1. #1

    Thread Starter
    Member
    Join Date
    May 2003
    Posts
    53

    Assigning values for multidimensional array

    Dim c, F

    F = Array(0#, 1#, 4#, 6#, 8#, 10#, 11#, 12#, 15#, 20#, 40#, 9999#)
    c = Array(3.8, 3.8, 3.8, 3.5, 3#, 1.6, 1.38, 1.18, 0.8, 0.7, 0.7, 0.7, -3.8, -3.8, -3.6, -2.7, -1.6, -0.95, -0.86, -0.8, -0.7, -0.7, -0.7, -0.7)

    Here F is simple array and c multidimensional array. Array F works but not c , How can I declare c(2,12) (a multidimensional array) and assign values in just one line as done in case of F.
    Last edited by bobbynad; Dec 29th, 2004 at 04:41 AM.
    Syed Nadeemulla Hussaini
    # I-2,C & FM Quater
    Hutti Gold Mines Co Ltd.
    Raichur Karnataka
    INDIA

  2. #2
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Assigning values for multidimensional array

    after you dimension it DIM c(4,1) (is 5x2)

    Code:
    c(0, 0) = 3.8
    c(0, 1) = 3.8
    c(1, 0) = 3.8
    c(1, 1) = 3.5

  3. #3

    Thread Starter
    Member
    Join Date
    May 2003
    Posts
    53

    Re: Assigning values for multidimensional array

    Hello thanks for the help

    I need to assign c (2,12) as done in case of F , i.e in just one line , is it possible
    Syed Nadeemulla Hussaini
    # I-2,C & FM Quater
    Hutti Gold Mines Co Ltd.
    Raichur Karnataka
    INDIA

  4. #4
    PowerPoster BruceG's Avatar
    Join Date
    May 2000
    Location
    New Jersey (USA)
    Posts
    2,657

    Re: Assigning values for multidimensional array

    It's not possible, as far as I know. The closest you can come to initializing an array in one statement (VB6 and earlier) is with the Array function as you have done, but the Array function makes your variable a one-dimensional array. Assigning the elements one-by-one as dglienna showed you is the only way to initialize a multi-dimensional array.
    "It's cold gin time again ..."

    Check out my website here.

  5. #5
    Frenzied Member David.Poundall's Avatar
    Join Date
    Sep 2002
    Location
    Robin Hood Land
    Posts
    1,457

    Re: Assigning values for multidimensional array

    If you want to hold the data linearly in a string use dlimiters, chop the string and load the slices into each field of the 2D array. Like this..

    Code:
    Dim a1, a2, a3
    Dim n as long, m as long
    Dim s1 As String
    s1 = "1,2,3,4|5,6,7,8|8,9,10,11"
    a1 = Split(s1, "|")
    Dim a3(0 To UBound(a1), 0)
    For n = 0 To UBound(a1)
         a2 = Split(a1(n), ",")
         If UBound(a2) > UBound(a3) Then ReDim Preserve a3(0 To UBound(a3, 1), 0 To UBound(a2))
         For m = 0 To UBound(a2)
              a3(n, m) = a2(m)
              Next m
         Next n
    a3 holds your finished 2D array.
    David

    Learn the Rules so that you know how to break them properly.

    Printing dll dBTools MZTools Winsock API WinsockVB More Winsock SGrid2 MSChart Mail2Web

    If you have found this thread useful then read this

  6. #6
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: Assigning values for multidimensional array

    You may create a dynamic two dimentional array:
    VB Code:
    1. Private Sub Command1_Click()
    2. Dim i%, j%, sValues$
    3. Dim arTest() As Integer
    4.  
    5.     i = 3
    6.     j = 1
    7.    
    8.     ReDim arTest(i + 1, j + 1)
    9.     For i = LBound(arTest, 1) To UBound(arTest, 1)
    10.         For j = LBound(arTest, 2) To UBound(arTest, 2)
    11.             arTest(i, j) = i + j
    12.         Next j
    13.     Next i
    14.    
    15.     For i = LBound(arTest, 1) To UBound(arTest, 1)
    16.         For j = LBound(arTest, 2) To UBound(arTest, 2)
    17.             sValues = sValues & arTest(i, j) & ";"
    18.         Next j
    19.         Debug.Print sValues
    20.         sValues = ""
    21.     Next i
    22.  
    23. End Sub

  7. #7
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: Assigning values for multidimensional array

    Here is another way of handling arrays: multidimensional array of arrays.
    VB Code:
    1. Private Sub Command2_Click()
    2. Dim i As Integer
    3. Dim arr1() As String
    4. Dim arr2() As String
    5. Dim arrTest(1)
    6. Dim sText1$, sText2$
    7.  
    8.     sText1 = "1;2;3;4;5"
    9.     sText2 = "A;B;C;D;E"
    10.    
    11.     arr1 = Split(sText1, ";")
    12.     arr2 = Split(sText2, ";")
    13.    
    14.     arrTest(0) = arr1()
    15.     arrTest(1) = arr2()
    16.    
    17.     Debug.Print arrTest(0)(2)
    18.     Debug.Print arrTest(1)(3)
    19.  
    20. End Sub

  8. #8
    Hyperactive Member
    Join Date
    Jun 2004
    Posts
    468

    Re: Assigning values for multidimensional array

    Not exactly a two-dimensional array, but close...
    VB Code:
    1. Dim c, F
    2.  
    3. F = Array(0#, 1#, 4#, 6#, 8#, 10#, 11#, 12#, 15#, 20#, 40#, 9999#)
    4. c = Array(Array(3.8, 3.8, 3.8, 3.5, 3#, 1.6, 1.38, 1.18, 0.8, 0.7, 0.7, 0.7), _
    5.           Array(-3.8, -3.8, -3.6, -2.7, -1.6, -0.95, -0.86, -0.8, -0.7, -0.7, -0.7, -0.7))
    6.  
    7. Debug.Print c(0)(0), c(0)(3), c(1)(0), c(1)(3)

Posting Permissions

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



Click Here to Expand Forum to Full Width