Variables and Arithmetic Operations

Slides:



Advertisements
Similar presentations
Using Macros and Visual Basic for Applications (VBA) with Excel
Advertisements

Office 2003 Post-Advanced Concepts and Techniques M i c r o s o f t Word Project 8 Working with Macros and Visual Basic for Applications (VBA)
Chapter Three Using Variables and Constants Programming with Microsoft Visual Basic th Edition.
COMPREHENSIVE Excel Tutorial 8 Developing an Excel Application.
Slide 1 Chapter 2 Visual Basic Interface. Slide 2 Chapter 2 Windows GUI  A GUI is a graphical user interface.  The interface is what appears on the.
Microsoft Visual Basic 2008 CHAPTER ELEVEN Multiple Classes and Inheritance.
Microsoft Visual Basic 2012 CHAPTER TWO Program and Graphical User Interface Design.
Microsoft Visual Basic 2005 CHAPTER 8 Using Procedures and Exception Handling.
Microsoft Visual Basic 2005 CHAPTER 12 Cell Phone Applications and Web Services.
Visual Basic Chapter 1 Mr. Wangler.
Variables and Constants
Chapter 3: Using Variables and Constants
Programming with Microsoft Visual Basic th Edition CHAPTER THREE USING VARIABLES AND CONSTANTS.
Microsoft Visual Basic 2012 Using Procedures and Exception Handling CHAPTER SEVEN.
Microsoft Visual Basic 2005 CHAPTER 5 Mobile Applications Using Decision Structures.
Microsoft Visual Basic 2008 CHAPTER 8 Using Procedures and Exception Handling.
© 2006 Lawrenceville Press Slide 1 Chapter 3 Visual Basic Interface.
Visual Basic 2005 CHAPTER 2 Program and Graphical User Interface Design.
Visual Basic.NET Comprehensive Concepts and Techniques Chapter 4 Working with Variables, Constants, Data Types, and Expressions.
Microsoft Visual Basic 2005 ENRICHMENT CHAPTER Visual Studio Tools for Office.
Program Design and Coding
Microsoft Visual Basic 2012 CHAPTER THREE Program Design and Coding.
Microsoft Visual Basic 2010 CHAPTER THREE Program Design and Coding.
Microsoft Visual Basic 2005 CHAPTER 4 Variables and Arithmetic Operations.
Chapter 4 Variables and constants. 4.1 Variables -Use of variables is good programming style -easier to modify -easier for a programmer to understand.
Working with the VB IDE. Running a Program u Clicking the”start” tool begins the program u The “break” tool pauses a program in mid-execution u The “end”
Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 3 Variables, Constants, Methods, and Calculations.
Visual Basic 2005 CHAPTER 2 Program and Graphical User Interface Design.
Fall 2003Sylnovie Merchant, Ph.D. ACCESS Tutorial Note: The purpose of this tutorial is to provide an introduction to some of the functions of ACCESS in.
Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Three Memory Locations and Calculations.
Chapter 4 Variables and constants. 4.1 Variables -Use of variables is good programming style -easier to modify -easier for a programmer to understand.
Visual Basic.NET Comprehensive Concepts and Techniques Chapter 4 Working with Variables, Constants, Data Types, and Expressions.
Programming with Microsoft Visual Basic 2008 Fourth Edition Chapter Three Using Variables and Constants.
Microsoft Visual Basic 2010 CHAPTER TWO Program and Graphical User Interface Design.
Programming with Microsoft Visual Basic th Edition
Chapter 2 More Controls Programming in C#. NET Objectives Use text boxes, group boxes, check boxes, radio buttons, and picture boxes effectively.
Microsoft Visual Basic 2012 CHAPTER FIVE Decision Structures.
Microsoft Visual Basic 2012 CHAPTER FOUR Variables and Arithmetic Operations.
Chapter 4.  Variables – named memory location that stores a value.  Variables allows the use of meaningful names which makes the code easier to read.
Slide 1 Chapter 3 Variables  A variable is a name for a value stored in memory.  Variables are used in programs so that values can be represented with.
Programming with Microsoft Visual Basic 2012 Chapter 3: Using Variables and Constants.
© 2006 Lawrenceville Press Slide 1 Chapter 4 Variables  A variable is a name for a value stored in memory.  Variables are created using a declaration.
Microsoft Visual Basic 2010 CHAPTER FOUR Variables and Arithmetic Operations.
IS 350 Numeric Data Types.
Excel Tutorial 8 Developing an Excel Application
Visual Basic.NET Windows Programming
Microsoft Visual Basic 2005: Reloaded Second Edition
A variable is a name for a value stored in memory.
Topics Designing a Program Input, Processing, and Output
Chapter 2 – Introduction to the Visual Studio .NET IDE
Chapter 3: Using Variables and Constants
Program and Graphical User Interface Design
CHAPTER FIVE Decision Structures.
Word 2002 Chapter 7 MANIPULATING TABS.
Microsoft Access Illustrated
Using Procedures and Exception Handling
Chapter 2 Visual Basic Interface
Program and Graphical User Interface Design
Chapter 7 Advanced Form Techniques
CHAPTER FIVE Decision Structures.
Variables and Arithmetic Operations
MODULE 7 Microsoft Access 2010
WEB PROGRAMMING JavaScript.
Visual Basic Programming Chapter Four Notes Working with Variables, Constants, Data Types, and Expressions GROUPBOX CONTROL The _____________________________________.
CIS16 Application Development Programming with Visual Basic
Benchmark Series Microsoft Word 2016 Level 1
Topics Designing a Program Input, Processing, and Output
Introduction to Programming
Topics Designing a Program Input, Processing, and Output
Key Applications Module Lesson 16 — Excel Essentials
Presentation transcript:

