Programming with Microsoft Visual Basic 2008 Fourth Edition Chapter Three Using Variables and Constants
Slide 2Previewing the Playtime Cellular Application Previewing the Playtime Cellular application Access Run summon on Start menu Browse to VB2008\Chap03 envelope Click the Playtime Cellular ( Playtime Cellular .exe ) document View finished request structure Completed application looks like Chapter 2 rendition Programming with Microsoft Visual Basic 2008, Fourth Edition
Slide 3Previewing the Playtime Cellular Application (proceeded with) Figure 3-1: Name Entry dialog box Programming with Microsoft Visual Basic 2008, Fourth Edition
Slide 4Lesson An Objectives After concentrating on Lesson A, you ought to have the capacity to: Declare variables and named constants Assign information to a current variable Convert string information to a numeric information sort utilizing the TryParse strategy Convert numeric information to an alternate information sort utilizing the Convert class systems Programming with Microsoft Visual Basic 2008, Fourth Edition
Slide 5Lesson An Objectives (proceeded with) Explain the degree and lifetime of variables and named constants Explain the motivation behind the Option Explicit, Option Infer, and Option Strict Programming with Microsoft Visual Basic 2008, Fourth Edition
Slide 6Using Variables to Store Information Controls and variables briefly store information Variable : Temporary stockpiling area in primary memory Specified by information sort, name, extension, and lifetime Reasons to utilize variables: Hold data that is not put away in control on structure Allow for more exact treatment of numeric information Enable code to run all the more productively Programming with Microsoft Visual Basic 2008, Fourth Edition
Slide 7Selecting a Data Type for a Variable Data sort : Specifies kind of information a variable can store Provides a class layout for making variables Unicode : Universal coding plan for characters Assigns one of a kind numeric worth to every character in the composed dialects of the world Programming with Microsoft Visual Basic 2008, Fourth Edition
Slide 8Selecting a Data Type for a Variable (proceeded with) Figure 3-3: Basic information sorts in Visual Basic Programming with Microsoft Visual Basic 2008, Fourth Edition
Slide 9Selecting a Data Type for a Variable (proceeded with) Figure 3-3: Basic information sorts in Visual Basic (kept) Programming with Microsoft Visual Basic 2008, Fourth Edition
Slide 10Selecting a Data Type for a Variable (proceeded) For this course: Use Integer information sort for all whole numbers Use either Decimal or Double information kind of numbers containing decimal places or numbers utilized as a part of estimations Use String information sort for content or numbers not utilized as a part of counts Use Boolean information sort for Boolean qualities Programming with Microsoft Visual Basic 2008, Fourth Edition
Slide 11Selecting a Name for a Variable Variables are alluded to by name Identifier : Another expression for variable name Guidelines for naming variables: Use Hungarian documentation, with a three character prefix speaking to the variableâs information sort Name ought to be distinct: e.g., dblLength Use camel case: e.g., dblSalesAmount Must take after variable naming principles Programming with Microsoft Visual Basic 2008, Fourth Edition
Slide 12Selecting a Name for a Variable (proceeded with) Figure 3-4: Variable naming tenets and illustrations Programming with Microsoft Visual Basic 2008, Fourth Edition
Slide 13Declaring a Variable Declaration proclamation : Used to pronounce (make) a variable and stores space in memory for it Syntax: {Dim|Private|Static} variablename [As datatype][= initialvalue] If no introductory worth is given to variable when proclaiming it, PC stores default esteem Numeric information sorts are set to 0 Boolean information sorts are set to False Object and String information sorts are set to Nothing Programming with Microsoft Visual Basic 2008, Fourth Edition
Slide 14Assigning Data to an Existing Variable Assignment explanation : Assigns worth to variable at runtime Syntax: variablename = Expression may contain exacting constants, properties of controls, variables, or number-crunching administrators Literal steady : Data thing whose quality does not change Example: The string âMaryâ Literal sort character : Changes kind of an exacting Example: sales=2356D Integer cast to Decimal Programming with Microsoft Visual Basic 2008, Fourth Edition
Slide 15The TryParse Method TryParse technique : Converts string to number Syntax: dataType.TryParse(string, variable) dataType : Numeric information sort, for example, Integer string : String to be changed over variable : Variable that gets the numeric quality TryParse is favored over Val just returns a sort Double esteem Programming with Microsoft Visual Basic 2008, Fourth Edition
Slide 16The TryParse Method (proceeded with) Figure 3-8: Results of the TryParse strategy for the Double, Decimal, and Integer information sorts Programming with Microsoft Visual Basic 2008, Fourth Edition
Slide 17The Convert Class Convert class : Can be utilized to change over a number starting with one sort then onto the next Syntax: Convert.method(value) Convert : Name of class technique : Converts quality to determined information sort esteem : Numeric information to be changed over TryParse is prescribed for changing over strings to numeric information sorts Will not deliver a mistake if transformation comes up short Programming with Microsoft Visual Basic 2008, Fourth Edition
Slide 18The Convert Class (proceeded with) Figure 3-9: Syntax and samples of the Convert class routines Programming with Microsoft Visual Basic 2008, Fourth Edition
Slide 19The Scope and Lifetime of a Variable Scope : Indicates where variable can be utilized Lifetime : Indicates to what extent variable stays in memory and can be utilized Scope and lifetime are dictated by where variable is announced Three sorts of degree: Module : Variable can be utilized by all methodology as a part of a structure Procedure : Variable can be utilized inside of technique Block : Variable can be utilized inside of particular code square Programming with Microsoft Visual Basic 2008, Fourth Edition
Slide 20The Scope and Lifetime of a Variable (proceeded with) Figure 3-11: Click occasion system utilizing methodology level variables Programming with Microsoft Visual Basic 2008, Fourth Edition
Slide 21The Scope and Lifetime of a Variable (proceeded with) Figure 3-13: Code utilizing a module-level variable Programming with Microsoft Visual Basic 2008, Fourth Edition
Slide 22Static Variables Static variable : Procedure level variable with developed lifetime Remains in memory between method calls Retains its worth notwithstanding when the system closes Static essential word : Used to announce static variable Static variables act like module-level variables, yet have smaller degree Can just be utilized inside of method where announced Programming with Microsoft Visual Basic 2008, Fourth Edition
Slide 23Static Variables (proceeded with) Figure 3-14: Code utilizing a static variable Programming with Microsoft Visual Basic 2008, Fourth Edition
Slide 24Named Constants Named consistent : Memory area inside PC whose substance can\'t be changed at runtime Const articulation : Creates named steady Syntax: Const constantname As datatype = expression expression : Can be exacting consistent, another named steady, or a number-crunching administrator Cannot contain a variable Programming with Microsoft Visual Basic 2008, Fourth Edition
Slide 25Named Constants (proceeded with) Figure 3-15: Syntax and cases of the Const explanation Programming with Microsoft Visual Basic 2008, Fourth Edition
Slide 26Named Constants (proceeded with) Figure 3-17: Calculate Area buttonâs Click occasion methodology Programming with Microsoft Visual Basic 2008, Fourth Edition
Slide 27Option Explicit, Option Infer, and Option Strict Option Explicit On articulation Prevents you from utilizing undeclared variables Implicit sort change : Converts right-side worth to the information sort of left side Promotion : Cata extended e.g., Integer to Decimal Demotion : information truncated e.g., Decimal to Integer Data misfortune can happen when downgrade happens Programming with Microsoft Visual Basic 2008, Fourth Edition
Slide 28Option Explicit, Option Infer, and Option Strict (proceeded with) Figure 3-18: Rules and cases of sort transformations Programming with Microsoft Visual Basic 2008, Fourth Edition
Slide 29Option Explicit, Option Infer, and Option Strict (proceeded with) Option Infer Off explanation: Ensures that each variable is pronounced with an information sort Option Strict On explanation: Disallows verifiable changes Type transformation standards are connected when this choice is on Programming with Microsoft Visual Basic 2008, Fourth Edition
Slide 30Option Explicit, Option Infer, and Option Strict (proceeded with) Figure 3-19: Option explanations entered in the General Declarations segment Programming with Microsoft Visual Basic 2008, Fourth Edition
Slide 31Lesson A Summary Declare a variable utilizing {Dim | Private | Static} Assignment explanation: Assigns worth to a variable Three levels of extension: Block, method, module TryParse () proselytes strings to numeric information Use Const to pronounce a named steady Avoid using so as to program blunders Option Explicit On, Option Infer Off, and Option Strict On Programming with Microsoft Visual Basic 2008, Fourth Edition
Slide 32Lesson B Objectives After considering Lesson B, you ought to have the capacity to: Include a technique level and module-level variable in an application Concatenate strings Get client data utilizing the InputBox capacity Include the ControlChars.NewLine steady in code Designate the default catch for a structure Format numbers utilizing the ToString strategy Programming with Microsoft Visual Basic 2008, Fourth Edition
Slide 33Modifying the Playtime Cellular Application Modifications required: Display message, deals duty sum, sales representative Calculate the business expense Revise the TOE diagram to mirror the new undertakings Must adjust btnCalc buttonâs Click occasion and the formâs Load occasion Progr