我找不到一种简单的方法来为多维数组赋值。它的工作原理如下:
Dim tTableArray(2, 14) As String
tTableArray(0, 0) = "John"
tTableArray(1, 0) = "Doe"
但似乎我应该能够做到这样的事情:
Dim tTableArray(2, 14) As String
tTableArray({0, 1}, 0) = {"John", "Doe"}
似乎有必要这样做,因为当你定义数组时,你可以,例如:
Dim values(,) As String = New String(,) {{"John", "Doe"}, {"Jane", "Doe"}, {"Spot", "the Dog"}}
任何想法?对不起,第一个问题 - 仍然需要弄清楚如何使代码块看起来像代码。
谢谢, 克里斯
答案 0 :(得分:2)
我不知道有一种内置的方式来分配它,但是一个自制的子可能同样好。扩展建议:
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
用法:
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", "-"})
' ...