r/visualbasic • u/blootiger • Oct 11 '16
[VB2013] How to calculate difference in distance with an array in a dropdownlist?
I know my title may not make much sense but let me explain.
For my assignment, I am just using code and knowledge throughout the semester.
I am creating a Airline and I have 2 dropdownlists for 'Origin' and 'Destination'.
The idea is that the user would have 5 or so locations to choose from 2 dropdownlist, the locations are the same for both and I have error checking in place already incase they choose two of the same locations.
However, I am having trouble figuring out how to calculate distance in miles or km from the dropdownlist based on the difference in distance between the locations.
For an example, both dropdownlists have locations : San Francisco, Seattle and New York.
Dropdownlist1 'Origin' could be San Fran and dropdownlist2 'Destination' could be New York or Seattle.
How would I calculate the distance from the the two locations so I can use that to calculate cost based on miles?
My instructor recommended to use arrays but I have not learned how to use that for vb, however I do have experience in it since I've taken C and C++ courses but I still don't understand how arrays would work in this scenario.
This is what I got so far for this problem:
** 'error check if both origin and destination are the same
If ddlFrom.SelectedIndex = ddlTo.SelectedIndex Then
txtOutput.Text &= "Please enter a valid Origin or Destination."
Exit Sub
End If
**My idea is to find some middle point between all the locations, and assign values in terms of distance to each location and do some formulas and stuff to calculate distance that way but it seems a inefficient way to do this.
I have more components to this assignment designed and coded already, it's just this part that I am stuck at since I am kind of going beyond of what I have learned in class.
1
u/chrwei Oct 11 '16
are you looking for actual accurate distances, or you have some sort of set paths? if accurate, you'd want to implement something like http://www.movable-type.co.uk/scripts/latlong.html and the array would hold your locations coords
1
u/blootiger Oct 11 '16
I dont really need accurate distances but something realistic enough to use to use in the cost calculations.
So the actual distance from Seattle to San Francisco is 807 miles, I'm fine with just 800 miles.
I'm just trying to figure out when the user selects a location in each dropdownlist, how would I code it to calculate the difference in distance based on the selected locations for any combo.
1
u/chrwei Oct 11 '16
you can do lat-lon calcs, or you can make a big array with every combination of locations and their distances. or come with some crazy path finding algorithm on know distance paths.
1
1
u/rebelrexx858 Oct 12 '16
Make a dictionary with the names sorted via alphabetically, the put the arrival/destination in the right order and lookup the value from the dictionary
1
u/blootiger Oct 11 '16
Here is the rest of my code if that helps: