Hey all, just wondering if anyone can find a problem in my piece of code for trying to work out the total cost.

When i run the program and click the cost button, it only returns a value of £0.00 for insurance cost and total cost. Any help much appreciated.

Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCost.Click
        Dim Cost As Double
        Dim Zone As String
        Dim Weight As Double
        Dim Delivery_Type As String

        Zone = CStr(ZoneCbox.Text)
        Weight = Val(Me.WeightTextBox.Text)
        Delivery_Type = CStr(DeliveryCbox.Text)

        If Zone = "UK" And Delivery_Type = "Next Day Delivery" Then
            Select Case Weight
                Case Is < 20
                    Cost = 19
                Case Is < 40
                    Cost = 28.8
                Case Is < 60
                    Cost = 39.1
                Case Is < 80
                    Cost = 47.9
                Case Is < 100
                    Cost = 71.1
            End Select
        End If

        If Zone = "UK" And Delivery_Type = "Standard Delivery" Then
            Select Case Weight
                Case Is < 20
                    Cost = 15.5
                Case Is < 40
                    Cost = 25.3
                Case Is < 60
                    Cost = 35.6
                Case Is < 80
                    Cost = 44.4
                Case Is < 100
                    Cost = 68.2
            End Select
        End If

        If Zone = "Rest of Europe" And Delivery_Type = "Surface Mail" Then
            Select Case Weight
                Case Is < 20
                    Cost = 19.5
                Case Is < 40
                    Cost = 28.3
                Case Is < 60
                    Cost = 40.1
                Case Is < 80
                    Cost = 49.5
                Case Is < 100
                    Cost = 75.5
            End Select
        End If

        If Zone = "Rest of Europe" And Delivery_Type = "Air Mail" Then
            Select Case Weight
                Case Is < 20
                    Cost = 28
                Case Is < 40
                    Cost = 36.8
                Case Is < 60
                    Cost = 48.6
                Case Is < 80
                    Cost = 58
                Case Is < 100
                    Cost = 84
            End Select
        End If

        If Zone = "WorldWide" And Delivery_Type = "Surface Mail" Then
            Select Case Weight
                Case Is < 20
                    Cost = 25.4
                Case Is < 40
                    Cost = 34.2
                Case Is < 60
                    Cost = 46.3
                Case Is < 80
                    Cost = 55.4
                Case Is < 100
                    Cost = 82.3
            End Select
        End If

        If Zone = "WorldWide" And Delivery_Type = "Air Mail" Then
            Select Case Weight
                Case Is < 20
                    Cost = 33.9
                Case Is < 40
                    Cost = 42.7
                Case Is < 60
                    Cost = 54.8
                Case Is < 80
                    Cost = 63.9
                Case Is < 100
                    Cost = 90.8
            End Select
        End If

        Me.lblCost.Text = Format(Cost, "£#0.00")
        Me.lblInsuranceCost.Text = Format(Cost * 0.1, "£#0.00")

    End Sub

Try this string.format style instead:

String.Format("{0:c}", Cost) will return $10.00. The $ is replaced by whatever symbol is specified by the locale setting.
If you need it to be pounds regardless try
Me.lblCost.Text = "" + String.Format("{0:n2}", Cost) which will give you a number to 2 decimal places with your currency symbol placed in front.

Try this string.format style instead:

String.Format("{0:c}", Cost) will return $10.00. The $ is replaced by whatever symbol is specified by the locale setting.
If you need it to be pounds regardless try
Me.lblCost.Text = "" + String.Format("{0:n2}", Cost) which will give you a number to 2 decimal places with your currency symbol placed in front.

Her hericles
Just tried using that code for the cost button there now and its still returning a value of 0.00 when i ask for the total cost.

OK, somewhere your Cost value must be getting dumped. Is it coming up zero for all destination and delivery types?
Step through your code in debug mode and check the Cost variable is ever showing anything other than 0.00.

Probably cost is never being set. Your code is not robust because it does not cater for all situations, for example it does not cater for weights greater than 100. If you can't accept weights > 100 you should cater for that in the code and display an error message.

This article has been dead for over six months. Start a new discussion instead.