We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,489 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

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

4
Contributors
6
Replies
2 Days
Discussion Span
1 Year Ago
Last Updated
8
Views
Question
Answered
jbutardo
Junior Poster in Training
74 posts since Jan 2012
Reputation Points: 8
Solved Threads: 2
Skill Endorsements: 0

You could use two ArrayLists.
For each ArrayList as an item in another ArrayList.
The "outer" ArrayList contains the rows, and the "inner" ArrayList contains the columns.

Dim arrRows As New ArrayList

For Each row As DataRow In <datatable>.Rows
   Dim arrCols As New ArrayList
   arrCols.Items.Add(row("first column"))
   arrCols.Items.Add(row("second column"))
   arrCols.Items.Add(row("third column"))
   arrCols.Items.Add(row("fourth column"))
   'and so on

   arrRows.Items.Add(arrCols)
Next

To access those rows and columns in the ArrayLists, you can do this.

For Each item As ArrayList in arrRows
   variable1 = item(0)
   variable2 = item(1)
   variable3 = item(2)
   variable4 = item(3)
Next

'Or this

someVariable1 = arrRows(0)(0) 'Retrieve the first "cell" in the first row
someVariable2 = arrRows(2)(3) 'Retrieve the fourth "cell" in the third row
Oxiegen
Master Poster
761 posts since Jun 2006
Reputation Points: 87
Solved Threads: 149
Skill Endorsements: 5
dim TwoDArray(10,10) as Int16
ChrisPadgham
Posting Pro in Training
451 posts since Sep 2009
Reputation Points: 127
Solved Threads: 88
Skill Endorsements: 5

Both would work for what you intend to do.

Oxiegen
Master Poster
761 posts since Jun 2006
Reputation Points: 87
Solved Threads: 149
Skill Endorsements: 5

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

if you have more than one type you may be better with a user defined type

Public Type UserDetails
    MyName As String
    BackupName As String
    CurrId as Integer
End Type

dim Userlist(99) as UserDetails
ChrisPadgham
Posting Pro in Training
451 posts since Sep 2009
Reputation Points: 127
Solved Threads: 88
Skill Endorsements: 5

All of these examples are perfectly good. When it comes down to it, it's a matter of preference for which way to go.
There really is no right or wrong method.

Oxiegen
Master Poster
761 posts since Jun 2006
Reputation Points: 87
Solved Threads: 149
Skill Endorsements: 5
Question Answered as of 1 Year Ago by Oxiegen, ChrisPadgham and lolafuertes

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.0930 seconds using 2.7MB