I am writing a program to read many lines from a textfile (line by line), each line contains 2 numbers separated by comma ",", first part is latitude and second is longitude. I am taking each line and storing it in a listbox.
I have a listbox in which each item includes two numbers (latitude and longitude) separated by comma "," (ex.: 12,64).
I want to create two array, first array containing latitudes and the second containing longitudes.
First of all, you should define appropriate data structures. Latitude values are usually numbers. So, a Double()
seems appropriate. Then:
Dim lat1(ListBox1.Items.Count - 2) As Double
Dim lat2(ListBox1.Items.Count - 2) As Double
For i As Integer = 0 To ListBox1.Items.Count - 1
Dim itemString = ListBox1.Items(i)
Dim splitItemString = itemString.Split(","c)
'Use Parse only if you're absolutely sure that this works.
'Use TryParse otherwise
Dim lat = Double.Parse(splitItemString(0))
Dim lon = Double.Parse(splitItemString(1))
'Now we can fill the arrays
If i < ListBox1.Items.Count - 1 Then lat1(i) = lat
If i > 0 Then lat2(i - 1) = lat
Next
However, just calculating distances barely requires two arrays. Instead, you should fill a single array (maybe even with a reasonable Struct
consisting of latitude and longitude values) and then:
For i As Integer = 0 To Coordinates.Count - 1
'Calculate distance between Coordinates(i) and Coordinates(i + 1)
Next
I'm not sure if I got your original question right, but I hope these blocks would be helpful. This code would redistribute your listbox's Items in 2 arrays of Double:
Dim allLatitudes(0) As Double
Dim allLongtitudes(0) As Double
Dim counter As Integer
For Each item In ListBox1.Items
Dim tempstr = item.ToString
Dim value = tempstr.Split(","c)
Dim lat = Double.Parse(value(0)) ' to convert it to numeric
Dim lon = Double.Parse(value(1)) ' to convert it to numeric
ReDim Preserve allLatitudes(counter)
allLatitudes(UBound(allLatitudes)) = lat
ReDim Preserve allLongtitudes(counter)
allLongtitudes(UBound(allLongtitudes)) = lon
counter = counter + 1
Next
In case you want to get for each item 2 arrays, one containing all values except last one, second containing all values except first one, than the code would be:
For Each item In ListBox1.Items
Dim tempstr = item.ToString
Dim value = tempstr.Split(","c)
Dim lat = value.Take(value.Length - 1).ToArray()
Dim lon = value.Skip(1).ToArray()
Next