Results 1 to 13 of 13

Thread: How to initialize a two-dimensional Array

  1. #1

    Thread Starter
    New Member
    Join Date
    Mar 2006
    Posts
    12

    How to initialize a two-dimensional Array

    Hello Everybody,

    We can initialize a one dimensional array in VB 6.0 as shown below

    ---------------------
    Dim Data(), N As Integer

    Private Sub Combo1_Click()
    Text1.Text = Combo1.Text
    End Sub

    Private Sub Form_Load()
    Data = Array("Abc", "Pqr", "Uvw", "Xyz")
    For N = 0 To UBound(Data)
    Combo1.AddItem Data(N)
    Next
    'This will initially display the first page
    Combo1.ListIndex = 0
    End Sub
    --------------------

    But Similarly how to initialize a two-dimensional Array. Following line is coming out of an error.

    Dim Data1()

    Data1(1 To 2, 1 To 3) = ((“EVA”, “MSA”), (“RA”, “MA8000”), (“Xyz”, “Pqr”))

    I have huge data like this to be assigned to a two-dimensional array:

    EVA 72GB 11111-111
    MSA 36GB 22222-222
    MSA 36GB 33333-333
    MSA 72GB 44444-444
    RA 18GB 55555-555
    RA 36GB 66666-666
    And so on…..

    Thanks in Advance !!

    Regards
    Susanta

  2. #2
    Frenzied Member d3gerald's Avatar
    Join Date
    Jan 2006
    Posts
    1,348

    Re: How to initialize a two-dimensional Array

    you may do it like this

    VB Code:
    1. dim arrStr(5,5) as string
    On error goto Trap

    Trap:
    in case of emergency, drop the case...

    ****************************************
    If this post has been resolved. Please mark it as "Resolved" by going through the "Thread Tools" above and clicking on the "Mark Thread Resolved " option.
    if a post is helpful to you, Please Rate it by clicking on the Rate link right below the avatar

  3. #3

    Thread Starter
    New Member
    Join Date
    Mar 2006
    Posts
    12

    Re: How to initialize a two-dimensional Array

    I tried following

    Dim data1(5, 5) As String
    - - -
    data1 = Array(("EVA","MSA1000"), ("RA", "MSA30", "33333-333"))

    Still I am getting "Compile error: Expected: )" error on 2nd line
    and it takes the cursor to the first comma(,) in the line.

    Regards
    Susanta

  4. #4
    Frenzied Member d3gerald's Avatar
    Join Date
    Jan 2006
    Posts
    1,348

    Re: How to initialize a two-dimensional Array

    you can do this
    VB Code:
    1. dim data1(5, 5) as string
    2. data1(0, 0) = "EVA"
    3. data1(0, 1) = "MSA1000"
    On error goto Trap

    Trap:
    in case of emergency, drop the case...

    ****************************************
    If this post has been resolved. Please mark it as "Resolved" by going through the "Thread Tools" above and clicking on the "Mark Thread Resolved " option.
    if a post is helpful to you, Please Rate it by clicking on the Rate link right below the avatar

  5. #5
    Conquistador
    Join Date
    Dec 1999
    Location
    Australia
    Posts
    4,527

    Re: How to initialize a two-dimensional Array

    data1 = Array(Array("EVA","MSA1000"), Array("RA", "MSA30", "33333-333"))

  6. #6

    Thread Starter
    New Member
    Join Date
    Mar 2006
    Posts
    12

    Re: How to initialize a two-dimensional Array

    It may be difficult to do this for hundreds of data, Is it not possible to define something like Data = Array("Abc", "Pqr", "Uvw", "Xyz") the way we do for one dimensional array.

    Planning to create EXE file which should have list of parts( in the range of hundreds )incorporated in it – We would use the executable file to apply some logic to find the correct part.

    Since I am unable to initialize the two dimensional array with the data, Is there any other way to do this.

    Regards
    Susanta
    Last edited by susanta_dutta; Mar 22nd, 2006 at 04:09 AM.

  7. #7
    Conquistador
    Join Date
    Dec 1999
    Location
    Australia
    Posts
    4,527

    Re: How to initialize a two-dimensional Array

    read in the data and populate it

    for i = 0 to rowcount
    for j = 0 to colcount
    somearray(i, j) = <some value>
    next
    next

    ?

  8. #8

    Thread Starter
    New Member
    Join Date
    Mar 2006
    Posts
    12

    Re: How to initialize a two-dimensional Array

    Values are “String” variable and can’t be derived from any counter ( I, j). It may not be possible to feed each and every element using “somearray(i, j) = <some value>”

    Can you please suggest any other alternate way.

    Regards
    Susanta

  9. #9
    Conquistador
    Join Date
    Dec 1999
    Location
    Australia
    Posts
    4,527

    Re: How to initialize a two-dimensional Array

    i,j is the position in the two dimensional array, not the value that it's initialized too

    basically inside that loop, you start reading in your data, and it will store it in array(0,0) array(0,1) etc

    somearray(i,j) = "some string" for example...

    i don't see what the problem is

  10. #10

    Thread Starter
    New Member
    Join Date
    Mar 2006
    Posts
    12

    Re: How to initialize a two-dimensional Array

    What I am trying to say..my data looks something like:

    EVA 72GB 11111-111
    MSA 36GB 22222-222
    MSA 36GB 33333-333
    MSA 72GB 44444-444
    RA 18GB 55555-555
    RA 36GB 66666-666
    And so on…..( There is no pattern )

    There are hundreds of data like this. Could you please help me, how to put the data for each element in the array. If “I” is horizontal counter and “j” is vertical, what exactly needs to written at the place of “some string” in your above post.

    Regards
    Susanta

  11. #11
    Conquistador
    Join Date
    Dec 1999
    Location
    Australia
    Posts
    4,527

    Re: How to initialize a two-dimensional Array

    VB Code:
    1. Sub test()
    2. Dim strInput As String, i As Integer
    3. Dim strArray(100, 3) As String
    4. Dim fPos As Integer, sPos As Integer
    5.     Open "C:\test.txt" For Input As #1
    6.     i = 0
    7.     While Not EOF(1)
    8.         Line Input #1, strInput
    9.         fPos = InStr(1, strInput, " ")
    10.         sPos = InStr(fPos + 1, strInput, " ")
    11.         strArray(i, 0) = Left(strInput, fPos)
    12.         strArray(i, 1) = Mid(strInput, fPos, sPos - fPos)
    13.         strArray(i, 2) = Right(strInput, Len(strInput) - sPos)
    14.         Debug.Print strArray(i, 0), strArray(i, 1), strArray(i, 2)
    15.         i = i + 1
    16.     Wend
    17.     Close #1
    18. End Sub

    ?

  12. #12

    Thread Starter
    New Member
    Join Date
    Mar 2006
    Posts
    12

    Re: How to initialize a two-dimensional Array

    Hi da_silvy,

    Thanks for the idea. Now I am getting the result what I expected.

    One final small query. Instead of reading data from a separate file , just to embed it inside the executable file, created one dimensional array as below

    StrArray = Array("EVA 72GB 11111 222", "MSA 36GB 22222 333", "RA 36GB 33333 444" , and so on)

    And then separating the three strings out from each element in the Array. I get an error when I put data like this:

    Data = Array(
    "EVA 72GB 11111 222",
    "MSA 36GB 22222 333",
    "RA 36GB 33333 444",
    and so on ...
    )

    And more than 256 Characters are not allowed in one line. Is there anyway to continue with same instruction in next line in the code.

    Regards
    Susanta

  13. #13
    Conquistador
    Join Date
    Dec 1999
    Location
    Australia
    Posts
    4,527

    Re: How to initialize a two-dimensional Array

    Data = Array(
    "EVA 72GB 11111 222", _
    "MSA 36GB 22222 333", _
    "RA 36GB 33333 444", _
    and so on ... etc
    )

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