因此,首先我創建了 3 個正確嵌套的字典,然后我將鍵/值添加到 gdicRawItems 字典中沒有問題。現在我正在向 gdicPreppedItems 字典添加一個鍵,但我需要它指向 gdicRawItems 中的多個鍵/值,這樣做的正確語法是什么?基本上,當我查看 PreppedItems 字典并查看鍵“Cheesburger”時,我希望看到的值是 Beef Patty、Bun 和 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("
uj5u.com熱心網友回復:
我們正在接近一個何時需要資料庫的示例。您可以使用類和串列,但這是在設計時構建的,如果您想更改配方,則需要重新編譯代碼:
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"}
}
}
}
使用您的型別,您可以創建一道菜,但當您嘗試添加另一道菜時它會中斷。因為這道菜包含準備好的專案字典,但如果你為下一道菜添加該字典,那么它也會為第一道菜添加。所以你的設計是不可能的。
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)
如果使用資料庫,您可以這樣設計它。讓您了解您想要做什么。
成分
| ID | 名稱 |
|---|---|
| 1 | 牛肉餅 |
| 2 | 切片芝士 |
| 3 | 包子 |
| 4 | 水 |
| 5 | 土豆 |
| 6 | 蘑菇 |
| 7 | 盤子 |
準備專案
| ID | 名稱 |
|---|---|
| 1 | 芝士漢堡 |
| 2 | 薯條 |
| 3 | 蘑菇漢堡 |
| 4 | 漢堡包 |
PreppedItemIngredient
| ID | PreppedItemID | 成分ID | 數量 |
|---|---|---|---|
| 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 |
盤子
| ID | 名稱 |
|---|---|
| 1 | 芝士漢堡盤 |
| 2 | 蘑菇漢堡盤 |
PlatePreppedItem
| ID | 板號 | PreppedItemID |
|---|---|---|
| 1 | 1 | 1 |
| 2 | 1 | 2 |
| 3 | 2 | 3 |
| 4 | 2 | 2 |
現在要更改配方,您只需更改資料庫,而不是您的代碼。關于數量和度量單位,您可以在這里做更多的事情,但這是一個基本描述。
uj5u.com熱心網友回復:
你不能按照你想要的方式去做。您必須添加一本字典,但您只有一本包含所有原材料的字典。
gdicPreppedItems.Add("Cheesburger", gdicRawItems)
這將添加所有原始專案。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/426192.html
標籤:VB.net
上一篇:vb.net源陣列型別不能分配給Enum上的目標陣列型別
下一篇:Unix時間戳到HH-mm-ss
