Viktor M.
Viktor M.

Reputation: 4633

VB How to return and receive 3-dimensional array from function

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?

Upvotes: 1

Views: 6786

Answers (3)

golddove
golddove

Reputation: 1206

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.

Upvotes: 1

Mark Hall
Mark Hall

Reputation: 54562

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.

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

Upvotes: 4

Luke94
Luke94

Reputation: 712

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

Dim value(msgLen + 2, 1, 1) As String

But in the Click Listener you create an Array

Dim value(tbText.Text.Length + 2, 1, 1) As Array

Upvotes: 0

Related Questions