create array with unknown # of elements

lkrterp

Member
Joined
Jul 10, 2007
Messages
21
Programming Experience
Beginner
I would like to create an array, but do not know the number of elements that will be passed to it.

The array would hold any error information (a string) caused by an unsuccessful database query. The code contains multiple queries and if an error occurs I would like to capture it in a table. There could be 1 error or 1000.

How do I Dim the array and then how would I load the information to it.

Thanks
 
You can't create an array without specifying the number of elements it contains. If you want to change the size of the array later then what you have to do is create a new array with the new size and then copy the data from the old array to the new. This functionality is encapsulated in the ReDim Preserve statement.

That said, you could just use an ArrayList, which is intended to function as a dynamic array. You create the collection and then you can just call Add and Remove and the collection will grow and shrink as needed.

As you're storing Strings it would probably be best to use a Specialized.StringCollection. It is much like the ArrayList but intended specifically for Strings, where the ArrayList can store anything and requires casting of items when they're retrieved.

From .NET 2.0 you have the luxury of generics and the List(Of T) class. It functions almost identically to the ArrayList but you specify the type of the items when you create the collection and there is then no casting required.
 
Back
Top