Problem with Array vb6

Dim mTaskArray() Form level variable

 File = Dir$(strPath & Extention)
    Do While Len(File)
         ReDim mTaskArray(iCounter)
        
        mTaskArray(iCounter) = gTasksFolder & File
         Debug.Print "test " & mTaskArray(iCounter) ' typical  C:\Users\Public\Documents\Personal\Tasks\Yr8-3-2016.rtf
         iCounter = iCounter + 1
       
        File = Dir$
    Loop

Open in new window

output by debug:
test C:\Users\Public\Documents\Personal\Tasks\01-03-2016.rtf
test C:\Users\Public\Documents\Personal\Tasks\02-20-2016.rtf
test C:\Users\Public\Documents\Personal\Tasks\03-10-2016.rtf
test C:\Users\Public\Documents\Personal\Tasks\07-26-2016.rtf
test C:\Users\Public\Documents\Personal\Tasks\1-3-2016.rtf
test C:\Users\Public\Documents\Personal\Tasks\10-14-2015.rtf
test C:\Users\Public\Documents\Personal\Tasks\11-18-2015.rtf
test C:\Users\Public\Documents\Personal\Tasks\11-29-2015.rtf
test C:\Users\Public\Documents\Personal\Tasks\12-25-2015.rtf
test C:\Users\Public\Documents\Personal\Tasks\3-10-2016.rtf
test C:\Users\Public\Documents\Personal\Tasks\9-25-2015.rtf
test C:\Users\Public\Documents\Personal\Tasks\Yr01-10-2016.rtf
test C:\Users\Public\Documents\Personal\Tasks\Yr06-23-2016.rtf
test C:\Users\Public\Documents\Personal\Tasks\Yr08-03-2016.rtf
test C:\Users\Public\Documents\Personal\Tasks\Yr09-09-2016.rtf
test C:\Users\Public\Documents\Personal\Tasks\Yr7-4-2016.rtf
test C:\Users\Public\Documents\Personal\Tasks\Yr8-22-2016.rtf
test C:\Users\Public\Documents\Personal\Tasks\Yr8-3-2016.rtf

this seems to work correct

but this does not, the array seems to be empty except for the last dimintion

Private Sub Command_Click()
Dim i As Integer
For i = LBound(mTaskArray) To UBound(mTaskArray) '0 to 17
Debug.Print "cmd " & mTaskArray(i)
Next
End Sub

Open in new window


the debug output
cmd
cmd
cmd
cmd
cmd
cmd
cmd
cmd
cmd
cmd
cmd
cmd
cmd
cmd
cmd
cmd
cmd
cmd C:\Users\Public\Documents\Personal\Tasks\Yr8-3-2016.rtf

What is wrong ?
isnoend2001Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

MacroShadowCommented:
Remove the ReDim mTaskArray(iCounter) line.
It resets the array so only the last text you put in is left.
If you want to preserve the arrays contents, you would use redim preserve, but in this case you don't want to do that.
0
isnoend2001Author Commented:
i commented that line then the next line gives "Subscript out range" error
 mTaskArray(iCounter) = gTasksFolder & File 'error here
0
MacroShadowCommented:
Before line 1 add
Dim mTaskArray() as String

Open in new window

0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

isnoend2001Author Commented:
already declared it
Option Explicit
Dim mTaskArray() Form level variable
i need it to have more scope
0
MacroShadowCommented:
Please provide full code.
0
isnoend2001Author Commented:
Project is too huge and many ocx's.
This is  all the code that pertains to the array
0
GrahamSkanRetiredCommented:
Try changing
ReDim mTaskArray(iCounter)

Open in new window

to
ReDim Preserver mTaskArray(iCounter)

Open in new window

0
GrahamSkanRetiredCommented:
Sorry, that should read
ReDim Preserve mTaskArray(iCounter)

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Martin LissOlder than dirtCommented:
You can do this instead (untested)
Dim MyCollection As New Collection
File = Dir$(strPath & Extention)
    Do While Len(File)
        MyCollection.Add gTasksFolder & File
        File = Dir$
    Loop

' Then to use
Dim lngIndex as Long
For lngIndex = 1 to MyCollection.Count
    Debug.Print MyCollection(lngIndex)
Next

Open in new window

0
MacroShadowCommented:
This is  all the code that pertains to the array
Where is iCounter defined? What value does it hold the first time on line mTaskArray(iCounter) = gTasksFolder & File? Does the array have an element equal to iCounter?

Without seeing your code, this is the best I can do. Most likely this is where the issue lies.
0
isnoend2001Author Commented:
thanks that works
0
isnoend2001Author Commented:
Where is iCounter defined?
 in the sub that creates the array
Dim iCounter As Integer
using Option Explicit would have caught it
using ReDim Preserve ( mTaskArray(iCounter) fixed the issue
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.