Hello everyone its me again try to learn VB. I need help to make sure if my code needs to be modify. Here is the instruction:

1- The list box that contains the city names is not visible when the program runs.
lstCity.Visible = Not lstCity.Visible (still showing at run time)?

2- Determine and Display the Price:
(Your ticket from Boston to Los Angeles costs $) output

3- The base price for a ticket on a flight that does not leave the zone is $250, and the base price for any flight that travels from one zone to another is $400.

4- Additional Fees
Regardless of the base price, add 30% for a business class ticket, and add 60% for a first class ticket.

5- Zone Airports
Zone 1 (West Coast) LAX & SFO
Zone 2 (East Coast) BOS, BWI, JFK, MIA
Zone 3 (Midwest) AUS, DFW, ORD

Public Class frmTicket

    Private Sub btnDetermineRoute_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDetermineRoute.Click
        ' Declare variables
        Dim fromCity, toCity, fromName, toName As String
        Dim fromZone, toZone, ticketPrice As Double
        'Dim businessClass, firstClass As Double

        ' input from the list box
        fromCity = lstFrom.Text
        fromName = lstFrom.Text
        toCity = lstTo.Text
        toName = lstTo.Text

        ' Select From City
        If (fromCity = "LAX" Or fromCity = "SFO") Then
            fromZone = 1
        ElseIf (fromCity = "BOS" Or fromCity = "BWI" Or fromCity = "JFK" Or fromCity = "MIA") Then
            fromZone = 2
        Else
            fromZone = 3
        End If

        ' Select to City
        If (toCity = "LAX" Or toCity = "SFO") Then
            fromZone = 1
        ElseIf (toCity = "BOS" Or toCity = "BWI" Or toCity = "JFK" Or toCity = "MIA") Then
            fromZone = 2
        Else
            fromZone = 3
        End If

        ' Select From City
        If (fromCity = "AUS") Then
            fromName = "Austin"
        ElseIf (fromCity = "BOS") Then
            fromName = "Boston"
        ElseIf (fromCity = "BWI") Then
            fromName = "Baltimore"
        ElseIf (fromCity = "DFW") Then
            fromName = "Dallas"
        ElseIf (fromCity = "JFK") Then
            fromName = "New York"
        ElseIf (fromCity = "LAX") Then
            fromName = "Los Angeles"
        ElseIf (fromCity = "MIA") Then
            fromName = "Miami"
        ElseIf (fromCity = "ORD") Then
            fromName = "Chicago"
        ElseIf (fromCity = "SFO") Then
            fromName = "San Francisco"
        End If

        ' Select To City
        If (toCity = "AUS") Then
            toName = "Austin"
        ElseIf (toCity = "BOS") Then
            toName = "Boston"
        ElseIf (toCity = "BWI") Then
            toName = "Baltimore"
        ElseIf (toCity = "DFW") Then
            toName = "Dallas"
        ElseIf (toCity = "JFK") Then
            toName = "New York"
        ElseIf (toCity = "LAX") Then
            toName = "Los Angeles"
        ElseIf (toCity = "MIA") Then
            toName = "Miami"
        ElseIf (toCity = "ORD") Then
            toName = "Chicago"
        ElseIf (toCity = "SFO") Then
            toName = "San Francisco"
        End If

        ' Id the user has not chosen both a From city and a 
        ' To City - provide a warning to selected BOTH a
        ' from and ti city
        If (fromCity = "" Or toCity = "") Then
            lblWarning.Text = "You must selected both a from to city"
            lblWarning.Visible = True
        End If

        ' Function
        If (fromCity = toCity) Then
            lblWarning.Text = "You must selected different cities!"
            lblWarning.Visible = True
            txtMessage.Text = ""
        Else

            ' Calculate the Ticket Price
            If (fromZone = toZone) Then
                ticketPrice = 250
            Else
                ticketPrice = 400
            End If

            ' Seat class to modify ticket price
            If radBusinessClass.Checked Then
                ticketPrice = ticketPrice * 1.3
            ElseIf radFirstClass.Checked Then
                ticketPrice = ticketPrice * 1.6
            End If

            ' Display the route in the text box
            txtMessage.Text = "Your ticket from " + fromName + " to " + toName + " costs  " + CStr(FormatCurrency(ticketPrice))
            lblWarning.Visible = False
            ' Not visibke at run time
            lstCity.Visible = Not lstCity.Visible
        End If

    End Sub

End Class

Oddly enough you have the correct the code to make the lblWarning not visible, yet for the list you have coded lstCity.Visible = Not lstCity.Visible.
try:

lstCity.Visible = false

You never seem to sent toZone, only fromZone, so your check to see if they match can never match. And you have an error in your code:

If (fromZone = toZone) Then
ticketPrice = 250
Else
ticketPrice = 400
End If

