vb.net VB如何从函数返回和接收3维数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12991326/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
VB How to return and receive 3-dimensional array from function
提问by Viktor V.
I have faced with a problem of how to work with multidimensional array. I have a Function:
我遇到了如何处理多维数组的问题。我有一个功能:
Public Function findCheckDigit(ByVal text As String) As String(,,)
Dim msgLen As Integer = text.Length
Dim value(msgLen + 2, 1, 1) As String
...
...
...
Return value
End Function
Below I try call this function:
下面我尝试调用这个函数:
Private Sub bGenerate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bGenerate.Click
Dim value(tbText.Text.Length + 2, 1, 1) As Array
value(tbText.Text.Length + 2, 1, 1) = findCheckDigit(tbText.Text) <--- Here is a problem
MsgBox(value(0, 1, 0)) ' Return empty in any position
End Sub
I`m sure that the problem in this place but how to implement my function call to another 3-dimensional array with the same size?
我确定问题出在这个地方,但是如何实现对另一个具有相同大小的 3 维数组的函数调用?
回答by Mark Hall
You are creating an Empty array and not putting any data into it. You do not need to create the bounds on the receiving array, do something like this and make sure the Array Types are the same as was mentioned earlier by Luke94.
您正在创建一个空数组,而不是将任何数据放入其中。您不需要在接收数组上创建边界,执行类似操作并确保数组类型与 Luke94 之前提到的相同。
Public Function findCheckDigit(ByVal text As String) As String(,,)
Dim msgLen As Integer = text.Length
Dim value(msgLen + 2, 1, 1) As String
value(0, 0, 0) = "Hello"
Return value
End Function
Private Sub bGenerate_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Dim value(,,) As String
value = findCheckDigit(tbText.Text)
MsgBox(value(0, 0, 0))
End Sub
回答by golddove
When you do
当你做
Dim value(msgLen + 2, 1, 1) As String
You are trying to access an element of the array that does not exist. Let's say the array has 5 elements (each containing 2d arrays). msgLen would be 5. Now, you are tying to access the eight element in "value" because you are saying msgLen + 2. That would not work because there are only 5 elements.
您正在尝试访问不存在的数组元素。假设数组有 5 个元素(每个包含二维数组)。msgLen 将是 5。现在,您要访问“value”中的 8 个元素,因为您说的是 msgLen + 2。这行不通,因为只有 5 个元素。
回答by Luke94
I am not sure if I have understood your question right. So here's my suggestion
我不确定我是否理解你的问题。所以这是我的建议
In the findCheckDigit function, you declare your Array as String
在 findCheckDigit 函数中,您将 Array 声明为 String
Dim value(msgLen + 2, 1, 1) As String
Dim value(msgLen + 2, 1, 1) 作为字符串
But in the Click Listener you create an Array
但是在 Click Listener 中,您创建了一个数组
Dim value(tbText.Text.Length + 2, 1, 1) As Array
Dim value(tbText.Text.Length + 2, 1, 1) 作为数组

