I'm having trouble figuring out how to fill a dynamic array function and then print it on declaration.

Here is the function:

 Public Function SQLConductGetFields(InCommand As Object) As String()

        Dim ConnectionA As New MySqlConnection(My.Application.sqlconnect)
        Dim command As New MySqlCommand()
        ConnectionA.Open()
        command.Connection = ConnectionA
        Dim reader As MySqlDataReader
        command.CommandText = InCommand
        reader = command.ExecuteReader()
        Dim count As Integer = 0
        Dim Results(count) As String

        While reader.Read()
            Results(count) = reader(0)
            count = count + 1
            ReDim Results(0 To count)

        End While
        reader.Close()
        ConnectionA.Close()

        SQLConductGetFields = Results
    End Function
End Class

This is how I am trying to access it:

  For index = 0 To T.SQLConductGetFields("Select Amount From " & Store1 & "lower Where id=" & modt.ToString & ";").GetUpperBound(0)
            MsgBox(T.SQLConductGetFields("Select Amount From " & Store1 & "lower Where id=" & modt.ToString & ";")(index) & " ")
        Next

When you ReDim Results it clears the values. Try placing the statement after line 22, or if it must be where it is, use the Preserve modifier to keep it from clearing the values

Edited 3 Years Ago by tinstaafl

I would have to agree with tinstaaf. Although I would place the Preserve keyword with the ReDim statement.

Edited 3 Years Ago by Maligui

Could you declare results as a list?

Dim Results As New List(Of String)

While Reader.Read()
    Results.Add(Reader(0))
End While
This article has been dead for over six months. Start a new discussion instead.