Tutorial 3: Using Variables and Constants1 Tutorial 3 Using Variables and Constants.

Slides:



Advertisements
Similar presentations
Tutorial 31 Variable Memory location whose value can change as the program is running. Used to hold temporary information Used to control the type of data.
Advertisements

Chapter 3: Using Variables and Constants
1.
Chapter 3: Using Variables and Constants Programming with Microsoft Visual Basic 2005, Third Edition.
Objectives Understand the software development lifecycle Perform calculations Use decision structures Perform data validation Use logical operators Use.
Chapter 11: Classes and Objects
1.
String Variables Visual Basic for Applications 4.
Input Validation Check the values entered into a text box before beginning any calculations Validation is a form of ‘self-protection’, rejecting bad data.
An Introduction to Programming with C++ Fifth Edition Chapter 4 Chapter 4: Variables, Constants, and Arithmetic Operators.
An Introduction to Programming with C++ Fifth Edition Chapter 4 Chapter 4: Variables, Constants, and Arithmetic Operators.
Data Types and Operations Programming Fundamentals (Writing Code)Programming Fundamentals (Writing Code)
Chapter 2: Introduction to C++.
5.05 Apply Looping Structures
Chapter Three Using Variables and Constants Programming with Microsoft Visual Basic th Edition.
Chapter 8: String Manipulation
Variables, Constants, Methods, and Calculations Chapter 3 - Review.
Variables and Constants
Chapter 3: Using Variables and Constants
Programming with Microsoft Visual Basic th Edition CHAPTER THREE USING VARIABLES AND CONSTANTS.
Chapter 4: The Selection Structure
Object Variables Visual Basic for Applications 3.
Tutorial 11 Using and Writing Visual Basic for Applications Code
® Microsoft Access 2010 Tutorial 11 Using and Writing Visual Basic for Applications Code.
Copyright © 2008 Pearson Prentice Hall. All rights reserved. 1 Microsoft Office Excel Copyright © 2008 Pearson Prentice Hall. All rights reserved
Chapter 3 Part 11 Variables, Constants, and Calculations Chapter 3 Section 3.3 Chapter 3 Section 3.4.
Representing Data: Constants and Variables CHAPTER THREE Matakuliah: T0063 – Pemrograman Visual Tahun: 2009.
Chapter 4 Variables and constants. 4.1 Variables -Use of variables is good programming style -easier to modify -easier for a programmer to understand.
Date Variables Visual Basic for Applications 5. Objectives n In this tutorial, you will learn how to: n Reserve a Date variable n Use an assignment statement.
1 Week 6 The Repetition Structure. 2 The Repetition Structure (Looping) Lesson A Objectives After completing this lesson, you will be able to:  Code.
Tutorial 6 The Repetition Structure
Introduction to Programming with RAPTOR
Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 3 Variables, Constants, Methods, and Calculations.
Data Types and Variables. Data Type! Computers are all about Data! Data can be in the form of Text Dates Sounds Pictures.
1.
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.
Programming with Microsoft Visual Basic 2008 Fourth Edition Chapter Three Using Variables and Constants.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
Programming with Microsoft Visual Basic th Edition
Variables Hold information that may be manipulated, used to manipulate other information or remembered for later use A storage location in memory (RAM)
Chapter 4 Getting Started with VBA. Subroutines Subroutine is the logical section of code that performs a particular task. Subroutine is also called a.
Variables in VB.NET. Variables  A storage location in memory (RAM)  Holds data/information while the program is running  These storage locations can.
1 Writing Software Kashef Mughal. 2 Algorithms  The term algorithm (pronounced AL-go-rith-um) is a procedure or formula for solving a problem.  An Algorithm.
Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 13 How Long Can This Go On?
Visual Basic CDA College Limassol Campus COM123 Visual Basic Programming Semester C Lecture:Pelekanou Olga Week 3: Using Variables.
Tutorial 3: Using Variables and Constants1 Tutorial 3 Using Variables and Constants.
Variables and Expressions Programming Right from the Start with Visual Basic.NET 1/e 7.
Tutorial 81 Field, Record, Data File Field - a single item of information about a person, place, or thing Record - a group of related fields that contain.
Microsoft Visual Basic 2012 CHAPTER FOUR Variables and Arithmetic Operations.
COMPREHENSIVE Access Tutorial 11 Using and Writing Visual Basic for Applications Code.
Chapter 4: Variables, Constants, and Arithmetic Operators Introduction to Programming with C++ Fourth Edition.
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
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.
Slide 1 Chapter 3 Variables  A variable is a name for a value stored in memory.  Variables are created using a declaration statement. For example: Dim.
Chapter 6 Controlling Program Flow with Looping Structures.
Chapter 2 Variables and Constants. Objectives Explain the different integer variable types used in C++. Declare, name, and initialize variables. Use character.
A variable is a name for a value stored in memory.
Chapter 3: Using Variables and Constants
Chapter 4: The Selection Structure
Objectives Learn about Function procedures (functions), Sub procedures (subroutines), and modules Review and modify an existing subroutine in an event.
Variables and Arithmetic Operations
Microsoft Visual Basic 2005 BASICS
WEB PROGRAMMING JavaScript.
CIS16 Application Development Programming with Visual Basic
CHAPTER FOUR VARIABLES AND CONSTANTS
Presentation transcript:

