Home > Net >  How do you nest dictionaries and point to specific keys in another dictionary
How do you nest dictionaries and point to specific keys in another dictionary

Time:02-16

So to begin I have created 3 dictionaries properly nested within each other, I then add keys/values to the gdicRawItems dictionary without issue. Now I am adding a key to the gdicPreppedItems dictionary but I need it to point to multiple key/values in gdicRawItems, what is the proper syntax to do so? Essentially when I view the PreppedItems dictionary and look at the key "Cheesburger" I want to see the values as Beef Patty, Bun, and Cheese Slice

Public Class frmDishDesigner
Public gdicRawItems As New SortedDictionary(Of String, String)
Public gdicPreppedItems As New SortedDictionary(Of String, SortedDictionary(Of String, String))
Public gdicDishes As New SortedDictionary(Of String, SortedDictionary(Of String, SortedDictionary(Of String, String)))

Public Sub Main()

End Sub
Public Sub frmDishDesigner_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    gdicRawItems.Add("Beef Patty", "Beef Patty")
    gdicRawItems.Add("Cheese Slice", "Cheese Slice")
    gdicRawItems.Add("Bun", "Bun")
    gdicRawItems.Add("Water", "Water")
    gdicRawItems.Add("Potato", "Potato")
    gdicRawItems.Add("Mushroom", "Mushroom")
    gdicRawItems.Add("Plate", "Plate")

    gdicPreppedItems.Add("Cheesburger", (gdicRawItems.Item(Bun)))
    gdicPreppedItems.Add("

CodePudding user response:

We're approaching an example of when a database is necessary. You can use classes and lists, but this is being built at design time, and if you ever want to change the recipe you will need to recompile your code:

Public Class PreppedItem
    Public Property Name As String
    Public Property RawIngredients As New List(Of String)
End Class

Public Class Dish
    Public Property Name As String
    Public Property PreppedItems As New List(Of PreppedItem)
End Class
Dim cheeseburgerDish As New Dish() With {
    .Name = "Cheeseburger Dish",
    .PreppedItems = New List(Of PreppedItem) From {
        New PreppedItem() With {.Name = "Cheeseburger",
            .RawIngredients = New List(Of String) From {"Beef Patty", "Cheese Slice", "Bun"}
        },
        New PreppedItem() With {.Name = "Fries",
            .RawIngredients = New List(Of String) From {"Potato"}
        }
    }
}

Using your types, you can create one dish, but it will break when you try to add another dish. Because the dish holds the prepped items dictionary, but if you add to that for the next dish, then it will also add for the first dish. So it's impossible with your design.

gdicRawItems.Add("Beef Patty", "Beef Patty")
gdicRawItems.Add("Cheese Slice", "Cheese Slice")
gdicRawItems.Add("Bun", "Bun")
gdicRawItems.Add("Water", "Water")
gdicRawItems.Add("Potato", "Potato")
gdicRawItems.Add("Mushroom", "Mushroom")
gdicRawItems.Add("Plate", "Plate")

Dim cheeseBurger = New SortedDictionary(Of String, String)
cheeseBurger.Add("Bun", gdicRawItems("Bun"))
cheeseBurger.Add("Beef Patty", gdicRawItems("Beef Patty"))
cheeseBurger.Add("Cheese Slice", gdicRawItems("Cheese Slice"))

Dim fries = New SortedDictionary(Of String, String)
fries.Add("Potato", gdicRawItems("Potato"))

gdicPreppedItems.Add("Cheeseburger", cheeseBurger)
gdicPreppedItems.Add("Fries", fries)

gdicDishes.Add("Cheeseburger Dish", gdicPreppedItems)

If using a database, you can design it as such. Gives you an idea of what you would want to do.

Ingredient

ID Name
1 Beef Patty
2 Cheese Slice
3 Bun
4 Water
5 Potato
6 Mushroom
7 Plate

PreppedItem

ID Name
1 Cheeseburger
2 Fries
3 Mushroom Burger
4 Hamburger

PreppedItemIngredient

ID PreppedItemID IngredientID Quantity
1 1 1 1
2 1 2 1
3 1 3 2
4 2 5 1
5 3 1 1
6 3 6 1
7 3 3 2
8 4 1 1
9 4 3 2

Plate

ID Name
1 Cheeseburger Plate
2 Mushroom Burger Plate

PlatePreppedItem

ID PlateID PreppedItemID
1 1 1
2 1 2
3 2 3
4 2 2

Now to change a recipe you just change the database, not your code. There is a lot more you could do here as to quantities and units of measure, but this is a basic description.

CodePudding user response:

You can't do it the way you want. You must add a dictionary, but you only have one dictionary for all raw ingredients.

gdicPreppedItems.Add("Cheesburger", gdicRawItems)

This will add all raw items.

  • Related