0

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
4
Contributors
3
Replies
4
Views
4 Years
Discussion Span
Last Post by Begginnerdev
0

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 by tinstaafl

0

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

Edited by Maligui

0

Could you declare results as a list?

Dim Results As New List(Of String)

While Reader.Read()
    Results.Add(Reader(0))
End While
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.