Results 1 to 8 of 8

Thread: Invalid Data Alert

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Oct 2010
    Posts
    25

    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

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,423

    Re: Invalid Data Alert

    can you show us the rest of the code?

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Oct 2010
    Posts
    25

    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

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5
    Hyperactive Member nagasrikanth's Avatar
    Join Date
    Nov 2004
    Location
    India,Hyderabad.
    Posts
    420

    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
    vb Code:
    1. Exit Sub
    after your
    vb Code:
    1. 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

  6. #6

    Thread Starter
    Junior Member
    Join Date
    Oct 2010
    Posts
    25

    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

  7. #7
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,423

    Re: Invalid Data Alert

    vb Code:
    1. If principal < 0 Then
    2. resultTextBox.Text = "The information input was not within the correct range of values."
    3. return
    4. End If

  8. #8
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,423

    Re: Invalid Data Alert

    vb Code:
    1. decimal.tryparse(principalTextBox.Text, principal) 'converts string to decimal
    2. rate = cdbl(NumericUpDown.Value)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width