1 / 13

Use TryParse to Validate User Input

Use TryParse to Validate User Input . Computer Programming I. Objective/Essential Standard. Essential Standard 6.00 Apply tools to obtain and validate user input. Indicator 6.03 Apply Procedures for Validation of User Input. (3%). Validating Numeric Input.

bary
Download Presentation

Use TryParse to Validate User Input

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Use TryParse to Validate User Input Computer Programming I

  2. Objective/Essential Standard Essential Standard6.00 Apply tools to obtain and validate user input. Indicator6.03 Apply Procedures for Validation of User Input. (3%)

  3. Validating Numeric Input • There are multiple methods you can use to check the input. • In previous lessons you have learned how to use the Convert methods. • In this lesson you will learn how to use TryParse. • TryParse is typically used in an assignment statement or in an if statement as it returns true/false.

  4. TryParse Methods

  5. Int32.TryParse • The TryParse (String, Int32) converts the string and returns true/false indicating success/failure. • If the result is true, TryParse will convert and load the number into the numeric variable. • If result is false, TryParse will load 0 into the numeric variable. Dim strNum As String = “5” Dim intNum As Integer Dim blnResultAs Boolean blnResult= Int32.TryParse(strNum, intNum) blnResult true intNum = 5

  6. Int32.TryParse Dim strNum As String = “five” Dim intNum As Integer Dim blnResult As Boolean blnResult = Int32.TryParse(strNum, intNum) blnResult false intNum = 0

  7. Double.TryParse The TryParse (String, Double) converts the string and returns true/false indicating success/failure. Dim strNum As String = “5.0” Dim dblNum As Double Dim blnResultAs Boolean blnResult= Double.TryParse(strNum, dblNum) blnResult true dblnum  5.0

  8. Double.TryParse Dim strNum As String = “five” Dim dblNum As Double Dim blnResult As Boolean blnResult = Double.TryParse(strNum, dblNum ) blnResult false dblnum  0

  9. Using the Convert Class Methods with Try/Catch • Remember you learned how to write a try/catch block of code to check for user input. • The Convert class methods will throw a runtime error if the string is not numeric. • We can use the tryParseto validate numeric input.

  10. Using TryParse with Convert Methods If Int32.TryParse( txtAge.Text, intAge) Then MessageBox.Show("Your age is: " & intAge.ToString) ’executes if the TryParse Convert works else MessageBox.Show("Enter your age in numbers."); ’executes if the TryParse fails End If

  11. Try It! • Create a new application called tryParseExample • Save it into the location as instructed by your teacher. • Add the following controls. • When the button is clicked, the result should be displayed in the lblAnswer label. • When entering 5 and clicking the button, you should display “ 5 is a number.” • When entering Hi and clicking the button, you should display “Hi is not a number”

  12. Try It! Solution Private Sub btnTry_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTry.Click Dim strNum As String = "" Dim dblNum As Double strNum = txtInput.Text If Double.TryParse(strNum, dblNum) Then lblAnswer.Text = dblNum.ToString() + " is a number" Else lblAnswer.Text = strNum + " is not a number" End If End Sub

  13. Conclusion • This PowerPoint provided an overview of using TryParse to validate input in Visual Studio. • For more information on this topic • http://msdn.microsoft.com/en-us/library/32s6akha(v=VS.90).aspx • http://msdn.microsoft.com/en-us/library/yxcx7skw.aspx

More Related