【问题标题】:VB.NET remove strings from an array if exists in another如果存在于另一个数组中,VB.NET 从数组中删除字符串
【发布时间】:2015-12-31 12:00:42
【问题描述】:

我想在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

现在我想在find_purpose 中找到strings,如果存在,删除那些strings 并将其余的添加到ComboBox

如果find_purpose 包含"1ST""3RD"ComboBox 将添加其余项目

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

我找到了This thread,但它在php

VB.NET 应该怎么做?

【问题讨论】:

  • ComboBox1.Items.AddRange(purpose_list.Except(find_ purpose).ToArray())
  • @HansPassant 感谢您的评论。但是您的代码正在跳过"2ND"
  • LIST.FIND 可以为每个人循环提供帮助...试试这个:stackoverflow.com/questions/9854917/…

标签: arrays vb.net


【解决方案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

现在,在 Event-Handling Sub(或您想要进行检查的任何地方)中输入以下代码:

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())

【讨论】:

  • 等我更新答案。我只是误解了整个事情。
猜你喜欢
  • 1970-01-01
  • 2022-11-12
  • 2020-05-18
  • 2018-11-01
  • 2013-03-02
  • 1970-01-01
  • 1970-01-01
  • 2011-05-19
  • 2014-12-25
相关资源
最近更新 更多