我有一些两列数据,我从文件中读取并放入列表中,然后按字母顺序排序。在列表中的二进制搜索和引用列表的特定列
//文件
鹰嘴豆泥,0.75
辣椒,0.50
Tabouli,1.25
Tzatziki,0.50
//声明的变量和公共属性
Dim extraList As List(Of extra)
Public Class extra
Implements IComparable(Of extra)
Public Property Name As String
Public Property Price As Decimal
'Public Property extraList As List(Of extra)
Public Function CompareTo(other As extra) As Integer Implements IComparable(Of extra).CompareTo
Return Me.Name.CompareTo(other.Name)
End Function
End Class
//将数据置于列表并对其排序,
Sub Get_Extras_List()
'Reads the extras file and puts the information into a list, splitting the name of the extra and the price into separate columns
Dim allExtras = From line In System.IO.File.ReadLines("C:\Users\ExtrasList.txt")
Let Columns = line.Split(","c)
Where Columns.Length = 2
Let Price = Decimal.Parse(Columns(1).Trim())
Let Name = Columns(0).Trim()
Select New extra With {.Name = Name, .Price = Price}
extraList = allExtras.ToList()
'Sort the list alphabetically
extraList.Sort()
End Sub
现在我需要编写一个方法,它允许用户键入一个额外的搜索使用二进制搜索,看是否存在它。到目前为止,我已经尝试过,但它不起作用,即使它如何让它返回true或false值? (如果它的存在与否?)
Sub Search_Extras_List()
Dim strSearchExtra As String = Console.ReadLine()
Console.WriteLine(vbLf & "BinarySearch for '{0}':", strSearchExtra)
Dim index As Integer =
List(Of extra).BinarySearch(extraList.Name, strSearchExtra)
End Sub
最后,我必须让用户选择的演员之一,然后它的价格增加的总价格。我如何参考价格? extraList.Price? extra.Price?等等。