locked
problems with an IF THEN statement RRS feed

  • Question

  • User-1474218207 posted

    Hi the code below shows an if statement that is determining a customer type and a discount percent for that customer.

    But when I run the code it only ever reads the first discount percent even though the allocated price range is above the first discount percents range and should therefore give the second discount percent and i cant figure out why it is not using the second discount percent in any of the bronze, silver or gold statements.

     any help with this would be great. 

      Private Sub btnPrice_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDiscount.Click
    
    
            Dim customerType As String = ""
            Dim price As Decimal
            Dim discountPercent As Decimal
    
            Me.AvaliableDiscount(price, discountPercent)
    
        End Sub
    
        Private Sub AvaliableDiscount(ByVal price As Decimal, ByVal discountPercent As Decimal)
    
            txtBeforeDiscount.Text = txtCarPrice.Text
    
            If txtCustomerType.Text = "Bronze" Or txtCustomerType.Text = "bronze" Then
                If price >= 5000 Or price < 9999 Then
                    discountPercent = 0.05D
                ElseIf price >= 10000 Then
                    discountPercent = 0.1D
                Else
                    discountPercent = 0D
                End If
            End If
    
            If txtCustomerType.Text = "Sliver" Or txtCustomerType.Text = "silver" Then
                If price >= 5000 Or price < 9999 Then
                    discountPercent = 0.1D
                ElseIf price >= 10000 Then
                    discountPercent = 0.15D
                Else
                    discountPercent = 0D
                End If
            End If
    
            If txtCustomerType.Text = "Gold" Or txtCustomerType.Text = "gold" Then
                If price >= 5000 Or price < 9999   Then
                    discountPercent = 0.15D
                ElseIf price >= 10000 Then
                    discountPercent = 0.2D
                Else
                    discountPercent = 0D
                End If
            End If
    
            Dim discountAmount As Decimal = CDec(CDbl(txtBeforeDiscount.Text) * discountPercent)
            Dim endPrice As Decimal = CDec(CDbl(txtBeforeDiscount.Text) - discountAmount)
    
            txtDiscountPercent.Text = (FormatPercent(discountPercent, 1))
            txtDiscountAmount.Text = FormatCurrency(discountAmount)
            txtTotalPrice.Text = FormatCurrency(endPrice)
    
        End Sub
    
      <?xml:namespace prefix = o /><o:p></o:p><?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p>
    Sunday, June 14, 2009 8:15 AM

Answers

  • User-1360095595 posted

    I'm wondering if it's a quircky thing where you have to use 5000D or 5000.0 instad of 5000.  Have you tried that?  Have you tried stepping through your code?

    Also, let me suggest that you use elseif between the vairous bronze, silver & gold because if it's bronze, there's no reason to even check for silver or gold.  It will make for slightly better performing code.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, June 14, 2009 8:58 AM
  • User-1171043462 posted

    If price >= 5000 Or price < 9999 Then

     

    Replace this with

    If price >= 5000 And price < 9999 Then

    The reason you used Or thus if the price  is Greater than 5000

    first condition is satisfied and hence it ignores the second part i.e. < 9999

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, June 14, 2009 9:01 AM

All replies

  • User-1360095595 posted

    I'm wondering if it's a quircky thing where you have to use 5000D or 5000.0 instad of 5000.  Have you tried that?  Have you tried stepping through your code?

    Also, let me suggest that you use elseif between the vairous bronze, silver & gold because if it's bronze, there's no reason to even check for silver or gold.  It will make for slightly better performing code.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, June 14, 2009 8:58 AM
  • User-1171043462 posted

    If price >= 5000 Or price < 9999 Then

     

    Replace this with

    If price >= 5000 And price < 9999 Then

    The reason you used Or thus if the price  is Greater than 5000

    first condition is satisfied and hence it ignores the second part i.e. < 9999

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, June 14, 2009 9:01 AM
  • User-925286913 posted

    Change

    If price >= 5000 Or price < 9999   Then
    to
    If price >= 5000 And price < 9999   Then
     

     

    Sunday, June 14, 2009 9:01 AM
  • User-1474218207 posted

    Thanks for the advice got it working now.

    Thanks again

    nbrooks[cool]

    Sunday, June 14, 2009 10:53 AM