screw you
screw you

Reputation: 141

Need help searching an array... very hard for me

Got a small problem. I don't know how to search one array so I can pull that same array number from the other 2 arrays. I know how to test for a lot of stuff so that won't be a problem.

The end result on this project is the user will place the amount they are willing to pay for a make of the car and the page will display the data. HOWEVER I don't know how to search the carArray() to find the index number and use that index number to find the other stuff. I did find something that did this (somewhat) earlier but I don't know how to modify it for me to keep that index number as a int and use it to search and display the other arrays.

I will need this in future projects later.

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

Upvotes: 0

Views: 119

Answers (2)

jcwrequests
jcwrequests

Reputation: 1130

Dim cars as new List(Of Car)({car1,car2,car3})

Dim indexOfCar2 = Array.IndexOf(cars.ToArray(),car2)

Since its dirt simple to convert to an array then you can use the built in function. Keep in mind that you need to override GetHash and Equals to get this to work properly.

Upvotes: 0

OneFineDay
OneFineDay

Reputation: 9024

Why not make this a class object? Easier to reuse later.

Public Class Car
  Public Property Make As String
  Public Property Value As Double
  Public Property Lease As Double
End Class

Then make a collection of them:

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})

For your requirements:

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

Usage:

Dim mazdalease = leaseAmt(getIndexByName("Mazda"))

Upvotes: 2

Related Questions