Two-dimensional array declaration

I have several college assignments that I'm having problems with. Actually, I'm just confused by one thing about the array. I need to declare an array of three columns, 5 rows. The first two columns are integers, and the third is alphabetic. Therefore, I am very confused by the declaration of the data type, since they are different. This is my first turn with arrays, so please excuse my ignorance. This is what my array should look like.

Column 1 {0,300,350,400,450}
Column 2 {299,349,399,449,500}
Column 3 {F,D,C,B,A}

(This app is for evaluation)

I can solve the rest of the problem myself, I'm just confused by this part of the array. So my question is how to declare such an array. They say that a two-dimensional array is used, which only confuses me, since there are three columns. Thank!

+4
source share
2 answers

The 2 dimensional array is correct. The first index is a column, the second is a row.

Dim strData(,) As String 'Use String variable type, even for the numbers
Dim intRowCount As Integer = 5
Dim intColumnCount As Integer = 3
ReDim strData(intColumnCount - 1, intRowCount - 1) 'subtract 1 because array indices are 0-based. Column 0 = Range start, Column 1 = Range End, Column 2 = Grade
'first row
strData(0, 0) = "0" 'Range start
strData(1, 0) = "299" 'Range end
strData(2, 0) = "F" 'Grade
'second row
strData(0, 1) = "300"
strData(1, 1) = "349"
strData(2, 1) = "D"
'third row
strData(0, 2) = "350"
strData(1, 2) = "399"
strData(2, 2) = "C"
'fourth row
strData(0, 3) = "400"
strData(1, 3) = "449"
strData(2, 3) = "B"
'fifth row
strData(0, 4) = "450"
strData(1, 4) = "500"
strData(2, 4) = "A"
'Add a row
intRowCount = intRowCount + 1
ReDim Preserve strData(intColumnCount - 1, intRowCount - 1)
'sixth row
strData(0, 5) = "501"
strData(1, 5) = "600"
strData(2, 5) = "A+"

Please note that Redim Preserveonly the last index in the array can change, so we keep it in order (column, row), not in a more traditional (row, column)order.

+9
source

There are several ways to approach this. One of them is to declare the array as an object type, and they assign integers or strings to the corresponding element. Some do not find this socially acceptable, because it can lead to complex code debugging.

You can also use the String type for a two-dimensional array and store integers in string variables. This also usually fails due to the conversion needed for numerical comparisons and calculations.

, , .

,

Structure Item
  Dim col1 as integer
  Dim col2 as integer
  Dim col3 as string
  End Structure

Dim itemList(20) as Item

itemList(4).col1 = 23
itemList(4).col2 = 45
itemList(4).col3 = "somestring"
+2

All Articles