列表(T)搜索vb.net

时间:2015-05-15 20:26:37

标签: vb.net visual-studio list

如何搜索我的列表? 我想在添加条形码时验证条形码。

Public Class item2
 Private m_name As String
 Private m_barcode As Integer
 Private m_Price As Double
 Private m_Quinety As Integer
 Public Property Name() As String
    Get
        Return m_name
    End Get
    Set(value As String)
        m_name = value
    End Set
End Property
Public Property Barcode() As Integer
    Get
        Return m_barcode
    End Get
    Set(value As Integer)
        m_barcode = value
    End Set
End Property
Public Property Price() As Double
    Get
        Return m_Price
    End Get
    Set(value As Double)
        m_Price = value
    End Set
End Property
Public Property Quinety() As Integer
    Get
        Return m_Quinety
    End Get
    Set(value As Integer)
        m_Quinety = value
    End Set
End Property
Dim ItemsList As New List(Of item2)()
End class

我一直在网上找到类似这样的东西:

dim zz as item2 = ItemsList.Find(Function(p) p.Barcode = 12345689)

但它不起作用。任何建议将不胜感激。

2 个答案:

答案 0 :(得分:0)

由于您可以构建和运行代码,因此尝试访问私有成员(即m_barcode与公共Barcode成员)似乎不是问题。

然后,您在问题中显示的item2课程似乎不完整。添加一个公共Barcode属性来获取/设置私有m_barcode字段,但这里有一个简单的例子,它听起来像你想要的那样:

Module Module1

    Sub Main()
        Dim ItemsList As New List(Of item2)()
        Dim foo As New item2()
        Dim bar As New item2()

        foo.Barcode = 12345689
        ItemsList.Add(foo)

        bar.Barcode = 98654321
        ItemsList.Add(bar)

        Dim zz As item2 = ItemsList.Find(Function(p) p.Barcode = 12345689)

        Debug.WriteLine(zz.Barcode.ToString()) ' outputs 12345689
    End Sub

    Public Class item2
        Private m_name As String
        Private m_barcode As Integer
        Private m_Price As Double
        Private m_Quinety As Integer

        Public Property Barcode() As Integer
            Get
                Return m_barcode
            End Get
            Set(value As Integer)
                m_barcode = value
            End Set
        End Property
    End Class

End Module

请注意,您应该重新考虑命名和大小写约定,因为item2是一个奇怪的类名。

答案 1 :(得分:0)

由于您未在 @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.activity_main, menu); return true; } 中进行任何数据验证,我建议使用自动属性。

Set

结果:

enter image description here