Variables and Arithmetic Operations CHAPTER FOUR Variables and Arithmetic Operations

Objectives Create, modify, and program a TextBox object Use code to place data in the Text property of a Label object Use the AcceptButton and CancelButton properties Understand and declare String and Numeric variables Use assignments statements to place data in variables Chapter 4: Variables and Arithmetic Operations

Objectives Use literals and constants in coding statements Understand scope rules for variables Convert string and numeric data Understand and use arithmetic operators and arithmetic operations Chapter 4: Variables and Arithmetic Operations

Objectives Format and display numeric data as a string Create a form load event Create a concatenated string Debug a program Chapter 4: Variables and Arithmetic Operations

Introduction Chapter 4: Variables and Arithmetic Operations

Preview the Chapter Project Chapter 4: Variables and Arithmetic Operations

TextBox Objects With Visual Studio open and the frmDigitalDownloads.vb [Design] tabbed page visible, point to the TextBox .NET component in the Toolbox Drag the TextBox .NET component onto the Windows Form object at the desired location When the upper-left corner of the pointer is located where you want the TextBox object’s upper-left corner, release the left mouse button Chapter 4: Variables and Arithmetic Operations

TextBox Objects Chapter 4: Variables and Arithmetic Operations

TextBox Objects Chapter 4: Variables and Arithmetic Operations

Sizing and Positioning a TextBox Object Select the TextBox object. Scroll in the Properties window until the Text property is visible and then click the right column for the Text property Type the maximum number of characters the user normally will enter into the text box and then press the ENTER key. When entering numbers, the digit 8 often is entered because it is wider than other digits. In this example, the value 888 is entered because three digits is the maximum number of digits the user normally will enter Chapter 4: Variables and Arithmetic Operations

Sizing and Positioning a TextBox Object Using the Font property in the Properties window, change the Font property to the correct font and font size. For this application, change the font to Century Gothic and change the font size to 12. Then, drag the right edge of the TextBox object to resize the TextBox object so it is slightly wider than the 888 entry To horizontally align the text in the label and the text in the text box, drag the text box up until a red snap line indicates the bottoms of the text are aligned. Then, release the left mouse button Chapter 4: Variables and Arithmetic Operations

