Array creation.
I have this question.
I have already imported all the excel data in the datatable,
but now I want them stored in a 2 dimensional array, meaning I want to have this kind of thing in the array, something like array[x][y], where x is the row and y is the column.
how can I create this kind of array?
thanks
Related Article: Interacting with Excel using vb.net
is a solved VB.NET discussion thread by belenos46 that has 4 replies, was last updated 11 months ago and has been tagged with the keywords: excel, vb.net, xls, microsoft.
jbutardo
Junior Poster in Training
74 posts since Jan 2012
Reputation Points: 8
Solved Threads: 2
Skill Endorsements: 0
A 2 dimension array means that all the items, in both dimensions, must be of the same type. If the columns in the datatable are not of the same type, this can not be implemented.
While the solution from Oxiegen uses ArrayLists, and is a very good solution for unknown types of items, the solution from ChrisPadgham uses less overhead but needs to know in advance the types and the number of elements.
Also there is an intermediate solution: use a undefined length 2 dimensions array of a known type, then enlarge the array as needed.
To define the array (lets say of integers) you can use:
Dim TwoDimensionsArray(,) as Intgeger
Then on the loop to fill the array from the datatable you can write something like:
Dim R as Integer = -1
For Each row As DataRow In <datatable>.Rows
R += 1;
If TwoDimensionsArray Is Null Then
ReDim TwoDimensionsArray(0,1)
Else
ReDim Preserve TwoDimensionsArray(R,1)
End If
TwoDimensionsArray(R,0) = row("first column")
TwoDimensionsArray(R,1) = row("second column")
Next
The ReDim Preserve sentence copies the current content in a new memory address and allocates as many space as requested. Depending on the size, this solution can have more (or less) overhead than the one of ArrayLists depending on the number of elements. Allways will have more overhead than the solution of knowing the right size.
Hope this helps
lolafuertes
Practically a Posting Shark
890 posts since Oct 2008
Reputation Points: 164
Solved Threads: 189
Skill Endorsements: 5
Question Answered as of 1 Year Ago by
Oxiegen,
ChrisPadgham
and
lolafuertes