tambenj tambenj - 1 year ago 77
Vb.net Question

Fill entire row of 2D-Array in VB.Net

I have a 2-dimensional array that I want to fill with values, but only on specific rows and columns (not all of them). For example, I want all the values for an angle of 15° in column 15, for 30° in column 30 and so on. Same with the rows.

I could fill in every value manually with both indices, but that's kind of against the point of an array, right?

Is there a way to fill an entire Row of an array by defining the index for the row and then having the second index with the value together?

Something like this:

Dim values(300,45) As Double

'I want to fill row 15 in columns 15, 30 and 45
values(15) = {{15, 0.06},{30, 0.04},{45, 0.03}}

'now row 20, again just columns 15, 30 and 45
values(20) = {{15, 0.26},{30, 0.23},{45, 0.17}}

I know it doesn't work that way, just to give you an idea what I imagined.

I hope my problem is understandable and thank you for your help :)

Answer Source

There is nothing I know of in the VB language that provides that type of utility. You could use an extension method to get close to that syntax though.

Module ArrayExtensions
    Public Sub LoadValues(Of T)(arrayToFill As T(,), rowIndex As Int32, ParamArray items() As Array)
        For Each ar As Array In items
            Dim colIndex As Int32 = CInt(ar.GetValue(0))
            Dim val As T = CType(ar.GetValue(1), T)
            arrayToFill(rowIndex, colIndex) = val
    End Sub
End Module

Then using your example:

Dim values(300, 45) As Double

'I want to fill row 15 in coloumns 15, 30 and 45
values.LoadValues(15, {15, 0.06}, {30, 0.04}, {45, 0.03})

'now row 20, again just coloums 15, 30 and 45
values.LoadValues(20, {15, 0.26}, {30, 0.23}, {45, 0.17})
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download