2015-05-15 169 views
-2

如何搜索我的列表? 我想在添加條形碼時驗證條形碼。List of(T)search vb.net

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) 

但它不工作。任何建議,將不勝感激。

+0

歡迎SO!嘗試比「它不工作」更具體一點。 – J0e3gan

+0

'Function(p)p.Barcode = 12345689'應該是幹什麼的? –

+0

你不能訪問他們需要成爲「朋友」或「公共」的私人成員。另外它被稱爲'm_barcode'。 – OneFineDay

回答

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是一個奇怪的類名。

0

由於您沒有在您的Set中進行任何數據驗證,我會建議使用自動屬性。

Sub Main() 
    Dim ItemsList As New List(Of Item2) 
    ItemsList.Add(New Item2 With {.Name = "Toy Truck", .Barcode = 12345689, .Price = "10.00", .Quinety = 100}) 
    ItemsList.Add(New Item2 With {.Name = "Bicycle", .Barcode = 98754152, .Price = "100.00", .Quinety = 50}) 

    Dim foundItem = ItemsList.Find(Function(i) i.Barcode = 12345689) 
    If Not foundItem Is Nothing Then 
     Console.WriteLine(foundItem.Name) 
    Else 
     Console.WriteLine("Item not found") 
    End If 

    foundItem = ItemsList.Find(Function(i) i.Barcode = 1111) 
    If Not foundItem Is Nothing Then 
     Console.WriteLine(foundItem.Name) 
    Else 
     Console.WriteLine("Item not found") 
    End If 

    Console.ReadLine() 
End Sub 

Public Class Item2 
    Public Property Name As String 
    Public Property Barcode As Integer 
    Public Property Price As Double 
    Public Property Quinety As Integer 
End Class 

結果:

enter image description here

相關問題