Returning an string 2D string array by a function?

To all

Does anyone know if it's possible to return a 2D string array from a function? I am having problems with conversion of '2D' to '1D' . I am also having problem with Rezing aan array (ReDim)!

'---------------------------
Option Strict Off
Imports System
Imports System.IO
Imports System.Collections
Imports NXOpen
Imports NXOpen.UF
Imports System.Windows.Forms

Module Module1

Dim theSession As Session = Session.GetSession()
Dim ufs As UFSession = UFSession.GetUFSession()

Dim theLW As ListingWindow = theSession.ListingWindow
Dim theSimPart As NXOpen.CAE.SimPart = theSession.Parts.BaseWork

Sub Main()

theLW.Open()

Dim i,j,k as integer
'Get the data in the input file and allocate to array string arrsDetailSim()
'arrsDetailSim(1,k)={RespSimName1,RespSimName2,etc,}
'arrsDetailSim(2,k)={EventName1,Eventname2,etc}
'arrsDetailSim(3,k)={ResType1,ResType2,etc}
'arrsDetailSim(4,k)={KeyWord1,Keyword2,etc}
Dim arrsDetailSim() As String

arrsDetailSim = GetInputfile()

End sub

Function GetInputfile() As Object()

Dim openFileDialog1 As New OpenFileDialog()
Dim i,j,k as Integer
Dim arrsToReturn(5,10) As String 'Array has always 4 rows of data but I ignore row=0 (so row=5)
'but number of column is variable. use col=10 for testing
' to be set to arrsToReturn() if Redim is to be used !!

'ReDim arrsToReturn(UBound(arrsToReturn)+1) ' THIS DOES NOT WORK!!

openFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*"
openFileDialog1.FilterIndex = 1
openFileDialog1.RestoreDirectory = True

Dim delim As Char() = {","c}
Dim line As String

i=0
k=0

If openFileDialog1.ShowDialog() = DialogResult.OK Then

Using sr As StreamReader = New StreamReader(openFileDialog1.FileName)
Try
line = sr.ReadLine()
While Not line Is Nothing
Dim arrsLineString As String() = line.Split(delim)
k = k+1
'ReDim Preserve arrsToReturn(UBound(arrsToReturn)+1)
arrsToReturn(1,k) =arrsLineString(i)
arrsToReturn(2,k) =arrsLineString(i+1)
arrsToReturn(3,k) =arrsLineString(i+2)
arrsToReturn(4,k) =arrsLineString(i+3)

line = sr.ReadLine()

End While
Catch E As Exception
MessageBox.Show(E.Message)
End Try
End Using

End If

For j = 1 to 5
theLW.WriteLine("")
For k = 1 to UBound(arrsToReturn)
theLW.WriteLine("rank " & j.ToString &"," & k.ToString & " is:" & arrsToReturn(j,k))
Next k
Next j

Return arrsToReturn
End Function
end Module

Dim arrsToReturn(5,10) As String 'Array has always 4 rows of data but I ignore row=0 (so row=5)
'but number of column is variable. use col=10 for testing
' to be set to arrsToReturn() if Redim is to be used !!

'ReDim arrsToReturn(UBound(arrsToReturn)+1) ' THIS DOES NOT WORK!!

The ReDim statement will work on multi-dimension arrays, but I believe you will need to give it more information. ReDim needs to know the new size of each dimension; initially a 2D array was created, ReDim was only given enough information to resize a 1D array. Try something like:

Dim arrsToReturn(5,10) As String

ReDim arrsToReturn(5,15)

Or, in your case, maybe something like:

Dim arrsToReturn(5,10) As String

ReDim arrsToReturn(UBound(arrsToReturn(1),UBound(arrsToReturn(2)+1)

For more information about arrays, see:
https://msdn.microsoft.com/en-us/library/wak0wfyt.aspx
https://msdn.microsoft.com/en-us/library/w8k3cys2.aspx

Thanks. Always struggling with these arrays!
I used
m=4 'used to initiate arrsToReturn()
n= 0 'used to initiate arrsToReturn()
Dim arrsToReturn(m,n) As String

Then later in the loop where I read/process the input file
k = k+1
ReDim Preserve arrsToReturn(m,k)

and it seems to work

The only issue now is to return the array arrsToReturn() back to the Sub Main()

Regards

JXB

Thanks
Regards

Below is a quick example of returning a 2D string array from a function:

Option Strict Off
Imports System
Imports NXOpen

Module Module1

Sub Main()

Dim theSession As Session = Session.GetSession()
If IsNothing(theSession.Parts.Work) Then
'active part required
Return
End If

Dim workPart As Part = theSession.Parts.Work
Dim lw As ListingWindow = theSession.ListingWindow
lw.Open()

Dim myStringArray(,) As String = FillArray()

For i As Integer = 0 To UBound(myStringArray, 1)
For j As Integer = 0 To UBound(myStringArray, 2)
lw.WriteLine(myStringArray(i, j))
Next

Next

lw.Close()

End Sub

Function FillArray() As String(,)

Dim numRows As Integer = 5
Dim numcols As Integer = 3

Dim tempArray(numRows, numcols) As String

For i As Integer = 0 To numRows - 1
For j As Integer = 0 To numcols - 1
tempArray(i, j) = i.ToString & ", " & j.ToString
Next
Next

Return tempArray

End Function

End Module

For the code you posted, you might be better served by creating a small, simple class to hold the appropriate information. You could then use a list to hold the individual objects which would easily allow adding, removing, or sorting the information.

Thanks. This will do just fine for the moment. It was all down to the definition (,) for the string to return
Function FillArray() As String(,)

Thanks again

Regards

JXb

Thanks
Regards

You can definitely return a 2D array of strings from a function. In fact, as far as I know, you can return *anything* from a function.

I suggest avoiding Redim. It's old-fashioned and error-prone.