2017-08-14 13 views
0

VB.Netで要素を動的に追加および取得できる3列のリストまたはArrayListを作成しようとしています。 。私はこのような要素を追加することができますArrayListのリスト3次元3の値を格納する次元(エラー指定されたキーが辞書に存在しません)

:。それが可能だ場合mylist.add(1)(2)(3)

は知りません?

あなたは以下

を助けることができますが、私はというエラーを取得しています私のコード

です(指定したキーが辞書に存在しませんでした。)

そのデータを仮定し
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

実際には、1つではなく2つのアイテムが "値"辞書に追加されました。forループで "値"辞書を処理すると、コードは最初の "値"商品のみを含む商品"です。 – vintastic

答えて

1

要素はすべて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、名前、説明)を入力するだけです。 – Alice

+0

@ Alice I;追加のSubで回答を更新してください。私のためにうまくいくと思う。 –

+0

Davidに感謝します。私はまた、構造のリスト[ここ](https://stackoverflow.com/questions/45770046/vb-net-clearing-list-of-structure-works-but-get-duplicated-item-after-更新)。あなたは助けてください! – Alice

関連する問題