Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Redim preserve in a loop(Synchronization problem)

Status
Not open for further replies.

yanikr

Programmer
Apr 29, 2004
8
0
0
CA
Hi,

I would appreciate if you can help on this: I have a Redim Preserve statement in a loop. The array get redimensioned a few times(15-20 times) with no problems, but sometimes, I get the message "object not set to an instance of an object".

It looks like its trying to Redim the array while the previous redim is not finished.. I am fairly confident that this is whats happening as I tried to put a textbox to see the arraysize everytime it changes. By doing that the process had to stop for a second or 2 to display the messagebox..
Result: no errors displayed..

Is there anyway to check the status of the array (If it is locked or if the previous redim is not finished)?

Please Help!
 
Sorry, here is the code:

For Each fileName In extractedFileList

tooldetails = myExtractor.Extract(fileName.FullName, False)

'Make sure extraction is successful.
If myExtractor.isExtractSuccess Then

'If its the first array copy, remove 1 to the array length as a length of 1 had to be attributed to create the array instance.

If firstArrayCopy Then
firstArrayCopy = False
targetArraySize = allToolDetails.GetLength(0) - 1
Else
targetArraySize = allToolDetails.GetLength(0)
End If

Try
'ReDimension the array to hold all the new tool information
ReDim Preserve allToolDetails(tooldetails.GetLength(0) + targetArraySize - 1)

'Copy the new tool array retrieved into a summary array
tooldetails.CopyTo(allToolDetails, targetArraySize)

Catch e As Exception
MessageBox.Show("Problem in function 'NCFileEditorProofing.IsEdtitionSuccess'" & vbCrLf & vbCrLf & e.Message & " " & e.Source, "Unknown Error..", MessageBoxButtons.OK, MessageBoxIcon.Error)

End Try

Else
'Extraction unsuccessful, advise user. Exit Process.
MessageBox.Show("Problem with file:" & fileName.FullName & Chr(13) & myExtractor.getErrorMessage)
Exit Function

End If
Next
 
Ohh... And the error is on the Redim Statement..

Thanks
 
I think the preserve only works when you have more elements not when you have less

so if you had 30 first and try redim preserve arrya(29) that wont work

Christiaan Baes
Belgium
"What a wonderfull world" - Louis armstrong
 
I would say avoid using Redim and especially with Preserve because redim preserve is considered to be slow and inefficient. Here is what Microsoft has to say about it

If your array changes size frequently and you need to retain the values of existing elements, the ArrayList object can give you better performance than the ReDim statement with the Preserve keyword.

Here is the link the explains it.

-Kris
 
Another vote for using an ArrayList instead of the redim-preserve.

Chip H.


____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
Ok,Thanks to all of you, from now on, arraylist!


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top