Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I need to read data (maybe numbers or strings) from a file into an array (using VB2019).

The value of the two indices of the array is variable from case to case, i.e., not known at the start. How can I do that? I know that the first index cannot be changed by ReDim, yet I need to do a ReDim on the first index. Any ideas?

What I have tried:

I can determine the number of elements along x & y axes individually.
Posted
Updated 8-Feb-22 0:24am
v2

You can't, and unless you are using VB6 or earlier, Redim is a pretty bad idea as it creates a new array, which means that "older references" to that data may not be updated. It's also a slow and memory wasting process if your array gets large.

Instead, use a List of (List of object) as they can be expanded or contracted as needed if you can't "know" the number of items in advance. This also has the advantage that the number of items per "row" does not have to be the same - if the file data has 2 items on line one and 6 on line two, it will still cope without reallocation.
 
Share this answer
 
Comments
Member 10118655 9-Feb-22 11:59am    
Thanks. Looks like that's the only way.
Thanks. Looks like that's the only way.
 
Share this answer
 
v3
Comments
Tony Hill 8-Feb-22 6:34am    
Don't submit a comment or thanks as a solution, submit it as comment.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900