I have a list of specific class.
In this list , a position class is included.
And that position class includes X and Y coordinates.
I have Current Coordinates and the coordinates in list .
I want to calculate the distance for each item in List and find which item has minimal distance.
Here is my code :
For Each item As ITEMX In xHandle.ItemList
Dim CurrX As Integer = txt_TrainX.Text
Dim CurrY As Integer = txt_TrainY.Text
Dim NextX As Integer = item.Position.x
Dim NextY As Integer = item.Position.y
Dim distance As Integer = DistanceBetween(CurrX, CurrY, NextX, NextY)
Next
so distance is the distance between my coordinates and the item.
I calculate it for each item in list but how do i find the minimal one ?
Thank you.
Create a variable for the minimal value, and check it against each value in the loop.
You should parse the text from the controls outside the loop, it’s a waste to do that over and over again inside the loop. You should also turn on strict mode so that you don’t do implicit conversions that shouldn’t be implicit.