Array manipulation

x86

Member
Joined
Jan 22, 2006
Messages
10
Programming Experience
Beginner
I'm not able to figure out this school work and i spent 2 hours on it. So ill seek help from you guys, before i go nuts.

I'm trying to add some data into the array and display it in a column. That's it!

I tried some code like this but it does not seem to work.
VB.NET:
dim dataArray(5,5)
Dim askNum = InputBox("enter some numbers ")
Dim data as integer
' loop through 5 numbers to populate array in columns
For i As Integer = 0 To 4
namesArray(i, data) = askNum
lblout.Text &= askNum
Next
 
how does it not work? what are you trying to do? from what I see it looks to be working perfectly.
 
for example, if my Array was [2][3], with numbers,
1 2 3
4 5 6

The columns in that case would be 1,4 & 2,5 & 3,6

In my case, i'm populating the array with numbers from the inputbox. Using a loop, I know i should be able to add the values in the column section of the array and display it vertically, without the vbcrlf, i hope.

How do i display the rows of an array? The columns?
 
There's a couple of things that you need to note. First off, myArray(5, 5) and myArray(5)(5) are NOT the same thing. The first is a 2-dimensional array, i.e. a matrix, while the second is called a jagged array, i.e. a 1-dimensional array of 1-dimensional arrays. They are similar but NOT the same. Also, you may already be aware but arrays are zero-based, so each of the examples I just gave have six rows and six columns, indexed from zero to five.

Normally if you want to traverse an entire 2-dimensional array you would use two nested For loops, one for the row and one for the column. You start with row zero and column zero, then you increment the column number until you reach the end. Then you go back to column zero and increment the row. You repeat this until you have visited every element. In code this looks like this:
VB.NET:
Dim maxRowIndex As Integer = myArray.GetUpperBound(0) 'The index of the last row.
Dim maxColIndex As Integer = myArray.GetUpperBound(1) 'The index of the last column.
Dim currentRowIndex, currentColIndex As Integer

For currentRowIndex = 0 To maxRowIndex Step 1
    For currentColIndex = 0 To maxColIndex Step 1
        'Use row and column coordinates here.
    Next currentColIndex
Next currentRowIndex
As this is homework I won't give any more code, but this should give you the idea.
 
Thanks, i'll be using GetUpperBound() from now.


I'm also trying to place that data in table format with headings. Is there a way to do that with the listbox or listview given i'm using the 2Dim-array and data from the inputbox?

I'm having a really hard time trying to make something like this work.

Here's what i'm thinking, the headings and the data.

VB.NET:
ID  |   Name  |    Site    | Year
--- ---------- ----------- ------
1      name1     sitename1   2004
2      name2     sitename2   2005
3      name3     sitename3   2005

Any help or site tutorials would be great!!! :)
Thanks.
 
A ListView would be the way to go. Set the View to Details and add the appropriate Columns in the design window. Then at run time you add individual ListViewItems for each row. I'd suggest reading the help/MSDN topic for the ListView and its member listing. It's almost certain to have a code example of just this. In fact, I suggest reading the overview and member listing from the help for EVERY new class you want to use. You can learn a lot by trial and error, but you won't learn everything and you can waste a lot of time.
 

Latest posts

Back
Top