AH5811 AH5811 - 1 year ago 124
Vb.net Question

select case and drop downbox

I am a beginner in ASP.Net and I am currently having problems trying to understand the mechanics of using the drop-down list.

My problem is I am trying to use a

Select Case
expression where I create a variable called
that is set equal to my drop-down list. From looking at the examples I have found on the internet I understand the logic of how to set up the statement, I am just not understanding how to allocate the items in my drop-down box to the
variable for the statement. This would be a huge help because also in the
Select Case
statement there are other drop-down lists that should hold different constant values for every item based on the user selection of the first drop-down box

Partial Class _Default
Inherits System.Web.UI.Page

Protected Sub CalculateButton_Click(sender As Object, e As EventArgs) Handles CalculateButton.Click
Dim value As String = AirlineDropDown.SelectedValue
Dim BagValue As String = NumBagsDropDown.SelectedValue
Dim OvWBagValue As String = OverweightDropdown.SelectedValue
Dim Total, Bagtotal, OvWBagTotal, DamagePro As Double
DamagePro = 12.0

Select Case value
Case Airtran
' set Constant values to Bagvalues and OvWBagValues based on user selection
End Select

Total = Bagtotal + OvWBagTotal + DamagePro
TotalTextbox.Text = FormatNumber(Total, 2)
End Sub

Protected Sub ClearButton_Click(sender As Object, e As EventArgs) Handles ClearButton.Click
TotalTextbox.Text = ""
End Sub
End Class

Image of my webpage

Answer Source

You're on the right track, you just need to compare value to strings. Using the word Airtran on its own would never return true unless it was part of an Airline enum or something. The cost of Airtran's bag fee also needs to be multiplied by the number of bags the user selects, so I've added that to the example below (BagValue must also be converted to an integer to perform multiplication). Lastly, when comparing strings, I tend to like making my string comparisons case-insensitive, so I would handle the select-case like this:

'Converting the number of bags into an integer
Dim BagValue As Integer = CInt(NumBagsDropDown.SelectedValue)
Dim OvWBagValue As Integer = CInt(OverweightDropdown.SelectedValue)

Select Case True
    Case value.ToUpper.Equals("AIRTRAN")
        Bagtotal = 15 * BagValue          '15, or whatever airtran's bag fee is
        OvWBagTotal = 20 * OvWBagValue    'also made this number up
End Select

Also, since DamagePro is a checkbox, you would only want to add the 12 if it is checked, currently your code will add it regardless of its checked state. That would look something like:

Total = Bagtotal + OvWBagTotal
If DamageProChkBox.Checked Then Total += DamagePro
TotalTextbox.Text = FormatNumber(Total, 2)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download