我有一个3数组(饮料,食物,沙漠),以及一个名为价格的多维数组,它存储菜单上8个项目的价格。我有一个子程序,处理从列表框中的项目转移到下面的文本框,但考虑到它是多维的价格有问题遍历多维数组以查找与单维数组匹配的元素
-1
A
回答
0
你会更好地考虑你的菜单和每个项目作为对象例如,菜单包含菜单项目列表,菜单项目是具有属性的对象,如名称,类型(饮料/主菜/甜点/面),描述和价格。
所以你可能会更好地创建一个Menu对象,其中包含所有要存储在其中的菜单项列表。
所以,首先你要定义你的MenuItems是什么......类似下面的代码。您还需要定义它是什么类型的项目。这由Enum..End Enum
块完成。
Friend Class FoodMenuItem
Enum ItemType
Drink
MainCourse
Dessert
Side
End Enum
Public Property Name As String
Public Property Price As Decimal
Public Property Catagory As ItemType
Public Property Description As String
Public Sub New(newName As String, newPrice As Decimal, newCatagory As ItemType, newDescription As String)
Name = newName
Price = newPrice
Catagory = newCatagory
Description = newDescription
End Sub
End Class
接下来,你要创建一个菜单,是一个简单的菜单项列表
Dim FoodItems As New List(Of FoodMenuItem)
到食品项目添加到您需要创建它,并把它添加到您的列表
列表Dim itemtoAdd1 As New FoodMenuItem("Pasta", 4.95D, FoodMenuItem.ItemType.MainCourse, "Delicious pasta with parmesan cheese")
Dim itemtoadd2 As New FoodMenuItem("Beer", 3D, FoodMenuItem.ItemType.Drink, "Cool and refreshing")
Dim itemtoadd3 As New FoodMenuItem("Red Wine", 3.3D, FoodMenuItem.ItemType.Drink, "Fruity")
Dim itemtoadd4 As New FoodMenuItem("White Wine", 3.5D, FoodMenuItem.ItemType.Drink, "Dry")
Dim itemtoadd5 As New FoodMenuItem("Salad", 4.5D, FoodMenuItem.ItemType.MainCourse, "Crisp Salad with iceberg lettuce, tomatoes and beetroot")
Dim itemtoadd6 As New FoodMenuItem("Chocolate Fudge Cake", 4.25D, FoodMenuItem.ItemType.Dessert, "Indulgent fudge cake with fresh whipped cream")
Dim itemtoadd7 As New FoodMenuItem("Ice Cream", 4.5D, FoodMenuItem.ItemType.Dessert, "In a choice of flavours with the topping of your choice")
FoodItems.Add(itemtoAdd1)
FoodItems.Add(itemtoadd2)
FoodItems.Add(itemtoadd3)
FoodItems.Add(itemtoadd4)
FoodItems.Add(itemtoadd5)
FoodItems.Add(itemtoadd6)
FoodItems.Add(itemtoadd7)
因此,在某些时候,您希望将这些项目放在适当的列表框中。您可以使用此子..
而且使用这样的,假设ListBoxDrinks,ListBoxMainCourse的列表框中的名称,ListBoxDessert
UpdateList(ListBoxDrinks, FoodMenuItem.ItemType.Drink)
UpdateList(ListBoxMainCourse, FoodMenuItem.ItemType.MainCourse)
UpdateList(ListBoxDessert, FoodMenuItem.ItemType.Dessert)
当您单击说ListBoxDrinks
一个项目,你会得到该项目,并将其名称放在一个文本框中,并将其价格放在另一个文本框中,如下所示。
Private Sub ListBoxDrinks_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListBoxDrinks.SelectedIndexChanged
Dim selectedDrink As FoodMenuItem = CType(ListBoxDrinks.SelectedItem, FoodMenuItem)
TextBoxItemName.Text = selectedDrink.Name
TextBoxItemPrice.Text = selectedDrink.Price.ToString("C")
End Sub
应该这样做。顺便提一句ToString("C")
在最后一行将文本格式为您的本地货币。
相关问题
- 1. 遍历PHP多维数组
- 2. 遍历多维数组
- 3. numpy遍历多维数组
- 4. 遍历多维数组
- 5. PHP:一维数组到多维数组与元素的乘法
- 6. 遍历numpy多维数组的一列?
- 7. 遍历大的JavaScript多维数组
- 8. 在多维数组与匹配PHP删除元素
- 9. 如何遍历多维JSON数组?
- 10. 二维数组查找char元素(s)
- 11. 多维数组遍历与Symfony表单验证
- 12. php echo单元素多维数组
- 13. Javascript - 组合多维数组的元素
- 14. Javascript/jQuery填充多维数组...数组与数组元素
- 15. JQuery循环遍历元素的二维数组
- 16. 多维数组与一维
- 17. JS。遍历多维数组来计算每列中元素的出现次数
- 18. 遍历Python中的二维数组?
- 19. 遍历二维数组中的每个单元格
- 20. 计数多维数组元素VB.NET
- 21. 与多维数组
- 22. jQuery:如何遍历一组元素,找到与其他数组中的值匹配的元素?
- 23. 遍历二维数组错误
- 24. 遍历二维数组,在语法
- 25. 有效地遍历二维数组
- 26. 使用PHP查找多维数组中元素的位置
- 27. 在C编程中查找多维数组中的元素
- 28. 搜索多维数组匹配
- 29. 遍历多维数组输出单个字符
- 30. 如何遍历多维数组并相应地执行查询?
你为什么要在不同的阵列中存储一个物品和它的价格?使用它们的类和集合意味着不必搜索价格。请阅读[问]并参加[旅游] – Plutonix
同上@Plutonix。另外,杰克,显示你的代码。 –