-4

could someone please advice, i am getting compile error for the following vb.net code.
VS highlights few error.

  • Number of indices is less than the number of dimensions of the indexed array.
  • Value of type 'Integer()' cannot be converted to 'Integer(,)' because the array types have different number of dimensions.
  • Public Sub Int_SplitEvenOdd_2D_Array(ByRef rawArray(,) As Integer, ByRef evenArray(,) As Integer, ByRef oddArray(,) As Integer)
        Dim firstUpperBound As Integer = rawArray.Length
        Dim secondUpperBound As Integer = rawArray(0).Length
        Dim rows As Integer = 3
        Dim columns As Integer = 2
        evenArray = New Integer((rows) - 1) {}
        oddArray = New Integer((rows) - 1) {}
        Dim i As Integer = 0
        Do While (i <= columns)
            evenArray(i) = New Integer((columns) - 1) {}
            oddArray(i) = New Integer((columns) - 1) {}
            i = (i + 1)
        Loop
    
        Dim value As Integer = 0
        Dim EvenIndex As Integer = 0
        Dim EvenIndex2 As Integer = 0
        Dim OddIndex As Integer = 0
        Dim OddIndex2 As Integer = 0
    
        Do While (i < firstUpperBound)
            Dim j As Integer = 0
            Do While (j < secondUpperBound)
                value = rawArray(i)(j)
                If ((value Mod 2) = 0) Then
                    evenArray(EvenIndex)(EvenIndex2) = (value + 2000)
                    EvenIndex2 = (EvenIndex2 + 1)
                    If (EvenIndex2 = columns) Then
                        EvenIndex2 = 0
                        EvenIndex = (EvenIndex + 1)
                    End If
    
                Else
                    oddArray(OddIndex)(OddIndex2) = (value + 3000)
                    OddIndex2 = (OddIndex2 + 1)
                    If (OddIndex2 = columns) Then
                        OddIndex2 = 0
                        OddIndex = (OddIndex + 1)
                    End If
    
                End If
    
                j = (j + 1)
            Loop
    
            i = (i + 1)
        Loop
    
    End Sub
    
  • 1 Answers1

    1

    Your subroutine declaration declares evenArray and oddArray as 2 dimensional arrays (,) - However when you call New on them you are re-defining them as single dimension arrays () - then further down you are treating them as jagged arrays. Assuming you wanted them to be multidimensional arrays, you need to give them two array boundaries when calling New and to always reference them with two indices (x,y).

    evenArray = New Integer(rows - 1, columns -1) {}
    oddArray = New Integer(rows - 1, columns -1) {}
    

    https://msdn.microsoft.com/en-us/library/d2de1t93(v=vs.90).aspx

    Proper declaration will remove the need for the below, which is where you are treating them as jagged arrays:

    Do While (i <= columns) 'Also note: This should be the number of rows, not columns if you want to use jagged arrays.
        evenArray(i) = New Integer((columns) - 1) {}
        oddArray(i) = New Integer((columns) - 1) {}
        i = (i + 1)
    Loop
    

    The above is actually assigning them as if they are Jagged arrays, which are like multidimensional arrays but not the same (one is like a Grid, the other like a bar graph to give you a visual representation) if Jagged arrays are what you want they typically have a declaration more along the lines of:

    evenArray = New Integer(rows -1)()
    

    and are accessed like so:

    evenArray(row)(column) = value
    

    The difference is simple, a multidimensional array is effectively a Grid, all rows have the same number of columns. In a jagged array, all of your rows, could have a different number of columns, and each row needs you to call New or assign it a single dimension array (which is what your for loop is doing)

    https://msdn.microsoft.com/en-us/library/hkhhsz9t(v=vs.90).aspx

    Hope that helps. :)

    n0mad
    • 11
    • 1