使用客户名称在我的字典中查找订单

时间:2018-02-16 19:19:55

标签: vb.net dictionary

我需要使用name在字典中查找订单,但无法确定如何使用名称(字符串)来查找匹配顺序,因为字典的值是对象Order。

Public Class MainForm
    Public Shared orders As New Dictionary(Of Integer, Order)

Private Sub btnFindName_Click(sender As Object, e As EventArgs) Handles btnFindName.Click

    Dim orderFound As Boolean

    Dim name As String = txtFindName.Text

    For Each order In orders

        If order.Value = name Then

            orderFound = True
            Return

        End If

    Next
    If Not orderFound Then
        errProvider.SetError(txtFindName, "No order found with that name.")
        txtFindName.Focus()
        Return

    End If
End Sub

1 个答案:

答案 0 :(得分:0)

当你遍历Orders时,你得到的是KeyValuePair整数,Order。假设您的Order类具有Name属性:

Public Class Order
    Public Property Name As String
End Class

您可以这样调整:

For Each Order As KeyValuePair(Of Integer, Order) In orders

    If Order.Value.Name = name Then

        orderFound = True
        Return

    End If

Next

如果你想对用户好,不要区分大小写:

If Order.Value.Name.ToUpper() = name.ToUpper() Then