[RESOLVED] [02/03] Need help with data validation function
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
Re: [02/03] Need help with data validation function
Confused,
Your Exercise Question calls for a Function to be called for the validation, and the function would handle all the validation, and return a value accordindly.
But I don't see any function in there besides the one you use to figure the discount amount. Where is your Validation function?
Also, I hope your book is advising you to turn Option Strict On because I can tell from that code that it is not on.
Thridly, what range are you checking values against? An Array Range? A user entered range of values? A preset range of integers? etc, a little more information on the problem would help.
~Crush
[RESOLVED][02/03] Need help with data validation function
I copied and pasted really quick so I didn't get the code on the post, but it is resolved now. I got all the functions and the module to work (a guy here at work helped).
Anyway, what are some of the problems you noticed? I won't know it's wrong if I don't know what it is. If you could hightlight them I would appreciate it. I don't want to start off with bad habits. Thanks.
Re: [02/03] Need help with data validation function
Hey Confused,
Glad you got it resolved!
A problem I noticed isn't a big deal, but should be avoided whenever possible.
They're called Implicit Type Conversions and can sometimes cause very subtle problems in your programs.
An Example:
If txtOrderTotal.Text > dMinOrderTotal
Here you are taking the text in TxtOrderTotal and comparing it to a Decimal.
But a string cannot be compared to a decimal, so the compiler implicitly converts the text for you.
It's just considered by good pratice to some to always explicitly convert values to be tested. I would bet if at the very top of your code, above the class statement if you typed 'Option Strict On' the IDE would underline that above code and give you an error.
An example of this type of problem is noted in this article from Cambridge about 80% of the way down it talks about Implicit Type Conversions.
Article
Anway, you're doing great, and everyone has to start somewhere.
Don't forget to Mark your thread resolved under the Thread Tools Menu.
To Actually explicitly convert the txtOrderTotal.Text to a decimal yourself (assuming its a decimal or can convert to a decimal) you could use either
VB Code:
ctype(txtOrderTotal.Text, Double) or
cdbl(txtOrderTotal.Text)
~Crush
Re: [02/03] Need help with data validation function
Crush,
You're glad? Man am I glad. Last night I was about to throw my computer through the window I was so frustrated. It just took a fresh look and a little help from someone that knew what they were doing.
Anyway, thanks for the words of wisdom and encouragement. I am quite sure I will be posting again because I got a quicker response from you than I did from my instructor. I still haven't heard from her and I emailed on Fri. at 1100 am. Thanks for the quick response and offering help. I appreciate it. Take care and probably talk to you soon.