2017-08-14 47 views
0

試圖製作一個列表,甚至是一個ArrayList,它有3列,我可以動態地添加和檢索VB.Net中的元素。列表ArrayList 3要存儲的維度3值(錯誤給定的鍵不存在於字典中。)

,我可以添加這樣的元素:。mylist.add(之一)(二)(三級)

不知道是否有可能?

請你能幫助

下面是我的代碼

我得到的錯誤說(給定的關鍵是不存在的字典。)

Public values As New List(Of Dictionary(Of String, String))() 


     values.Add(New Dictionary(Of String, String)() From { _ 
     {"product", TextBox1.Text.Trim} _ 
     }) 


     values.Add(New Dictionary(Of String, String)() From { _ 
     {"description", TextBox2.Text.Trim} _ 
     }) 


     Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click 

     For Each value As Dictionary(Of String, String) In values 

      Dim product As String = value("product") 
      Dim description As String = value("description") 

      MsgBox(product & " - " & description) 



     Next 

    End Sub 
+0

實際上,您已將2個項目添加到您的「值」字典而不是1個。當您在for循環中處理「值」字典時,代碼正在尋找第一個「值」 「只包含」產品「的項目。 – vintastic

回答

1

假設數據元素都鏈接到一個項目,這聽起來像你需要一個結構列表。

Public Class Form1 

    Structure Product 
     Dim Id As Integer 
     Dim Name As String 
     Dim Description As String 
    End Structure 

    Dim Products As New List(Of Product) 

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load 
     Dim newproduct As New Product With {.Id = 54835, .Name = "My Greatest Product", .Description = "Blue,Large and Shiny!"} 
     Products.Add(newproduct) 
     MessageBox.Show(GetProduct(54385).Name & " - " & GetProduct(54385).Description) 
    End Sub 

    Private Function GetProduct(searchId As Integer) As Product 
     Return Products.Find(Function(x) x.Id = searchId) 
    End Function 

    Private Sub DeleteProduct(searchId As Integer) 
     Dim productToDelete As Product = GetProduct(searchId) 
     If Not IsNothing(productToDelete.Name) Then 
      Products.Remove(productToDelete) 
     Else 
      MessageBox.Show("Product: " & searchId & " does not exist") 
     End If 
    End Sub 

End Class 
+0

謝謝David Wilson。那麼,我如何刪除一行/索引/項目(如果我只輸入一個** ID **),我只是想要輸入完整的信息(id,Name,Description)。 – Alice

+0

@愛麗絲我用另一個小組更新了答案。似乎爲我工作得很好。 –

+0

謝謝大衛。我還有清除結構列表[這裏]的另一個問題(https://stackoverflow.com/questions/45770046/vb-net-clearing-list-of-structure-works-but-get-get-liced-item-after-更新)。請你幫忙! – Alice

相關問題