You need to use == to check for equality. fromZone = toZone simply sets fromZone to be equal to toZone. It doesn't check if they are the same, it makes them the same.

Oddly enough you have the correct the code to make the lblWarning not visible, yet for the list you have coded lstCity.Visible = Not lstCity.Visible.
try:

lstCity.Visible = false

You never seem to sent toZone, only fromZone, so your check to see if they match can never match. And you have an error in your code:

If (fromZone = toZone) Then
ticketPrice = 250
Else
ticketPrice = 400
End If

You need to use == to check for equality. fromZone = toZone simply sets fromZone to be equal to toZone. It doesn't check if they are the same, it makes them the same.

Hi,

I did what you told me. But "==" is the operator for equality testing in C and Java syntax is that right.?

' Calculate the Ticket Price
            If (fromZone == toZone) Then
                ticketPrice = 250
            Else
                ticketPrice = 400
            End If

I get this error "Expression expected". Also I change suggested line to this.

lstCity.Visible = False

and still show at run time.
Any other idea will appreciated.
Thks.

If I might make a suggestion. Have you considered using a dictionary? It would simplify your code considerably. Here's an example:

Dim city As New Dictionary(Of String, String)

city("AUS") = "Austin"
city("BOS") = "Boston"
city("BWI") = "Baltimore"
city("DFW") = "Dallas"
city("JFK") = "New York"
city("LAX") = "Los Angeles"
city("MIA") = "Miami"
city("ORD") = "Chicago"
city("SFO") = "San Francisco"

'if you want to get the corresponding city for a given code you just index
'the dictionary with the code. For example

Dim code As String = "ORD"

If city.ContainsKey(code) Then
    MsgBox("The city corresponding to " & code & " is " & city(code))
Else
    MsgBox("There is no corresponding city for the code " & code)
End If

There are other useful properties/methods including ContainsValue. You can define the dictionary at the class level. You can add more codes and not have to add corresponding ElseIfs. You can define the types of values to include string arrays where the first element is the city and the second is the zone as in:

Dim city As New Dictionary(Of String, String())

city("AUS") = {"Austin", "3"}
city("BOS") = {"Boston", "2"}
city("BWI") = {"Baltimore", "2"}
city("DFW") = {"Dallas", "3"}
city("JFK") = {"New York", "2"}
city("LAX") = {"Los Angeles", "1"}
city("MIA") = {"Miami", "2"}
city("ORD") = {"Chicago", "3"}
city("SFO") = {"San Francisco", "1"}

'if you want to get the corresponding city for a given code you just index
'the dictionary with the code. For example

Dim code As String = "ORD"

If city.ContainsKey(code) Then
    MsgBox("The city corresponding to " & code & " is " & city(code)(0) & " zone " & city(code)(1))
Else
    MsgBox("There is no corresponding city for the code " & code)
End If

Just a suggestion. I have found dictionaries to be extremely useful and flexible.

If I might make a suggestion. Have you considered using a dictionary? It would simplify your code considerably. Here's an example:

Dim city As New Dictionary(Of String, String)

city("AUS") = "Austin"
city("BOS") = "Boston"
city("BWI") = "Baltimore"
city("DFW") = "Dallas"
city("JFK") = "New York"
city("LAX") = "Los Angeles"
city("MIA") = "Miami"
city("ORD") = "Chicago"
city("SFO") = "San Francisco"

'if you want to get the corresponding city for a given code you just index
'the dictionary with the code. For example

Dim code As String = "ORD"

If city.ContainsKey(code) Then
    MsgBox("The city corresponding to " & code & " is " & city(code))
Else
    MsgBox("There is no corresponding city for the code " & code)
End If

There are other useful properties/methods including ContainsValue. You can define the dictionary at the class level. You can add more codes and not have to add corresponding ElseIfs. You can define the types of values to include string arrays where the first element is the city and the second is the zone as in:

Dim city As New Dictionary(Of String, String())

city("AUS") = {"Austin", "3"}
city("BOS") = {"Boston", "2"}
city("BWI") = {"Baltimore", "2"}
city("DFW") = {"Dallas", "3"}
city("JFK") = {"New York", "2"}
city("LAX") = {"Los Angeles", "1"}
city("MIA") = {"Miami", "2"}
city("ORD") = {"Chicago", "3"}
city("SFO") = {"San Francisco", "1"}

'if you want to get the corresponding city for a given code you just index
'the dictionary with the code. For example

Dim code As String = "ORD"

If city.ContainsKey(code) Then
    MsgBox("The city corresponding to " & code & " is " & city(code)(0) & " zone " & city(code)(1))
Else
    MsgBox("There is no corresponding city for the code " & code)
End If

Just a suggestion. I have found dictionaries to be extremely useful and flexible.

Hey Jim,
Thank you, for your input. But I'm a little bit confused on using "dictionary" would you please help me to define the rest of this code..

