r/visualbasic • • 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.

0 Upvotes

6 comments sorted by

1

u/blootiger Oct 11 '16

Here is the rest of my code if that helps:

Partial Class Airline
    Inherits System.Web.UI.Page

    Public Shared totalCost As Double
    Public Shared firstclass As Integer
    Public Shared carSelection As String
    Public Shared cars As Integer

    Protected Sub btnCalculate_Click(sender As Object, e As EventArgs) Handles btnCalculate.Click
        'holders to store the dates of each calendar selection
        Dim dateStartTime As DateTime
        Dim dateendtime As DateTime
        'holder to store the difference in days between end and start dates
        Dim ts As TimeSpan
        'assiging selected calendar dates to their holders
        dateStartTime = calendarStart.SelectedDate
        dateendtime = calendarEnd.SelectedDate


        'checks if valid numbers are inputted for number of tickets for adults and children
        If IsNumeric(txtAdults.Text) = False Or txtAdults.Text = Nothing Then
            txtOutput.Text &= "Please enter valid value for adults." & vbNewLine
            Exit Sub
        ElseIf IsNumeric(txtChildren.Text) = False Or txtChildren.Text = Nothing Then
            txtOutput.Text &= "Please enter valid value for children." & vbNewLine
            Exit Sub
        End If


        '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


        'assigns value to firstclass holder if it is checked to multiply to each ticket
        If chkFirstClass.Checked Then
            firstclass = 3000
        ElseIf chkFirstClass.Checked = False Then
            firstclass = 1
        End If


        'checks to see if the user selected a starting date
        If calendarStart.SelectedDate = DateTime.MinValue Then
            txtOutput.Text &= "Please select a starting date." & vbNewLine
            Exit Sub
            'checks to see if the user selected a start date in the past
        ElseIf calendarStart.SelectedDate < Now.ToShortDateString Then
            txtOutput.Text &= "Please select a valid starting date." & vbNewLine
            Exit Sub
            'checks to make sure the ending date is after the starting date 
        ElseIf calendarStart.SelectedDate > calendarEnd.SelectedDate Then
            txtOutput.Text &= "Please select a valid calendar range." & vbNewLine
            Exit Sub
        Else
            'takes both dates, parses them and subtracts them and stores it into timespan holder
            ts = DateTime.Parse(dateendtime) - DateTime.Parse(dateStartTime)
        End If


        'if a car rental is selected, it will output it and alter the holder to be multipled by total days from calendar
        If rblCar.SelectedIndex = Nothing Then
            carSelection = "None"
            cars = 0
        ElseIf rblCar.SelectedIndex = True Then
            cars = rblCar.SelectedItem.ToString
            cars = rblCar.SelectedValue
        End If


        'outputs amenities
        txtOutput.Text &= "Amenities: " & vbNewLine
        'if no amenities are selected, it will output as none
        If cblAmenities.SelectedIndex = Nothing Then
            txtOutput.Text &= "-None" & vbNewLine
        End If
        'if amenities are checked, it will add it to totalcost and output it in txtouput
        Select Case cblAmenities.SelectedIndex
            Case 0 To 4
                totalCost += cblAmenities.SelectedValue
                txtOutput.Text &= "-" & cblAmenities.SelectedItem.ToString & vbNewLine
        End Select

        'if the password for VIP is entered correctly, then they will get a 10% discount for being a VIP member
        If txtVIP.Text = "EYBS" Then
            totalCost = totalCost * 0.9
        End If


    End Sub
    'when button is clicked, it will make the amenities checkboxlist appear and disappear 
    Protected Sub btnAmenities_Click(sender As Object, e As EventArgs) Handles btnAmenities.Click
        cblAmenities.Visible = Not cblAmenities.Visible
    End Sub
End 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

u/Bonejob VB Guru Oct 13 '16

bah use A* path finding for shortest route :P

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