Tutorial 3: Using Variables and Constants1 Tutorial 3 Using Variables and Constants

Tutorial 3: Using Variables and Constants2 Creating Variables and Named Constants Lesson A Objectives After completing this lesson, you will be able to:  Create a local and form-level variable  Select an appropriate data type for a variable  Select an appropriate name for a variable  Assign data to an existing variable  Create a named constant

Tutorial 3: Using Variables and Constants3 Using Variables to Store Information  Besides storing data in the properties of controls, a programmer also can store data, temporarily, in memory locations inside the computer  The memory locations are called variables, because the contents of the locations can change as the program is running  You can enter and store data in the box, but you cannot actually see the box

Tutorial 3: Using Variables and Constants4 Using Variables to Store Information  One use for a variable is to hold information that is not stored in a control on the user interface  You can also store the data contained in a control’s property in a variable  Before learning how to create a variable in a Visual Basic.NET application, you learn how to select an appropriate data type and name for the variable

Tutorial 3: Using Variables and Constants5 Selecting a Data Type for a Variable TypeSizeTypeSize Byte1Short2 Char2Integer4 Boolean2Long8 Decimal16Single4 Double8StringVaries Date8ObjectAnything

Tutorial 3: Using Variables and Constants6 Choose the Correct Data Type Short, Integer, LongUsed to store whole numbers Single, DoubleStore floating-point numbers DecimalStores numbers with a decimal point BooleanStores True and False CharStores one Unicode character ByteStores 8-bits of data DateStores date and time information StringStores a sequence of characters

Tutorial 3: Using Variables and Constants7 Selecting a Name for a Variable  You should assign a descriptive name to each variable used in an application  The name should help you remember the variable’s data type and purpose

Tutorial 3: Using Variables and Constants8 Selecting a Name for a Variable  Figure 3-4 lists the three characters typically associated with the Visual Basic.NET data types  It is a common practice to type the letter m and the three-character ID using lowercase letters, and then use Pascal-case for the remainder of the variable’s name

Tutorial 3: Using Variables and Constants9

10 Selecting a Name for a Variable  Using Pascal-case, you capitalize the first letter in each word in the name  In addition to being descriptive, the name that a programmer assigns to a variable must follow several rules, which are listed in Figure 3-5  Also included in the figure are examples of valid and invalid variable names

Tutorial 3: Using Variables and Constants11

Tutorial 3: Using Variables and Constants12 Rules for Naming Variables  Name must begin with a letter  Name can contain only letters, numbers, and the underscore. No punctuation characters or spaces are allowed  Name cannot exceed 255 characters  Name cannot be a reserved word

Tutorial 3: Using Variables and Constants13 Declaring a Variable Accessor variablename As Datatype [ = InitialValue] Accessor is [Public|Private|Static|Dim] Dim intTotal As Integer Dim sngRadius As Single = 12 Dim intYellow, intBlue As Integer Public strName As String = “Diane Zak”

Tutorial 3: Using Variables and Constants14 Assigning Data to an Existing Variable  A literal constant is simply an item of data whose value does not change while the application is running  Also notice that string literal constants are enclosed in quotation marks, but numeric literal constants and variable names are not  The quotation marks differentiate a string from both a number and a variable name  It is important to remember that a variable can store only one item of data at any one time

Tutorial 3: Using Variables and Constants15 Assigning Data to an Existing Variable  When you use an assignment statement to assign another item to the variable, the new data replaces the existing data  When you run the application and click the button, the three lines of code are processed as follows:  The Dim statement creates the intNumber variable in memory and automatically initializes it to the number 0

