Redim Array

emia
09-08-2009, 10:20 AM
Hello, I have problem , when Redim Array, and create new array,
Error ?

Any idea to fix, please!


Option Explicit


Private Sub Command1_Click()

Dim a() As String
ReDim a(2, 1) As String

a(0, 0) = "a00"
a(0, 1) = "a01"
a(1, 0) = "a10"
a(1, 1) = "a11"
a(2, 0) = "a20"
a(2, 1) = "a21"

'Need use For repert 5 times
Dim FCMax As Double
FCMax = 5

Dim b() As String
ReDim b(0 To UBound(a) + FCMax, 0 To 1)

Dim crr As Long 'base=0
Dim i As Integer


For i = LBound(a) To UBound(a)
b(crr, 0) = a(i, 0)
b(crr, 1) = a(i, 1)
crr = crr + 1
Next i


'Probelm
Dim e as Integer
'Now, Problem is why use For(), not success ?
For e = 0 To FCMax
b(crr, 0) = "a" '& e
b(crr, 1) = "b" '& e
crr = crr + 1
Next e
'/////////////////


'Result
Dim cm As Integer
For cm = LBound(b) To UBound(b)
List1.AddItem cm & ")" & b(cm, 0) & ">" & b(cm, 1) & ">"
Next cm



End Sub

EmptyVessel
09-08-2009, 12:45 PM
If you let your program fail then click the debug button you can hover your mouse over variables. VB6 will report their current value at the time of the failure.

Doing so in your program shows that the variable 'crr' is 8 and 'e' has a value of 5. Since the upper bound of the left side of 'b' is 7 you are exceeding the upper bound of 'b'. This is what the error "Subscript out of bounds" means.

Change your Last 'For' statement to:

For e = ubound(a) to ubound(b)

P.S. You should really use the second argument of Ubound(<arg1>,<arg2>) to specify which column you are getting the upper bound from. Left or Right
(Numbered as 1 and 2). uBound(b,1) = 7.... uBound(b,2) = 1

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum