Declaring 2D arrays within 1D arrays

  • Thread starter Thread starter Tariq Hasan
  • Start date Start date
T

Tariq Hasan

Guest
Hi. I want to declare an one dimensional array in which each element has 2D array. I have declared the array variables like:

Dim subArray()(,) As Integer

Later i have initialized the array like:

cp=3

Iteration=8

Gene=8

ReDim subArray(cp)(Iteration, Gene)

meaning that subArray(0), subArray(1), subArray(2) and subArray(3) each will have the marix of 9x9 dimensions

Is my array declaration is OK?

Just after the redeclaration of array when i run a For Loop for copying values from another Rand Matrix to each subArray matrix i find NullReferenceException error. Here is my code:

Dim subArray()(,) As Integer

Private Sub btnOk_Click(sender As Object, e As EventArgs) Handles btnOk.Click


Try
cp = 3
Gene = 8
Iteration=8

BuildMatrix(Iteration, Gene)

ReDim subArray(cp)(Iteration - 1, Gene - 1)

For w = 1 To 42 // NullReferenceException error is caught in this line
gene_value(j) = CInt(txtGene(w).Text)

For j = 1 To cp

Array.Copy(rndMatrix, j - 1, subArray(j), j - 1, Iteration * gene_value(j))

Next
Next

Catch ex As Exception
MessageBox.Show(ex.Message)
End Try

End Sub

Public Sub BuildMatrix(rows As Integer, columns As Integer)

ReDim rndMatrix(rows - 1, columns - 1)
For x = 1 To rows
Dim valu As Integer = mRand.Next(0, Integer.MaxValue)
For y = 1 To columns
rndMatrix(x - 1, y - 1) = ((valu >> y) And &H1)
Console.Write(String.Format("{0} ", rndMatrix(x - 1, y - 1)))
Next
Console.Write(Environment.NewLine)
Next
End Sub

Continue reading...
 
Back
Top