Tutorial 3: Using Variables and Constants16 Assigning Data to a Variable  The intNumber = 500 assignment statement removes the zero from the intNumber variable and stores the number 500 there instead  The intNumber = intNumber *2 assignment statement first multiplies the contents of the intNumber variable (500) by the number 2, giving 1000 The assignment statement is of the form variablename = value sngHours = 38.5 sngBonus = sngSales * 0.1 strName = “Mary” intNumber = 500

Tutorial 3: Using Variables and Constants17 The Scope of a Variable  A variable’s scope indicates which procedures in an application can use the variable  The scope is determined by where the Dim, Public or Private statement is entered  When you declare a variable in a procedure, the variable is called a local variable and is said to have procedure scope, because only that procedure can use the variable  When you declare a variable in the form’s Declarations section, the variable is called a form- level variable and is said to have module scope

Tutorial 3: Using Variables and Constants18 Creating a Local Variable  Created with the Dim statement  The Dim statement is entered in an object’s event procedure  Only the procedure in which it is declared can use the variable  Removed from memory when the procedure ends

Tutorial 3: Using Variables and Constants19 Creating a Form-level Variable  Created with the Public/Private statement  Entered in a form’s General declarations section  Can be used by any of the procedures in the form  Removed from memory when the application ends or the form is destroyed

Tutorial 3: Using Variables and Constants20 Constants  Literal constant  an item of data whose value cannot change while the program is running  Examples:  7  “Mary”  Named constant  a memory location whose contents cannot be changed while the program is running  Examples:  conPi  conRate

Tutorial 3: Using Variables and Constants21 Creating a Named Constant  A memory location whose value cannot change during run time  Syntax: [Public|Private] Const constname [As datatype] = expression  Examples:  Const conPi As Single =  Public Const conMaxAge as Integer = 65

Tutorial 3: Using Variables and Constants22 Modifying the Skate-Away Sales Application Lesson B Objectives After completing this lesson, you will be able to:  Include local and form-level variables in an application  Concatenate strings  Get user input using the InputBox function  Locate the Visual Basic.NET intrinsic constants in the Object Browser  Include the vbNewLine constant in code  Designate the default button for a form

Tutorial 3: Using Variables and Constants23 Concatenating Strings  Connecting strings together is called concatenating  You use the concatenation operator, which is the ampersand (&), to concatenate strings in Visual Basic.NET  When concatenating strings, you must be sure to include a space before and after the concatenation operator ExampleResult “Hello “ & strFirstNameHello Mary strFirstName & “ sold $“ & sngSales & “.”Mary sold $1000. intUnits & sngSales intUnits + sngSales1200

Tutorial 3: Using Variables and Constants24 The InputBox Function  The InputBox function displays one of Visual Basic.NET’s predefined dialog boxes  Contains a message, along with an OK button, a Cancel button, and an input area  Syntax: strAnswer = InputBox(prompt, title, default)  Use sentence capitalization for the prompt, and book title capitalization for the title  Has limitations: can’t control appearance and allows user to enter only one piece of data

Tutorial 3: Using Variables and Constants25 The Newline Character  The newline character, which is Chr(13) & Chr(10), instructs the computer to issue a carriage return followed by a line feed  An intrinsic constant is a named constant that is built into Visual Basic.NET itself

Tutorial 3: Using Variables and Constants26 The Object Browser  Provides information about objects available to your application

Tutorial 3: Using Variables and Constants27 Designating a Default Button  Can be selected by pressing the Enter key even when the button does not have the focus  Set the form’s AcceptButton property to the desired button  If used, it is typically the first button  If a button’s action is destructive and irreversible, then it should not be the default button

Tutorial 3: Using Variables and Constants28 Modifying the Skate-Away Sales Application’s Code Lesson C Objectives After completing this lesson, you will be able to:  Include a static variable in code  Code the TextChanged event procedure  Create a procedure that handles more than one event

Tutorial 3: Using Variables and Constants29 Static Variables  A static variable is a local variable that retains its value when the procedure in which it is declared ends  Syntax: Static variablename As datatype [= initialvalue]  Removed from memory when application ends or form is removed from memory

Tutorial 3: Using Variables and Constants30 Coding the TextChanged Event Procedure  A control’s TextChanged event occurs when the contents of a control’s Text property change  This can happen as a result of either the user entering data into the control, or the application’s code assigning data to the control’s Text property

Tutorial 3: Using Variables and Constants31 Associating a Procedure with Different Objects or Events  The keyword Handles appears in a procedure header and indicates the object and event associated with the procedure  You can also associate a procedure with more than one object and event; to do so, you simply list each object and event, separated by commas, in the Handles section of the procedure header