Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VB.Net - Assign values to 1 row of MultiDimensional Array

Tags:

vb.net

I cannot find an easy way to assign values to a multidimensional array. It works like this:

Dim tTableArray(2, 14) As String
tTableArray(0, 0) = "John"
tTableArray(1, 0) = "Doe"

but it seems like I should be able to do something like this:

Dim tTableArray(2, 14) As String
tTableArray({0, 1}, 0) = {"John", "Doe"}

It seems like there aught to be way to do that, because when you are defining the array you can, for example:

Dim values(,) As String = New String(,) {{"John", "Doe"}, {"Jane", "Doe"}, {"Spot", "the Dog"}}

Any Ideas? Sorry, first questions - still need to figure out how to make a code block look like code.

Thanks, Chris

like image 511
Chris McDonough Avatar asked Dec 29 '25 22:12

Chris McDonough


1 Answers

I don't know of a built-in way to assign that, however a self-made sub might be just as good. An extension suggestion:

Imports System.Runtime.CompilerServices  
Module Module1

    <Extension()>
    Public Sub setRow(p(,) As Object, pRow As Integer, pValues As Object())
        If p.GetLength(1) >= pValues.Length Then
            For i As Integer = 0 To pValues.Length - 1
                p(pRow, i) = pValues(i)
            Next
        End If
    End Sub

End Module

Usage:

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        Dim x(,) As String
        ReDim x(2, 4)

        x.setRow(0, {"Mr.", "", "John", "Doe", "-"})
        x.setRow(1, {"Ms.", "Dr.", "Sue", "Smith", "-"})
        ' ...
like image 174
KekuSemau Avatar answered Jan 07 '26 10:01

KekuSemau



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!