Sizing and Positioning a TextBox Object Chapter 4: Variables and Arithmetic Operations

Sizing and Positioning a TextBox Object Chapter 4: Variables and Arithmetic Operations

Aligning Text in a TextBox Object Select the TextBox object. In the Properties window, scroll until the TextAlign property is visible, click the TextAlign property in the left column, and then click the list arrow in the right column of the TextAlign property Click Center in the TextAlign property list Because the TextBox object is sized properly, remove the digits in the TextBox object. Select the characters 888 in the Text property, press the DELETE key on your keyboard, and then press the ENTER key Chapter 4: Variables and Arithmetic Operations

Aligning Text in a TextBox Object Chapter 4: Variables and Arithmetic Operations

Aligning Text in a TextBox Object Chapter 4: Variables and Arithmetic Operations

Creating a MultiLine Text Box Select the TextBox object, click the Action tag, and point to the MultiLine check box Click the MultiLine check box Chapter 4: Variables and Arithmetic Operations

Creating a MultiLine Text Box Chapter 4: Variables and Arithmetic Operations

Creating a MaskedTextBox Object Chapter 4: Variables and Arithmetic Operations

Creating a MaskedTextBox Object Chapter 4: Variables and Arithmetic Operations

Creating a MaskedTextBox Object Drag a MaskedTextBox .NET component from the Toolbox to the Windows Form object. Then, click the Action tag on the TextBox object and point to the Set Mask command Click Set Mask on the MaskedTextBox Tasks list and then click the Short date mask description in the Input Mask dialog box Click the OK button in the Input Mask dialog box and then click anywhere in the Windows Form object Chapter 4: Variables and Arithmetic Operations

Creating a MaskedTextBox Object Chapter 4: Variables and Arithmetic Operations

Creating a MaskedTextBox Object Chapter 4: Variables and Arithmetic Operations

Label Objects Drag a Label object onto the Windows Form object to the correct location. Name the label lblTotalCostOfDownloads. Change the label to the appropriate font size (Century Gothic, 12 point). In the Text property for the Label object, enter the maximum number of characters ($888.88) that will appear in the label during execution of the program Drag the Label object up until the red snap line appears. Then release the left mouse button Chapter 4: Variables and Arithmetic Operations

Label Objects Chapter 4: Variables and Arithmetic Operations

Label Objects Chapter 4: Variables and Arithmetic Operations

Accept Button in Form Properties The program will carry out the event handler processing associated with the Accept Button if the user clicks the button or if the user presses the ENTER key Click a blank area in the Windows Form object to select it. Scroll in the Properties window until the AcceptButton property is visible. Click the AcceptButton property name in the left column and then click the AcceptButton property list arrow in the right column Click btnCalculateCost in the AcceptButton property list Chapter 4: Variables and Arithmetic Operations

Accept Button in Form Properties Chapter 4: Variables and Arithmetic Operations

Cancel Button in Form Properties When the user presses the ESC key, the event handler processing for the button identified as the Cancel button will be executed Click a blank area in the Windows Form object to select it Click the CancelButton property name in the left column in the Properties window for the Windows Form object, and then click the CancelButton list arrow Click the button name (btnClear) in the CancelButton property list Chapter 4: Variables and Arithmetic Operations

Visual Studio Preparation for Code Entry With the Toolbox visible, click the Toolbox Close button. The Toolbox closes and the work area expands in size. To reshow the Toolbox after it has been closed, click the Toolbox button on the Standard toolbar Chapter 4: Variables and Arithmetic Operations

Visual Studio Preparation for Code Entry Chapter 4: Variables and Arithmetic Operations

