2017-08-14 37 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

相关问题