Use Try Parse to Validate User Input COMPUTER

  • Slides: 13
Download presentation
Use Try. Parse to Validate User Input COMPUTER PROGRAMMING I

Use Try. Parse to Validate User Input COMPUTER PROGRAMMING I

Objective/Essential Standard �Essential Standard 6. 00 Apply tools to obtain and validate user input.

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 �There are multiple methods you can use to check the input.

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 Try. Parse. �Try. Parse is typically used in an assignment statement or in an if statement as it returns true/false.

Try. Parse Methods Method Try. Parse (String, Int 16) Converts the string representation of

Try. Parse Methods Method Try. Parse (String, Int 16) Converts the string representation of a number to its 16 -bit signed integer equivalent. A Boolean return value indicates whether the conversion succeeded or failed (true or false). Try. Parse (String, Int 32) Converts the string representation of a number to its 32 -bit signed integer equivalent. A Boolean return value indicates whether the conversion succeeded or failed (true or false). Try. Parse (String, Double) Converts the string representation of a number to its double equivalent. A Boolean return value indicates whether the conversion succeeded or failed (true or false).

Int 32. Try. Parse � The Try. Parse (String, Int 32) converts the string

Int 32. Try. Parse � The Try. Parse (String, Int 32) converts the string and returns true/false indicating success/failure. If the result is true, Try. Parse will convert and load the number into the numeric variable. If result is false, Try. Parse will load 0 into the numeric variable. Dim str. Num As String = “ 5” Dim int. Num As Integer Dim bln. Result As Boolean bln. Result = Int 32. Try. Parse(str. Num, int. Num) bln. Result true int. Num = 5

Int 32. Try. Parse Dim str. Num As String = “five” Dim int. Num

Int 32. Try. Parse Dim str. Num As String = “five” Dim int. Num As Integer Dim bln. Result As Boolean bln. Result = Int 32. Try. Parse(str. Num, int. Num) bln. Result false int. Num = 0

Double. Try. Parse �The Try. Parse (String, Double) converts the string and returns true/false

Double. Try. Parse �The Try. Parse (String, Double) converts the string and returns true/false indicating success/failure. Dim str. Num As String = “ 5. 0” Dim dbl. Num As Double Dim bln. Result As Boolean bln. Result = Double. Try. Parse(str. Num, dbl. Num ) bln. Result true dblnum 5. 0

Double. Try. Parse Dim str. Num As String = “five” Dim dbl. Num As

Double. Try. Parse Dim str. Num As String = “five” Dim dbl. Num As Double Dim bln. Result As Boolean bln. Result = Double. Try. Parse(str. Num, dbl. Num ) bln. Result false dblnum 0

Using the Convert Class Methods with Try/Catch �Remember you learned how to write a

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 try. Parse to validate numeric input.

Using Try. Parse with Convert Methods If Int 32. Try. Parse( txt. Age. Text,

Using Try. Parse with Convert Methods If Int 32. Try. Parse( txt. Age. Text, int. Age) Then Message. Box. Show("Your age is: " & int. Age. To. String) ’executes if the Try. Parse Convert works Else Message. Box. Show("Enter your age in numbers. "); ’executes if the Try. Parse fails End If

Try It! � Create a new application called try. Parse. Example Save it into

Try It! � Create a new application called try. Parse. Example 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 lbl. Answer 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” Control Name Text/Items Label lbl. Input Enter a Number Text. Box txt. Input Label lbl. Answer Empty Button btn. Try It

Try It! Solution Private Sub btn. Try_Click(By. Val sender As System. Object, By. Val

Try It! Solution Private Sub btn. Try_Click(By. Val sender As System. Object, By. Val e As System. Event. Args) Handles btn. Try. Click Dim str. Num As String = "" Dim dbl. Num As Double str. Num = txt. Input. Text If Double. Try. Parse(str. Num, dbl. Num) Then lbl. Answer. Text = dbl. Num. To. String() + " is a number" Else lbl. Answer. Text = str. Num + " is not a number" End If End Sub

Conclusion �This Power. Point provided an overview of using Try. Parse to validate input

Conclusion �This Power. Point provided an overview of using Try. Parse to validate input in Visual Studio. �For more information on this topic http: //msdn. microsoft. com/enus/library/32 s 6 akha(v=VS. 90). aspx http: //msdn. microsoft. com/en-us/library/yxcx 7 skw. aspx