I am taking VB.NET programming and am at wits end with trying to figure out a function for both numeric and range validation. I was hoping someone could explain it better than my book. The exercise question says to add a function that will validate numeric and range using order total as the argument and return a value of 0 if it's valid data, 1 if it is not numeric, and 2 if it's out of the range. The calling procedure should process the data or display an error message based on the value returned by the function. It also says to move the range checking variables to the module level so they're available to both the function and calling procedure. I can get one validation to work but can't figure out how to get both to work together. I am so frustrated I want to give up. If someone can help I would really appreciate it. Thanks.
VB Code:
Private Sub btnCalculate_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles btnCalculate.Click Const dMinOrderTotal As Decimal = 10 Const dMaxOrderTotal As Decimal = 10000 Dim dOrderTotal As Decimal Dim dDiscountPct As Decimal Dim dDiscountAmount As Decimal Dim dInvoiceTotal As Decimal If IsNumeric(txtOrderTotal.Text) Then If txtOrderTotal.Text > dMinOrderTotal And _ txtOrderTotal.Text < dMaxOrderTotal Then dOrderTotal = txtOrderTotal.Text dOrderTotal = Math.Round(dOrderTotal, 2) dDiscountAmount = DiscountAmount(dOrderTotal) dInvoiceTotal = dOrderTotal - dDiscountAmount lblDiscountAmount.Text = FormatNumber(dDiscountAmount) lblInvoiceTotal.Text = FormatNumber(dInvoiceTotal) Else ' Out of range MessageBox.Show("Order total must be between " & _ dMinOrderTotal & " and " & _ dMaxOrderTotal & ".", "Entry error") End If Else MessageBox.Show("You must enter a numeric value.", "Entry error") End If txtOrderTotal.Focus() End Sub Public Function DiscountAmount(ByVal totalval As Decimal) As Decimal Dim discount As Decimal If totalval >= 500 Then discount = 0.2 ElseIf totalval >= 250 Then discount = 0.15 ElseIf totalval >= 100 Then discount = 0.1 Else discount = 0 End If Dim DiscountAmt As Decimal = totalval * discount Return DiscountAmt End Function Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click Me.Close() End Sub




Reply With Quote