|
-
Mar 22nd, 2006, 02:25 AM
#1
Thread Starter
New Member
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
-
Mar 22nd, 2006, 02:27 AM
#2
Frenzied Member
Re: How to initialize a two-dimensional Array
you may do it like this
VB Code:
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
-
Mar 22nd, 2006, 02:35 AM
#3
Thread Starter
New Member
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
-
Mar 22nd, 2006, 02:49 AM
#4
Frenzied Member
Re: How to initialize a two-dimensional Array
you can do this
VB Code:
dim data1(5, 5) as string
data1(0, 0) = "EVA"
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
-
Mar 22nd, 2006, 03:09 AM
#5
Conquistador
Re: How to initialize a two-dimensional Array
data1 = Array(Array("EVA","MSA1000"), Array("RA", "MSA30", "33333-333"))
-
Mar 22nd, 2006, 03:51 AM
#6
Thread Starter
New Member
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.
-
Mar 22nd, 2006, 04:09 AM
#7
Conquistador
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
?
-
Mar 22nd, 2006, 04:54 AM
#8
Thread Starter
New Member
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
-
Mar 22nd, 2006, 05:05 AM
#9
Conquistador
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
-
Mar 22nd, 2006, 05:15 AM
#10
Thread Starter
New Member
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
-
Mar 22nd, 2006, 09:56 AM
#11
Conquistador
Re: How to initialize a two-dimensional Array
VB Code:
Sub test()
Dim strInput As String, i As Integer
Dim strArray(100, 3) As String
Dim fPos As Integer, sPos As Integer
Open "C:\test.txt" For Input As #1
i = 0
While Not EOF(1)
Line Input #1, strInput
fPos = InStr(1, strInput, " ")
sPos = InStr(fPos + 1, strInput, " ")
strArray(i, 0) = Left(strInput, fPos)
strArray(i, 1) = Mid(strInput, fPos, sPos - fPos)
strArray(i, 2) = Right(strInput, Len(strInput) - sPos)
Debug.Print strArray(i, 0), strArray(i, 1), strArray(i, 2)
i = i + 1
Wend
Close #1
End Sub
?
-
Mar 23rd, 2006, 12:09 AM
#12
Thread Starter
New Member
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
-
Mar 23rd, 2006, 04:27 AM
#13
Conquistador
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|