'if you want to get the corresponding city for a given code you just index
'the dictionary with the code. For example

Dim code As String = "ORD"

When I start define I get an error because the variable code is already use.

--

Public Class frmTicket

    Private Sub btnDetermineRoute_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDetermineRoute.Click
        ' Declare variables
        Dim fromCity, toCity, fromName, toName As String
        Dim fromZone, toZone, ticketPrice As Double

        Dim city As New Dictionary(Of String, String())

        city("AUS") = {"Austin", "3"}
        city("BOS") = {"Boston", "2"}
        city("BWI") = {"Baltimore", "2"}
        city("DFW") = {"Dallas", "3"}
        city("JFK") = {"New York", "2"}
        city("LAX") = {"Los Angeles", "1"}
        city("MIA") = {"Miami", "2"}
        city("ORD") = {"Chicago", "3"}
        city("SFO") = {"San Francisco", "1"}

        'if you want to get the corresponding city for a given code you just index
        'the dictionary with the code. For example

        Dim code As String = "ORD"

        If city.ContainsKey(code) Then
            MsgBox("The city corresponding to " & code & " is " & city(code)(0) & " zone " & city(code)(1))
        Else
            MsgBox("There is no corresponding city for the code " & code)
        End If

        ' input from the list box
        fromCity = lstFrom.Text
        fromName = lstFrom.Text
        toCity = lstTo.Text
        toName = lstTo.Text

        ' Select From City
        If (fromCity = "AUS") Then
            fromName = "Austin"
        ElseIf (fromCity = "BOS") Then
            fromName = "Boston"
        ElseIf (fromCity = "BWI") Then
            fromName = "Baltimore"
        ElseIf (fromCity = "DFW") Then
            fromName = "Dallas"
        ElseIf (fromCity = "JFK") Then
            fromName = "New York"
        ElseIf (fromCity = "LAX") Then
            fromName = "Los Angeles"
        ElseIf (fromCity = "MIA") Then
            fromName = "Miami"
        ElseIf (fromCity = "ORD") Then
            fromName = "Chicago"
        ElseIf (fromCity = "SFO") Then
            fromName = "San Francisco"
        End If

        ' Select To City
        If (toCity = "AUS") Then
            toName = "Austin"
        ElseIf (toCity = "BOS") Then
            toName = "Boston"
        ElseIf (toCity = "BWI") Then
            toName = "Baltimore"
        ElseIf (toCity = "DFW") Then
            toName = "Dallas"
        ElseIf (toCity = "JFK") Then
            toName = "New York"
        ElseIf (toCity = "LAX") Then
            toName = "Los Angeles"
        ElseIf (toCity = "MIA") Then
            toName = "Miami"
        ElseIf (toCity = "ORD") Then
            toName = "Chicago"
        ElseIf (toCity = "SFO") Then
            toName = "San Francisco"
        End If

        ' Id the user has not chosen both a From city and a 
        ' To City - provide a warning to selected BOTH a
        ' from and ti city
        If (fromCity = "" Or toCity = "") Then
            lblWarning.Text = "You must selected both a from to city"
            lblWarning.Visible = True
        End If

        ' Function
        If (fromCity = toCity) Then
            lblWarning.Text = "You must selected different cities!"
            lblWarning.Visible = True
            txtMessage.Text = ""
        Else

            ' Calculate the Ticket Price
            If (fromZone = toZone) Then
                ticketPrice = 250
            Else
                ticketPrice = 400
            End If

            ' Seat class to modify ticket price
            If radBusinessClass.Checked Then
                ticketPrice = ticketPrice * 1.3
            ElseIf radFirstClass.Checked Then
                ticketPrice = ticketPrice * 1.6
            End If

            ' Display the route in the text box
            txtMessage.Text = "Your ticket from " + fromName + " to " + toName + " costs  " + CStr(FormatCurrency(ticketPrice))
            lblWarning.Visible = False
            ' Not visible at run time
            lstCity.Visible = False
        End If

    End Sub

End Class

Please where is mistake, maybe I don't see ...
thanks

The code I posted was an example of how to use a dictionary relating to your code. The actual variable "code" was just to show how to relate the airport code to the city and region. In actual use it would be

