Alice
Alice

Reputation: 95

List of ArrayList 3 Dimensions to store 3 Values (Error The given key was not present in the dictionary.)

Trying to make an List or even an ArrayList that has 3 columns which I can dynamically add and retrieve elements in VB.Net.

Which I can add element like this : mylist.add(one).(two).(three)

don't know if it's possible??

Please can you help

Below is my code

I'm getting the error saying (The given key was not present in the dictionary.)

    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

Upvotes: 0

Views: 298

Answers (1)

David Wilson
David Wilson

Reputation: 4439

Assuming that the data elements are all linked to one item, it sounds like you need a list of a structure.

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

Upvotes: 1

Related Questions