简体   繁体   中英

problems iterating through generic list of string

I have the follow code in my program where I hit a SQLCe database to append the results into a list. That part works, but instead of exiting the function 'QueryDB' it goes to the else statement and runs the function again, which will return a null value. I designed it this way becuase I wanted to check to make sure the database is open before I try to execute the SQL statement, and if it's not open, call the method to open it and run through the function again.

Public Function QueryDB(ByVal strSQL As String) As List(Of String)

    Dim reader As SqlCeDataReader
    Dim results As New List(Of String)

    Using cmdAdd As New SqlCeCommand(strSQL, conn)
        If Me.checkConnection Then
            reader = cmdAdd.ExecuteReader()
            Do While reader.Read
                results.Add(reader.GetString(0))
            Loop

            Return results
            Exit Function

        Else
            connectPlansdb()
            QueryDB(strSQL) 'does not exit function when done and goes through the function again

        End If

    End Using

End Function

The second problem I have is when I try to populate the list into a combo box in my form class where I call out to the database and use the returned list to populate my combo box. I can't seem to figure out how to get the code to deal with the list.

    Private Sub cmbInvestmentStrategy_DropDown(sender As System.Object, e As System.EventArgs) Handles cmbInvestmentStrategy.DropDown

    Dim strat As New clsInvestmentStrategies()
    Dim invStrat As New List(Of String)

    invStrat = strat.getInvestmentStrategies() 'String cannot be converted to List(pf String)
    cmbInvestmentStrategy.Items.Add(invStrat) 'Error    3   Value of type 'System.Collections.Generic.List(Of String)' _
    'cannot be converted to '1-dimensional array of Object'.    

    End Sub

Any help would be greatly appreciated.

Thanks!

Your QueryDB method has a big flaw. If the database is unavailable (connectivity problems, database offline or wrong connection string), there will be an infinite loop. Your query DB method should just query the database. You could wrap it in another method responsible for connecting to the database, but you don't want to retry database connection infinitely.

As for your second method:

invStrat = strat.getInvestmentStrategies() 'String cannot be converted to List(Of String)

Error is pretty clear here. getInvestementStrategies returns a single String and cannot be converted to a list. It should return a List(Of String), or at least some collection of strings, I suppose?

cmbInvestmentStrategy.Items.Add(invStrat) 'Error    3   Value of type 'System.Collections.Generic.List(Of String)' _
'cannot be converted to '1-dimensional array of Object'.

Items.Add will add a single element to the combobox. You should loop through the invStrat values, and call Add for every item. Alternatively, you could use the AddRange method, but this method expects an Array, not a List.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM