1

I've got a question about using two-dimensional array.

Public twolist(,) As String
For i As Integer = 0 To twolist.length()-1
 If Func(twolist(i, )) Then 'this part is hard for me
     'doing something
 End If

Public Function Func(ByVal CancelInput() As String) As Boolean

What i want to do is Passing two-dimensional array to an array. I want to read one row in two-dimensional array and pass to function(Func), which is using an array.

Hope You can understand my question... and Thank you!

1
  • Then you need to create a 1D array and copy the elements from the 2D array, which you would do using a For loop. The size of the 1D array will be determined by the size of one dimension of the 2D array, not the entire Length, which is all elements. Commented May 2, 2018 at 3:20

3 Answers 3

2

As an alternative to the For Next Loop, you could use Linq (if you are confortable with it) to perform the same task.

This transforms each element of the source array to a String, groups them in an IEnumerable(Of String) and the result is converted to an unidimensional Array of Strings:

Dim twolist(N, N) As String

Dim CancelInput() As String = twolist.Cast(Of String).Select(Function(str) str).ToArray()

Dim result As Boolean = Func(CancelInput)
Sign up to request clarification or add additional context in comments.

3 Comments

Hi Thank you for giving me a way I didn't know! but After i tried this way, I've got a problem which said 'Cast' is not a member of 'System.Array'
@su jeong You are probably missing System.Collections.Generics from your project references. Go to you project Properties/References and add/tick System.Collections and System.Collections.Generics.
@su jeong Also, see if you can insert: Imports System.Linq in you class. It shouldn't be necessary, but you can verify is something is wrong there. Maybe you have deleted some system references in your code. If so, again => project Properties/References => add/tick System.Linq.
0

I have just used an arbitrary size for your array. You need nested For loops to iterate through a 2 dimensional array. The outer loop goes through the rows and the inner loop adds the value in each field to another array that you are passing to your Function. Each row is passed individually as a single dimension array.

Private Sub TwoDimensionalArray()
    Dim twolist(,) As String
    ReDim twolist(10, 5)
    'First you will need to add data to your array
    For x As Integer = 0 To 10
        Dim arrayRow(5) As String
        For y As Integer = 0 To 5
            arrayRow(y) = twolist(x, y)
        Next
        If Func(arrayRow) Then 'this part is hard for me
            'doing something
        End If
    Next
End Sub
Public Function Func(ByVal CancelInput() As String) As Boolean
    Return True
End Function

2 Comments

Oh thank you. The answer was what i knew. but i wasn't sure that if there is easier way. And Now i solved my worry. Thank you again!!
If my answer was helpful, please accept it with the check mark (tick mark) to the left of the answer.
0

Mary's answer is good, but assumes you know the length of each dimension.

I have changed it slightly to use the Array.GetLength function:

Private Sub TwoDimensionalArray()
    Dim twolist(,) As String
    ReDim twolist(10, 5)

    'First you will need to add data to your array
    For x As Integer = 0 To 10

        'Fetch the length of this dimension:
        Dim i As Integer = twolist.GetLength(x)

        Dim arrayRow(i) As String
        For y As Integer = 0 To i - 1
            arrayRow(y) = twolist(x, y)
        Next

        If Func(arrayRow) Then
            'do something
        End If
    Next
End Sub
Public Function Func(ByVal CancelInput() As String) As Boolean
    Return True
End Function

Note: In VB.Net, ReDim twoList(10,5) actually gives you an array of (11,6). Array.GetLength(0) will return 6 (0,1,2,3,4,5). In short, Dim specifies the maximum index in each dimension, Length & GetLength return the count of elements.

1 Comment

Oh Thanks I found easy and simple way to get length cuz you. I gotta use this! thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.