Use TryParse to Validate User Input

Slides:



Advertisements
Similar presentations
5.04 Apply Decision Making Structures
Advertisements

5.05 Apply Looping Structures
COMPUTER PROGRAMMING II SUMMER 2011 Visual Basic to C#
Apply Sub Procedures/Methods and User Defined Functions
Visual Basic Fundamental Concepts. Integrated Development Enviroment Generates startup form for new project on which to place controls. Features toolbox.
COMPUTER PROGRAMMING I Objective 7.04 Apply Built-in String Functions (3%)
COMPUTER PROGRAMMING I Objective 7.03 Apply Built-in Math Class Functions.
Visual Basic.NET Comprehensive Concepts and Techniques Chapter 4 Working with Variables, Constants, Data Types, and Expressions.
1 CC111 Lec9 : Visual Basic Visual Basic (3) Lecture 9.
3 - Variables Lingma Acheson Department of Computer and Information Science, IUPUI CSCI N331 VB.NET Programming.
COMPUTER PROGRAMMING I SUMMER Apply operators and Boolean expressions.
Chapter 4 Variables and constants. 4.1 Variables -Use of variables is good programming style -easier to modify -easier for a programmer to understand.
COMPUTER PROGRAMMING I Objective 7.03 Apply Built-in Math Class Functions Image taken from:
Programming Test #1 Solutions. Multiple Choice 1. B) the grammar of the coding language 2. C) String 3. A) Single 4. C) 2Burgers4Me 5. B) Design Time.
COMPUTER PROGRAMMING I 5.05 Apply Looping Structures.
COMPUTER PROGRAMMING I 5.05 Apply Looping Structures.
CS0004: Introduction to Programming Project 1 – Lessons Learned.
COMPUTER PROGRAMMING II SUMMER 2011 Visual Basic to C#
Applications Development
Chapter 5: More on the Selection Structure
6c – Function Procedures Lingma Acheson Department of Computer and Information Science, IUPUI CSCI N331 VB.NET Programming.
COMPUTER PROGRAMMING I SUMMER Apply operators and Boolean expressions.
Variables & Function Calls. Overview u Variables  Programmer Defined & Intrinsic  Data Types  Calculation issues u Using Functions  The val() function.
1 Week 5 More on the Selection Structure. 2 Nested, If/ElseIf/Else, and Case Selection Structures Lesson A Objectives After completing this lesson, you.
4-1 Chapter 4 The Selection Process in VB.NET. 4-2 Learning Objectives Understand the importance of the selection process in programming. Describe the.
Chapter 4 Variables and constants. 4.1 Variables -Use of variables is good programming style -easier to modify -easier for a programmer to understand.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
Created by Alia Al-Abdulkarim 2008 Visual Basic Vs. Java.
COMPUTER PROGRAMMING I 5.04 Apply Decision Making Structures.
COMPUTER PROGRAMMING I SUMMER Apply operators and Boolean expressions.
31/01/ Selection If selection construct.
Controlling Program Flow with Decision Structures.
Variables in VB. What is a variable? ► A named memory location that stores a value.
5.02B Decision Making Structure (part 2). Compound Boolean Expressions.
COMPUTER PROGRAMMING I Objective 7.04 Apply Built-in Math Class Functions.
Addison Wesley is an imprint of © 2011 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 3 Variables and Calculations.
Making Interactive Programs with Visual Basic .NET
Visual Basic Declaring Variables Dim x as Integer = 0 In the statement above, x is being declared as an Integer (whole number) and is initialised.
Chapter 6 Controlling Program Flow with Looping Structures.
Visual Basic/ Visual Studio Brandon Large. Connecting to prior knowledge In your notes write down what the two main parts of the computer are. The “software”
Computer Science Up Down Controls, Decisions and Random Numbers.
COMPUTER PROGRAMMING I Apply Procedures to Develop List Box and Combo Box Objects.
Input, Output and Variables GCSE Computer Science – Python.
Welcome to Computer Programming II! Computer Programming II Summer 2015.
Use TryParse to Validate User Input
Visual Basic Fundamental Concepts
5.03 Apply operators and Boolean expressions
Chapter 11 – Exception Handling
5.04 Apply Decision Making Structures
A variable is a name for a value stored in memory.
Objective 7.04 Apply Built-in String Functions (3%)
UNIT 5 Lesson 15 Looping.
Apply Procedures to Develop Menus, List Box and Combo Box Objects
Objective 7.03 Apply Built-in Math Class Functions
Labs for week 2.
UNIT 4 Lesson 13 If statements.
Apply Procedures to Develop Message, Input, and Dialog Boxes
3.01 Apply Controls Associated With Visual Studio Form
Computer Programming I
Single Dimensional Arrays
3.01 Apply Controls Associated With Visual Studio Form
Apply Procedures to Develop Menus, List Box and Combo Box Objects
Understand Variables and Naming Conventions
للمزيد زورونا على موقعنا الإلكتروني:
Visual Basic..
Department Array in Visual Basic
Chapter (3) - Looping Questions.
Decision Structures ISYS 350.
CIS16 Application Development and Programming using Visual Basic.net
GUI Programming in Visual Studio .NET
Presentation transcript:

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 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.

TryParse Methods Method TryParse (String, Int16) 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). TryParse (String, Int32) 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). TryParse (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).

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 blnResult As Boolean blnResult = Int32.TryParse(strNum, intNum) blnResult  true intNum = 5

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

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 blnResult As Boolean blnResult = Double.TryParse(strNum, dblNum ) blnResult  true dblnum  5.0

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

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

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

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” Control Name Text/Items Label lblInput Enter a Number TextBox txtInput lblAnswer Empty Button btnTry Try It

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

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