1 / 64

Objectives

1. Objectives. Declare both a variable and a named constant Write an assignment statement Use the Convert class methods to convert data to an appropriate type Write arithmetic expressions Understand the scope of both a variable and a named constant. Objectives ( continued ).

darius
Télécharger la présentation

Objectives

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

  2. Objectives • Declare both a variable and a named constant • Write an assignment statement • Use the Convert class methods to convert data to an appropriate type • Write arithmetic expressions • Understand the scope of both a variable and a named constant Microsoft Visual Basic .NET: Reloaded

  3. Objectives (continued) • Include internal documentation in the code • Use the Option Strict and Option Explicit statements • Use a TOE chart to code an application • Use pseudocode and a flowchart to plan an object’s code • Send focus to a control Microsoft Visual Basic .NET: Reloaded

  4. Objectives (continued) • Explain the difference between syntax errors and logic errors • Format an application’s numeric output Microsoft Visual Basic .NET: Reloaded

  5. Variables • Computer memory locations where users can temporarily store data • Must be assigned a data type by programmer • Data type determines type of data variable can store • Contents can change as application runs Microsoft Visual Basic .NET: Reloaded

  6. Selecting a Data Type for a Variable Microsoft Visual Basic .NET: Reloaded

  7. Selecting a Data Type for a Variable (continued) • Integers  whole numbers • Integer, Long, Short • Floating-point numbers  real numbers • Single, Double – use exponential notation • Example: 32000 = 3.2E4 • Decimal  uses fixed decimal point • Character Types • Char – one character • String – 0 to 2 billion characters Microsoft Visual Basic .NET: Reloaded

  8. Selecting a Name for a Variable • Name should be descriptive • Use three-letter prefix • Use “m” for “module-scope” variables • Punctuate using Pascal-case • Type m and 3 letter prefix using lowercase, then capitalize the first letter of each word in the variable name Microsoft Visual Basic .NET: Reloaded

  9. Selecting a Name for a Variable (continued) Microsoft Visual Basic .NET: Reloaded

  10. Selecting a Name for a Variable (continued) Microsoft Visual Basic .NET: Reloaded

  11. Declaring a Variable Microsoft Visual Basic .NET: Reloaded

  12. Declaring a Variable (continued) • accessibility variablename as [datatype = initialvalue] • accessibility - sets scope of variable • Dim, Public, Private • variablename = programmer chosen name • datatype = data type of the variable • Anything in brackets is optional • initialvalue = default value at start of program • If no value specified numeric types = 0, character types = Nothing, boolean = false Microsoft Visual Basic .NET: Reloaded

  13. Assigning Data to an Existing Variable • assignment statement • strName = “Mary” • intAge = 35 • “=“ sign is called assignment operator • Literal constant – value does not change • Number 35 is numeric literal constant • “Mary” is a string literal constant • String - a group of characters enclosed in quotes Microsoft Visual Basic .NET: Reloaded

  14. Assigning Data to an Existing Variable (continued) Microsoft Visual Basic .NET: Reloaded

  15. Assigning Data to an Existing Variable (continued) • Literal type characters force literal constants to assume new data type Microsoft Visual Basic .NET: Reloaded

  16. Using the Convert Class • Convert class creates object that is either a number or a string by converting value to a specified data type and returning result strAge = Convert.ToString(intAge) Microsoft Visual Basic .NET: Reloaded

  17. Writing Arithmetic Expressions • Precedence number indicates order in which VB performs operation in an expression Microsoft Visual Basic .NET: Reloaded

  18. Writing Arithmetic Expressions (continued) • Integer division operator (\) • Returns integer portion of division discarding remainder • 8\3 results in answer of 2 • Modulus arithmetic operator (Mod) • Returns remainder portion of division discarding integer portion • 7 Mod 3 results in an answer of 1 • Often used to determine Leap years • Valid only with division of integer values Microsoft Visual Basic .NET: Reloaded

  19. How To… Microsoft Visual Basic .NET: Reloaded

  20. The Scope and Lifetime of a Variable • Scope • Indicates where in the application’s code the variable can be used • Lifetime • Indicates how long the variable remains in the computer’s internal memory Microsoft Visual Basic .NET: Reloaded

  21. The Scope and Lifetime of a Variable (continued) • Procedure Scope • Procedure-level variable • Variable declared within a procedure • Variable can only be used in procedure in which declared • Module scope • Module-level variable • Variable declared in General Declarations section of the form • Variable can be used within all procedures in the form Microsoft Visual Basic .NET: Reloaded

  22. Procedure-level variables example Microsoft Visual Basic .NET: Reloaded

  23. Module-level variable example Microsoft Visual Basic .NET: Reloaded

  24. The Scope and Lifetime of a Variable (continued) • Block scope • Block-level variable • Declared inside specific blocks of code • If…Then…Else or For….Next • Can be used only inside the block in which declared Microsoft Visual Basic .NET: Reloaded

  25. Named Constants • A memory location inside a computer whose value cannot be changed while the application is running Microsoft Visual Basic .NET: Reloaded

  26. Named Constants (continued) Microsoft Visual Basic .NET: Reloaded

  27. Internally Documenting the Program Code • Internal documentation: • Term used by programmers for comments placed in code • Place an apostrophe (‘) before text you want treated as internal documentation (comment) • VB ignores everything after apostrophe on line • Place comments at beginning of each procedure Microsoft Visual Basic .NET: Reloaded

  28. Internally Documenting the Program Code (continued) • Include comments: • as necessary to explain various sections of code • at beginning of application explaining application’s: • Name • Purpose • Author • Date of creation or modification Microsoft Visual Basic .NET: Reloaded

  29. Internally Documenting the Program Code (continued) Microsoft Visual Basic .NET: Reloaded

  30. Option Explicit and Option Strict • Option Explicit On • Tells computer to warn you if your code contains name of an undeclared variable • Prevents unintentional declaration of variables which automatically are of type Object • Option Strict On • Tells computer not to perform any implicit type conversions which may lead to loss of data • Implicit type conversions are automatic conversions from one data type to another performed by the computer to fit data into an assigned memory location Microsoft Visual Basic .NET: Reloaded

  31. Option Explicit and Option Strict (continued) Microsoft Visual Basic .NET: Reloaded

  32. Coding the SKATE-AWAY SALES Application • Application calculates and displays total number of skateboards ordered and total price of skateboards including 5% sales tax • Planning steps: • Identify tasks the application needs to perform • Identify objects to which you will assign those tasks • Identify events required to trigger an object into performing its assigned tasks • Design the user interface Microsoft Visual Basic .NET: Reloaded

  33. Coding the SKATE-AWAY SALES Application (continued) Microsoft Visual Basic .NET: Reloaded

  34. Using Pseudocode to Plan a Procedure • Pseudocode: short phrases to describe steps a procedure needs to take to accomplish its goal Microsoft Visual Basic .NET: Reloaded

  35. Using a Flowchart to Plan a Procedure • Flowchart – uses standardized symbols to show steps procedure must follow to reach its goal • Standard symbols utilized: • Flowlines - connection lines between symbols • Start/stop – ovals indicating starting and ending points of procedure • Process – rectangles designating tasks such as calculations • Input/Output – parallelogram designating input and output tasks such as getting information from user Microsoft Visual Basic .NET: Reloaded

  36. Using a Flowchart to Plan a Procedure (continued) Microsoft Visual Basic .NET: Reloaded

  37. Using a Flowchart to Plan a Procedure (continued) • Coding the one task of bntExit_Click event using either pseudocode or flowchart tools Microsoft Visual Basic .NET: Reloaded

  38. Coding the btnClear control’s Click Event Procedure Microsoft Visual Basic .NET: Reloaded

  39. Assigning a Value to the Property of a Control • Zero-length string - “” • Also called empty string • Set of quotation marks with nothing between them • Assigned to the text property of a control at run-time removes contents of control • Me.txtname.Text • Me refers to the current form • txtName refers to a textbox control on the form • Text refers to a property of the txtName control Microsoft Visual Basic .NET: Reloaded

  40. HOW TO… Microsoft Visual Basic .NET: Reloaded

  41. Using the Focus Method • Focus indicates the control that can accept input and/or is awaiting an action from user Microsoft Visual Basic .NET: Reloaded

  42. btnClear_Click Event Procedure code Microsoft Visual Basic .NET: Reloaded

  43. Coding the btnCalc Control’s Click Event Procedure Microsoft Visual Basic .NET: Reloaded

  44. Coding the btnCalc Control’s Click Event Procedure (continued) Microsoft Visual Basic .NET: Reloaded

  45. Completed Code for Application Microsoft Visual Basic .NET: Reloaded

  46. Completed Code for Application (continued) Microsoft Visual Basic .NET: Reloaded

  47. Testing and Debugging the Application • Invalid data - data the application is not expecting • Debugging: • Process of locating syntax and logic errors in the program • Syntax errors • Typographical errors that violate rules governing valid syntax of the language • Me.Colse() instead of Me.Close() • Logic errors • Instruction that does not give expected results • decAverage = decNum1 + decNum2 / 2 Microsoft Visual Basic .NET: Reloaded

  48. Testing and Debugging the Application (continued) • First test with valid data Microsoft Visual Basic .NET: Reloaded

  49. Testing and Debugging the Application (continued) • Then test with invalid data Microsoft Visual Basic .NET: Reloaded

  50. Formatting Numeric Output • Formatting: • Specifying number of decimal places and special characters to display in a number • Format string: • String used to specify format • Example: “Axx” must beenclosed in quotes • Format specifier - A • Alphabetic character that specifies format you wish to use • Precision specifier – xx • xx is the sequence of digits desired Microsoft Visual Basic .NET: Reloaded

More Related