-
November 19th, 2012, 09:22 AM
#1
exception errors
I am beginning to use exceptions in visual basic and haveing issues with my compute button(totals button works fine). My issue is I have a group box of customer information and the customer name, shipping address, and telephone cannot be blank and just need help on how I can throw exception with this. I also have a purchase information group box that checks to make sure product identifier, purchase price, and quantity purchased are not blank either. I was able to get this to work fine for purchase price and quantity purchased but does not work for product identifier. If someone could please walk me through this so I hopefully wont have to ask how to do exceptions again. Here is my code and if I need to explain anything better please let me know...
Code:
Public Class Form1
'Declare module level variables
Private totalDiscountDecimal As Decimal
Private totalQuantityInteger As Integer
Private Sub computeButton_Click(sender As Object, e As EventArgs) Handles computeButton.Click
'Begin try statement to check for errors
Try
'Declare constant for sales tax
Const SALES_TAX As Single = 0.05
Const FED_EX_AIR As Single = 25.0
Const FED_EX_GROUND As Single = 20.0
Const US_POSTAL As Single = 12.0
Const ONE_YEAR_WARRANTY As Single = 75.0
Const CLUB_MEMBERSHIP As Single = 150.0
Const TIRE_SALE As Single = 25.0
Const WHOLESALE_DISCOUNT As Single = 0.1
Const RETAIL_DISCOUNT As Single = 0.0
'Declare variables
Dim subtotalDecimal, totalDueDecimal, salesTaxDecimal, shippingCostDecimal, oneYearWarrantyDecimal,
extrasDecimal, clubMembershipDecimal, tireSaleDecimal, discountDecimal As Decimal
'Declare variables and convert values from textbox controls to memory
Dim purchasePriceDecimal As Decimal = Decimal.Parse(purchasePriceTextBox.Text, Globalization.NumberStyles.Currency)
Dim quantityPurchasedInteger As Integer = Integer.Parse(quantityPurchasedTextBox.Text, Globalization.NumberStyles.Number)
'Determine the shipping cost if fed ex air is clicked
If fedExAirRadioButton.Enabled Then
shippingCostDecimal = FED_EX_AIR
ElseIf fedExGroundRadioButton.Enabled Then
shippingCostDecimal = FED_EX_GROUND
ElseIf USPostalRadioButton.Enabled Then
shippingCostDecimal = US_POSTAL
End If
'Calculate total shipping cost
shippingCostDecimal *= quantityPurchasedInteger
'Show the results in a textbox
shippingCostTextBox.Text = shippingCostDecimal
'Check extras
If oneYearWarrantyCheckBox.Checked Then
oneYearWarrantyDecimal = ONE_YEAR_WARRANTY
End If
If clubMembershipCheckBox.Checked Then
clubMembershipDecimal = CLUB_MEMBERSHIP
End If
If tireSaleCheckBox.Checked Then
tireSaleDecimal = TIRE_SALE
End If
'Determine extrasDecimal
extrasDecimal = oneYearWarrantyDecimal + clubMembershipDecimal + tireSaleDecimal
'Determine the discount
If wholesaleRadioButton.Checked Then
discountDecimal = (purchasePriceDecimal * quantityPurchasedInteger) * WHOLESALE_DISCOUNT
ElseIf retailRadioButton.Checked Then
discountDecimal = RETAIL_DISCOUNT
End If
subtotalDecimal = purchasePriceDecimal * quantityPurchasedInteger + shippingCostDecimal + extrasDecimal - discountDecimal
'Determine the sales tax
If wholesaleRadioButton.Checked Then
salesTaxDecimal = 0.0
ElseIf retailRadioButton.Checked Then
salesTaxDecimal = subtotalDecimal * SALES_TAX
End If
'Determine the total due
totalDueDecimal = subtotalDecimal + salesTaxDecimal
'Display output in textboxes
salesTaxTextBox.Text = salesTaxDecimal.ToString("C")
subtotalTextBox.Text = subtotalDecimal.ToString("C")
shippingCostTextBox.Text = shippingCostDecimal.ToString("C")
costOfExtrasTextBox.Text = extrasDecimal.ToString("C")
discountTextBox.Text = discountDecimal.ToString("C")
totalDueTextBox.Text = totalDueDecimal.ToString("C")
'Enable\Disable buttons
computeButton.Enabled = False
resetButton.Enabled = True
'Accumulate totals
totalDiscountDecimal += discountDecimal
totalQuantityInteger += quantityPurchasedInteger
Catch ex As Exception
MessageBox.Show("Product identifier, purchase price, or quantity purchased has not been entered!",
"Customer name error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub
Private Sub totalCountAverageButton_Click(sender As Object, e As EventArgs) Handles totalCountAverageButton.Click
'Try statement to check for errors
Try
'Determine the average Discount
Dim averageDiscountDecimal = totalDiscountDecimal / totalQuantityInteger
'Display totals in a message string
Dim messageString As String = "Total Wholesale Discounts: " & totalDiscountDecimal.ToString("C") &
ControlChars.NewLine & "Total Wholesale Quantity Sold: " & totalQuantityInteger.ToString("N0") &
ControlChars.NewLine & "Average Discount Amount: " & averageDiscountDecimal.ToString("C")
'Display a header for the message string
MessageBox.Show(messageString, "Totals, Counts and Averages", MessageBoxButtons.OK, MessageBoxIcon.Information)
Catch exArithmeticException As ArithmeticException
MessageBox.Show("No products have been sold yet", "Zero Sales Message", MessageBoxButtons.OK, MessageBoxIcon.Information)
Catch ex As Exception
MessageBox.Show("Unexpected error - inform the system administrator", "Unknown error in totals",
MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub
Private Sub resetButton_Click(sender As Object, e As EventArgs) Handles resetButton.Click
'Clear the textboxes
customerNameTextBox.Clear()
shippingAddressTextBox.Clear()
telephoneMaskedTextBox.Clear()
productIdentifierTextBox.Clear()
purchasePriceTextBox.Clear()
quantityPurchasedTextBox.Clear()
shippingCostTextBox.Clear()
costOfExtrasTextBox.Clear()
discountTextBox.Clear()
subtotalTextBox.Clear()
salesTaxTextBox.Clear()
totalDueTextBox.Clear()
'Set radio buttons back to default
wholesaleRadioButton.Checked = True
fedExAirRadioButton.Checked = True
'Uncheck all the extras checkboxes
oneYearWarrantyCheckBox.Checked = False
clubMembershipCheckBox.Checked = False
tireSaleCheckBox.Checked = False
'Set the focus to the customer name textbox
customerNameTextBox.Focus()
'Enable\disable buttons
computeButton.Enabled = True
resetButton.Enabled = False
End Sub
Private Sub exitButton_Click(sender As Object, e As EventArgs) Handles exitButton.Click
'Exit the program with being prompted to exit
Dim response As Integer
response = MessageBox.Show("Do you want to close the form?", "Quit?", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2)
If response = vbYes Then
Me.Close()
End If
End Sub
End Class
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|