String Data Type A String data type allows every character available on the computer to be stored in it The data type for the value the user enters in a TextBox object and that is stored in the Text property of the TextBox object is string A variable is a named location in RAM where data is stored A String variable is a named location in RAM that can store a string value Chapter 4: Variables and Arithmetic Operations

String Data Type Chapter 4: Variables and Arithmetic Operations

Assignment Statements One method to place data in the variable is to use an assignment statement Chapter 4: Variables and Arithmetic Operations

Assignment Statements With Visual Studio displaying the code editing window and the insertion point located in the desired column, type Dim followed by a space. Then, type the name of the String variable you want to define, strNumberOfSongs on your keyboard Press the SPACEBAR, type the word As and then press the SPACEBAR again Because the entry should be String, type str on your keyboard Press the ENTER key Chapter 4: Variables and Arithmetic Operations

Assignment Statements To begin the assignment statement, type strn. IntelliSense displays the only variable name that starts with the letter strn, the String variable strNumberOfDownloads Press the SPACEBAR, press the EQUAL SIGN key, and then press the SPACEBAR Type me. to display the IntelliSense list, and then type txt to identify the txtNumberOfSongs TextBox object in the IntelliSense list Press the PERIOD key and then, if necessary, type te to highlight the Text entry in the IntelliSense list Press the ENTER key Chapter 4: Variables and Arithmetic Operations

Assignment Statements Chapter 4: Variables and Arithmetic Operations

Assignment Statements Chapter 4: Variables and Arithmetic Operations

Numeric Data Types A numeric data type must be used in arithmetic operations Chapter 4: Variables and Arithmetic Operations

Numeric Data Types Chapter 4: Variables and Arithmetic Operations

Other Data Types Chapter 4: Variables and Arithmetic Operations

Other Data Types Chapter 4: Variables and Arithmetic Operations

Miscellaneous Data Types Chapter 4: Variables and Arithmetic Operations

Variable Prefixes Chapter 4: Variables and Arithmetic Operations

Literals A value is called a literal when the value being used in the assignment statement is literally the value that is required Chapter 4: Variables and Arithmetic Operations

Forced Literal Types Sometimes you might want a literal to be a different data type than the Visual Basic default Chapter 4: Variables and Arithmetic Operations

Constants A constant variable will contain one permanent value throughout the execution of the program Chapter 4: Variables and Arithmetic Operations

Constants The declaration of a constant variable begins with the letters Const, not the letters Dim You must assign the value to be contained in the constant on the same line as the definition of the constant. You cannot attempt to change the value in the constant variable anywhere in the program. If you attempt this, you will produce a compiler error The letter c often is placed before the prefix of the constant variable name to identify throughout the program that it is a constant variable and cannot be changed Other than the letter c constant variable names are formed using the same rules and techniques as nonconstant names Chapter 4: Variables and Arithmetic Operations

Referencing a Variable When a variable is declared, it will be underlined with a green squiggly line until it is referenced in a statement It is mandatory when using a variable in a program that the variable is defined prior to using the variable name in a statement Chapter 4: Variables and Arithmetic Operations

Scope of Variables The scope of a variable specifies where within the program the variable can be referenced in a Visual Basic statement A variable can be referenced only within the region of the program where it is defined The code between the Sub statement and the End Sub statement is a procedure A variable that can only be referenced within the region of the program where it is defined is called a local variable Local variables have a certain lifetime in the program Global variables can be used in multiple regions of a program Chapter 4: Variables and Arithmetic Operations

Converting Variable Data Visual Basic includes several procedures that allow you to convert one data type to another data type A procedure to convert a String data type to an Integer data type is named ToInt32 The procedure is found in the Convert class, which is available in a Visual Studio 2008 class library Chapter 4: Variables and Arithmetic Operations

Using a Procedure A procedure that performs its task but does not return a value is called a Sub procedure A procedure that returns a value is called a Function procedure, or a function An argument identifies a value required by a procedure Every procedure is part of a class Chapter 4: Variables and Arithmetic Operations