Public Class Form1

    Private city As New Dictionary(Of String, String())

    Private Sub btnDetermineRoute_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDetermineRoute.Click

        Dim fromCode As String = lstFrom.Text       'get airport code for source city
        Dim toCode As String = lstTo.Text           'get airport code for destination city

        'check that a valid airport code was entered for source and destination

        If Not city.ContainsKey(fromCode) Then
            lblWarning.Text = "You must select a source city"
            lblWarning.Visible = True
            Exit Sub
        End If

        If Not city.ContainsKey(toCode) Then
            lblWarning.Text = "You must select a destination city"
            lblWarning.Visible = True
            Exit Sub
        End If

        'check that source and destination cities are different

        If (fromCode = toCode) Then
            lblWarning.Text = "You must selected different cities!"
            lblWarning.Visible = True
            Exit Sub
        End If

        'get the city and zone for the source and destination

        Dim fromCity As String = city(fromCode)(0)
        Dim fromZone As Integer = city(fromCode)(1)

        Dim toCity As String = city(toCode)(0)
        Dim toZone As Integer = city(toCode)(1)

        'set base ticket price based on zone and coach

        Dim ticketPrice As Double = IIf(fromZone = toZone, 250, 400)

        'increase ticket price for business or first class

        If radBusinessClass.Checked Then
            ticketPrice *= 1.3
        ElseIf radFirstClass.Checked Then
            ticketPrice *= 1.6
        End If

        'display ticket summary

        txtMessage.Text = "Your ticket from " + fromCity + " to " + toCity + " costs " + CStr(FormatCurrency(ticketPrice))
        lblWarning.Visible = False

    End Sub

    Private Sub Form1_Load(sender As Object, e As System.EventArgs) Handles Me.Load

        city("AUS") = {"Austin", "3"}
        city("BOS") = {"Boston", "2"}
        city("BWI") = {"Baltimore", "2"}
        city("DFW") = {"Dallas", "3"}
        city("JFK") = {"New York", "2"}
        city("LAX") = {"Los Angeles", "1"}
        city("MIA") = {"Miami", "2"}
        city("ORD") = {"Chicago", "3"}
        city("SFO") = {"San Francisco", "1"}

    End Sub

    Private Sub lstFrom_TextChanged(sender As System.Object, e As System.EventArgs) Handles lstFrom.TextChanged

    End Sub
End Class

But you'll still have to add a few lines to make controls visible/not visible. As I said, the advantages of the dictionary are

1) simpler and clearer code
2) easy to add more codes/cities

The code I posted was an example of how to use a dictionary relating to your code. The actual variable "code" was just to show how to relate the airport code to the city and region. In actual use it would be

Public Class Form1

    Private city As New Dictionary(Of String, String())

    Private Sub btnDetermineRoute_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDetermineRoute.Click

        Dim fromCode As String = lstFrom.Text       'get airport code for source city
        Dim toCode As String = lstTo.Text           'get airport code for destination city

        'check that a valid airport code was entered for source and destination

        If Not city.ContainsKey(fromCode) Then
            lblWarning.Text = "You must select a source city"
            lblWarning.Visible = True
            Exit Sub
        End If

        If Not city.ContainsKey(toCode) Then
            lblWarning.Text = "You must select a destination city"
            lblWarning.Visible = True
            Exit Sub
        End If

        'check that source and destination cities are different

        If (fromCode = toCode) Then
            lblWarning.Text = "You must selected different cities!"
            lblWarning.Visible = True
            Exit Sub
        End If

        'get the city and zone for the source and destination

        Dim fromCity As String = city(fromCode)(0)
        Dim fromZone As Integer = city(fromCode)(1)

        Dim toCity As String = city(toCode)(0)
        Dim toZone As Integer = city(toCode)(1)

        'set base ticket price based on zone and coach

        Dim ticketPrice As Double = IIf(fromZone = toZone, 250, 400)

        'increase ticket price for business or first class

        If radBusinessClass.Checked Then
            ticketPrice *= 1.3
        ElseIf radFirstClass.Checked Then
            ticketPrice *= 1.6
        End If

        'display ticket summary

        txtMessage.Text = "Your ticket from " + fromCity + " to " + toCity + " costs " + CStr(FormatCurrency(ticketPrice))
        lblWarning.Visible = False

    End Sub

    Private Sub Form1_Load(sender As Object, e As System.EventArgs) Handles Me.Load

        city("AUS") = {"Austin", "3"}
        city("BOS") = {"Boston", "2"}
        city("BWI") = {"Baltimore", "2"}
        city("DFW") = {"Dallas", "3"}
        city("JFK") = {"New York", "2"}
        city("LAX") = {"Los Angeles", "1"}
        city("MIA") = {"Miami", "2"}
        city("ORD") = {"Chicago", "3"}
        city("SFO") = {"San Francisco", "1"}

    End Sub

    Private Sub lstFrom_TextChanged(sender As System.Object, e As System.EventArgs) Handles lstFrom.TextChanged

    End Sub
End Class

But you'll still have to add a few lines to make controls visible/not visible. As I said, the advantages of the dictionary are

1) simpler and clearer code
2) easy to add more codes/cities

Hi Jim,
thanks man I just adding the lines to make the list of the cities not showed at run time.

Please ignore lines 73-75.

Thank you, for follow up with me. I did ignored that line already..
regards,

Toldav

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.