Vb.net 遍历多维数组以查找与一维数组匹配的元素

Vb.net 遍历多维数组以查找与一维数组匹配的元素,vb.net,Vb.net,我有一个3个数组(饮料、食物、沙漠),还有一个名为prices的多维数组,用于存储菜单上8个项目的价格。我有一个子过程处理将项目从列表框中的数组转移到下面的文本框,但考虑到它是多维的,我在价格方面遇到了问题。例如,您最好将菜单和每个项目都看作对象,菜单包含菜单项列表,菜单项是具有名称、类型(饮料/主菜/甜点/配菜)、说明和价格等属性的对象 因此,您最好创建一个包含要存储在其中的所有菜单项列表的菜单对象 因此,首先要定义菜单项是什么。。类似下面的代码。您还需要定义它是什么类型的项目。这是由Enum

我有一个3个数组(饮料、食物、沙漠),还有一个名为prices的多维数组,用于存储菜单上8个项目的价格。我有一个子过程处理将项目从列表框中的数组转移到下面的文本框,但考虑到它是多维的,我在价格方面遇到了问题。例如,您最好将菜单和每个项目都看作对象,菜单包含菜单项列表,菜单项是具有名称、类型(饮料/主菜/甜点/配菜)、说明和价格等属性的对象

因此,您最好创建一个包含要存储在其中的所有菜单项列表的菜单对象

因此,首先要定义菜单项是什么。。类似下面的代码。您还需要定义它是什么类型的项目。这是由
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) 
因此,在某个时候,您希望将这些项目放在相应的列表框中。你可以用这艘潜艇

Private Sub UpdateList(listBoxToUpdate As ListBox, category As FoodMenuItem.ItemType)
    listBoxToUpdate.Items.Clear()
    listBoxToUpdate.Items.AddRange((From item As FoodMenuItem In FoodItems Where item.Category = category Select item).ToArray)
    listBoxToUpdate.DisplayMember = "Name"
End Sub
然后像这样使用它,假设ListBoxDrinks、ListBoxMainCourse、ListBoxSummit的列表框名称

UpdateList(ListBoxDrinks, FoodMenuItem.ItemType.Drink)
UpdateList(ListBoxMainCourse, FoodMenuItem.ItemType.MainCourse)
UpdateList(ListBoxDessert, FoodMenuItem.ItemType.Dessert)
当您在say
ListBoxDiverses
中单击一个项目时,您将获得该项目,并将其名称放在一个文本框中,将其价格放在另一个文本框中,如下所示

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”)
将文本格式化为您的当地货币。

您为什么要将项目及其价格存储在不同的数组中?使用类和集合意味着永远不必搜索价格。请阅读并使用同上的@Proputinix。还有,杰克,拿出你的密码。