Using a Procedure Chapter 4: Variables and Arithmetic Operations

Option Strict On Visual Basic will, by default, automatically convert data types if the data type on the right side of the equal sign in an assignment statement is different from the data type on the left side of the equal sign To prevent automatic conversion of values, the developer must insert the Option Strict On statement in the program prior to any event handler code in the program The Option Strict On statement explicitly disallows any default data type conversions in which data loss would occur and any conversion between numeric types and strings Chapter 4: Variables and Arithmetic Operations

Option Strict On Chapter 4: Variables and Arithmetic Operations

Arithmetic Operations Chapter 4: Variables and Arithmetic Operations

Arithmetic Operators Chapter 4: Variables and Arithmetic Operations

Multiple Operations A single assignment statement can contain multiple arithmetic operations Hierarchy of Operations Exponentiation (^) is performed first Multiplication (*) and division (/) are performed next Integer division (\) is next MOD then occurs Addition (+) and subtraction (-) are performed last Within these five steps, calculations are performed left to right Chapter 4: Variables and Arithmetic Operations

Displaying Numeric Output Data Chapter 4: Variables and Arithmetic Operations

Format Specifications for the ToString Function Use the format specifier to identify the format for the numeric data to be returned by the ToString function Chapter 4: Variables and Arithmetic Operations

Precision Specifier The precision specifier is a number that is included within the quotation marks in the function call to identify the number of positions to the right of the decimal point that should be returned Chapter 4: Variables and Arithmetic Operations

Clear Procedure The Clear procedure clears any data currently placed in the Text property of a TextBox object Chapter 4: Variables and Arithmetic Operations

Clearing the Text Property of a Label The Clear procedure cannot be used with a Label object You must write an assignment statement that assigns a null length string to the Text property of a Label object Chapter 4: Variables and Arithmetic Operations

Setting the Focus When the focus is on a TextBox object, the insertion point is located in the text box Chapter 4: Variables and Arithmetic Operations

Form Load Event A form load event occurs when the program starts and the Digital Downloads form is loaded This event handler completes the following tasks: Display the cost per download heading Clear the placeholder from the lblTotalCostOfDownloads Text property Set the focus on the txtNumberOfDownloads text box Chapter 4: Variables and Arithmetic Operations

Concatenation The process of joining two different values into a single string is called concatenation The values being concatenated must be String data types Chapter 4: Variables and Arithmetic Operations

Concatenation Chapter 4: Variables and Arithmetic Operations

Class Scope When a variable is referenced in two different event handling procedures, it must be defined at the class level instead of the procedure (event handler) level Chapter 4: Variables and Arithmetic Operations

Debugging Your Program A Format Exception occurs when the user enters data that a statement within the program cannot process properly Chapter 4: Variables and Arithmetic Operations

Debugging Your Program An Overflow Exception occurs when the user enters a value greater than the maximum value that can be processed by the statement It is not possible to divide by zero, so if your program contains a division operation and the divisor is equal to zero, the Divide By Zero Exception will occur Chapter 4: Variables and Arithmetic Operations

Program Design Chapter 4: Variables and Arithmetic Operations

Program Design Chapter 4: Variables and Arithmetic Operations

Event Planning Document Chapter 4: Variables and Arithmetic Operations

Summary Create, modify, and program a TextBox object Use code to place data in the Text property of a Label object Use the AcceptButton and CancelButton properties Understand and declare String and Numeric variables Use assignments statements to place data in variables Chapter 4: Variables and Arithmetic Operations

Summary Use literals and constants in coding statements Understand scope rules for variables Convert string and numeric data Understand and use arithmetic operators and arithmetic operations Chapter 4: Variables and Arithmetic Operations

Summary Format and display numeric data as a string Create a form load event Create a concatenated string Debug a program Chapter 4: Variables and Arithmetic Operations

Variables and Arithmetic Operations CHAPTER FOUR COMPLETE Variables and Arithmetic Operations