|
-
Oct 3rd, 2010, 04:40 PM
#1
Thread Starter
Junior Member
Invalid Data Alert
Novice here with a question. I am writing a program that has to print an alert message in a textbox if the user inputs invalid data. Invalid data would be a negative number. The textbox is the same textbox as the result would be in provided they input valid data. Here is what I have but it will not print the alert.
principal = Val(principalTextBox.Text)
rate = Val(NumericUpDown.Value)
If principal < 0 Then
resultTextBox.Text = "The information input was not within the correct range of values."
End If
-
Oct 3rd, 2010, 10:26 PM
#2
Re: Invalid Data Alert
can you show us the rest of the code?
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Oct 4th, 2010, 03:32 AM
#3
Thread Starter
Junior Member
Re: Invalid Data Alert
Public Class InterestCalculatorForm
' handles Calculate Button's Click event
Private Sub calculateButton_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles calculateButton.Click
' declare variables to store user input
Dim principal As Decimal ' store principal
Dim rate As Double ' store interest rate
Dim amount As Decimal ' store each calculation
Dim output As String ' store output
' retrieve user input
principal = Val(principalTextBox.Text)
rate = Val(NumericUpDown.Value)
If principal < 0 Then
resultTextBox.Text = "The information input was not within the correct range of values."
End If
' set output header
output = "Year" & ControlChars.Tab _
& "Amount on Deposit" & ControlChars.CrLf
' calculate amount after each year and append to string
For yearCounter As Integer = 1 To yearUpDown.Value
amount = _
principal * ((1 + rate / 100) ^ yearCounter)
output &= (yearCounter & ControlChars.Tab & _
String.Format("{0:C}", amount) & ControlChars.CrLf)
Next
resultTextBox.Text = output ' display result
End Sub ' calculateButton_Click
End Class ' InterestCalculatorForm
-
Oct 4th, 2010, 04:37 AM
#4
Re: Invalid Data Alert
First up, don't use Val. If I enter this 15o9 into your TextBox, most likely a intended to enter 1509, but you don't know that. Passing that text to Val will produce 15, which you can fair pretty much positive that I didn't intend to enter. Val is, in general, a bad option. You should use the TryParse method of the type you want to convert to.
Also, what point calling Val on the Value of a NumericUpDown when it's already a Decimal?
-
Oct 4th, 2010, 11:36 AM
#5
Hyperactive Member
Re: Invalid Data Alert
Coming to your concern, Why are you continuing your code, when you found that, the user entered some invalid value. I think, You can exit from the method from there. Place after your
vb Code:
resultTextBox.Text = "The information input was not within the correct range of values."
..
If you really want to continue, even after that, then let me know, what is the result at the end of your code in textbox ?.
The Difference between a Successful person and others is not a Lack of Knowledge,
But rather a Lack of WILL 
-
Oct 4th, 2010, 05:23 PM
#6
Thread Starter
Junior Member
Re: Invalid Data Alert
The code is in a event handler for a calculate button. I tried making some changes. I have no idea what the TryParse method is. I figured since I had the variable output set to display in the resultstextbox then I could say that if the variable "principal" is less than 0 then the output is equal to "blah blah blah". So I tried that and it still prints the year and amount deposited in the resultstextbox
-
Oct 4th, 2010, 05:26 PM
#7
Re: Invalid Data Alert
vb Code:
If principal < 0 Then
resultTextBox.Text = "The information input was not within the correct range of values."
return
End If
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Oct 4th, 2010, 05:31 PM
#8
Re: Invalid Data Alert
vb Code:
decimal.tryparse(principalTextBox.Text, principal) 'converts string to decimal
rate = cdbl(NumericUpDown.Value)
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
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
|