遇到一个小问题。我不知道如何搜索一个数组,所以我可以从其他2个数组中提取相同的数组。我知道如何测试很多东西,这样就不会有问题了。
这个项目的最终结果是用户将放置他们愿意支付汽车品牌的金额,页面将显示数据。但是,我不知道如何搜索carArray()
来查找索引号并使用该索引号来查找其他内容。我确实发现了一些事情(有点)早些时候做了但我不知道如何修改它以保持索引号为int并用它来搜索和显示其他数组。
我将来会在以后的项目中使用它。
Public Class paymentPage
Private Sub car_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles car.TextChanged
End Sub
Private Sub price_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles price.TextChanged
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim carArray() As String = {"Ford", "Chevy", "Mazda"}
Dim sellAmount() As Decimal = {32700, 35625, 24780}
Dim leaseAmount() As Decimal = {425, 505, 385}
End Sub
End Class
答案 0 :(得分:2)
为什么不把它变成一个类对象?以后更容易重复使用。
Public Class Car
Public Property Make As String
Public Property Value As Double
Public Property Lease As Double
End Class
然后收集它们:
Private cars As New List(Of Car)
cars.Add(New Car With {.Make = "Ford", .Value = 32700, .Lease = 425})
cars.Add(New Car With {.Make = "Chevy", .Value = 35625, .Lease = 505})
cars.Add(New Car With {.Make = "Mazda", .Value = 24780, .Lease = 385})
根据您的要求:
Private Function getIndexByName(make As string) As Integer
Dim result As Integer = -1
For i As Integer = 0 To carArray.Length -1
If carArray(i) = make Then
result = i
Exit for
End If
Next
Return Result
End Function
用法:
Dim mazdalease = leaseAmt(getIndexByName("Mazda"))
答案 1 :(得分:0)
Dim cars as new List(Of Car)({car1,car2,car3})
Dim indexOfCar2 = Array.IndexOf(cars.ToArray(),car2)
由于它的污垢很容易转换为数组,因此您可以使用内置函数。请记住,您需要覆盖GetHash和Equals才能使其正常工作。