VB.NET从数组中删除字符串(如果存在于另一个数组中)

时间:2015-10-03 10:10:39

标签: arrays vb.net

我想在ComboBox中添加项目。我有两个array()

Dim purpose_list() As String = {"ADMISSION", "1ST", "2ND", "3RD", "4TH", "5TH", "6TH", "7TH", "8TH", "9TH", "10TH", "11TH", "12TH"}

Dim find_purpose = GetListOfPurpose()

函数GetListOfPurpose()如下所示..

Private Function GetListOfPurpose() As List(Of String)
    Dim Output As New List(Of String)()
    Try
        OpenConnection()
        Dim st_roll As String = TbRoll.Text
        Dim c_id As String = CmbCourse.SelectedValue

        Dim cmd As New MySqlCommand
        Dim qry As String = "SELECT purpose FROM fee_payment WHERE roll_no='" + st_roll + "' AND course='" + c_id + "'"
        cmd.Connection = conn
        cmd.CommandText = qry
        Dim dr As MySqlDataReader = cmd.ExecuteReader
        While dr.Read
            Output.Add(dr("purpose").ToString())
        End While
        dr.Close()
    Catch ex As Exception
        MsgBox(ex.Message)
    Finally
        CloseConnection()
    End Try
    Return Output
End Function

现在,我想在strings中找到find_purpose,如果存在,请删除strings,然后将其余内容添加到ComboBox

如果find_purpose包含"1ST""3RD",则ComboBox会添加其他项目

"ADMISSION", "2ND", "4TH", "5TH", "6TH", "7TH", "8TH", "9TH", "10TH", "11TH", "12TH"

我找到This thread,但它位于php

我应该在VB.NET中做什么?

1 个答案:

答案 0 :(得分:1)

您发布的代码:

Dim purpose_list() As String = {"ADMISSION", "1ST", "2ND", "3RD", "4TH", "5TH", "6TH", "7TH", "8TH", "9TH", "10TH", "11TH", "12TH"}
Dim find_purpose = GetListOfPurpose()

Private Function GetListOfPurpose() As List(Of String)
Dim Output As New List(Of String)()
Try
    OpenConnection()
    Dim st_roll As String = TbRoll.Text
    Dim c_id As String = CmbCourse.SelectedValue

    Dim cmd As New MySqlCommand
    Dim qry As String = "SELECT purpose FROM fee_payment WHERE roll_no='" + st_roll + "' AND course='" + c_id + "'"
    cmd.Connection = conn
    cmd.CommandText = qry
    Dim dr As MySqlDataReader = cmd.ExecuteReader
    While dr.Read
        Output.Add(dr("purpose").ToString())
    End While
    dr.Close()
Catch ex As Exception
    MsgBox(ex.Message)
Finally
    CloseConnection()
End Try
Return Output
End Function

现在,在事件处理子程序中(或者您想要进行检查的任何地方)输入以下代码:

Dim final_list As New List(Of String)
For Each item In purpose_list
    If Not(find_purpose.Contains(item)) Then
        final_list.Add(item)
    End If
Next
ComboBox1.Items.AddRange(final_list.ToArray())