Presentation is loading. Please wait.

Presentation is loading. Please wait.

An Application Uses Variables to Hold Information So It May Be Manipulated, Used to Manipulate Other Information, or Remembered for Later Use.

Similar presentations


Presentation on theme: "An Application Uses Variables to Hold Information So It May Be Manipulated, Used to Manipulate Other Information, or Remembered for Later Use."— Presentation transcript:

1 An Application Uses Variables to Hold Information So It May Be Manipulated, Used to Manipulate Other Information, or Remembered for Later Use

2 Why Have Variables? A variable is a storage location in the computer’s memory, used for holding information while the program is running The information that is stored in a variable may change, hence the name “variable”

3 What Can You Do With Variables?
Copy and store values entered by the user, so they may be manipulated Perform arithmetic on values Test values to determine that they meet some criterion Temporarily hold and manipulate the value of a control property Remember information for later use in the program

4 Setting the Value of a Variable
An assignment statement is used to set the (new) value of a variable, as in: length = greeting = "Good Morning " & txtName.Text

5 Variable Declarations
A variable declaration is a statement that causes Visual Basic .NET to create a variable in memory As in Dim length As Integer

6 Declaration Syntax The official syntax is Dim VariableName As DataType where Dim (stands for Dimension) is a keyword VariableName is the name to be used As is a keyword DataType is the type of the variable and will be one of many possible keywords

7 Visual Basic .NET Data Types
Boolean Byte Char Date Decimal Double Integer Long Object Short Single String

8 Variable Naming Rules The first character of a variable name must be a character or an underscore Subsequent characters may be either of those plus the numeric digits Thus variable names cannot contain spaces or periods (or many other kinds of characters) Variable names must not be keywords

9 Variable Naming Conventions
Each variable name should describe its use, e.g., itemsOrdered When multiple words are used in a name, capitalize the initials, except for the first one (again, itemsOrdered) As noted earlier, certain names should have a specific prefix, e.g., btn

10 Auto List Feature As you are entering your Visual Basic .NET program, VB will often aid you by offering a list of choices for that could be entered next Right after you type "As" in a variable declaration, Visual Basic .NET will offer you a list of all of the established data types Either choose one or keep typing

11 Variable Default Values
When a variable is first created in memory, Visual Basic .NET assigns it a default value numeric types are given a value of zero strings are given a value of Nothing dates default to 12:00:00 AM January 1,1

12 Initialization of Variables via the Declaration
It is preferable to establish your program's own initial value for variables that will not otherwise be given values before they are used In the declaration, simply append " = value" Dim length As Integer = 112

13 Scope of a Variable, I A variable’s scope is the part of the program where the variable is visible and may be accessed by programming statements

14 Scope of a Variable, II The scope of a variable begins where it is declared And extends to the end of the procedure in which it appears This variable is called local The variable is not visible outside of the procedure and its name cannot be declared again within the same procedure

15 Lifetime of a Variable The storage for a variable is created upon each use of the procedure The storage for a variable is destroyed as soon as the procedure finishes executing

16 The Val Function, I Suppose you wish to use text input as a number: number = txtInput.Text This will work without a run time error as long as txtInput.Text is the text equivalent of a numerical value (like "45") If it is not, there will be a run time error

17 The Val Function, II The Val function is more lenient on conversions from text to numeric values If the initial characters form a numeric value, it will return that Otherwise, it will return a value of zero

18 The Val Function, III Argument Val(Argument) "34.90" 34.9 "86abc" 86
"34.90" 34.9 "86abc" 86 "$24.95" 0 "3,789" 3 "" 0 "x29" 0 "47%" 47 "Geraldine" 0

19 ToString Method This is a Method that will convert any variable to a string, as in Dim number As Integer = 123 lblNumber.Text = number.ToString

20 Option Strict On Placed at the very top of the code window this will prevent Visual Basic .NET from performing implicit data type conversion The code must perform all conversions using Val or ToString

21 Performing Calculations and Working With Numbers
Visual Basic .NET Provides Several Operators for Performing Mathematical Operations You May Also Use Parentheses to Group Operations and Build More Complex Mathematical Statements

22 The Arithmetic Operators, I
Visual Basic .NET provides operators for the common arithmetic operations: Addition + Subtraction - Multiplication * Division / Exponentiation ^

23 The Arithmetic Operators, II
Examples of use: total = price + tax area = length * width average = total / items salePrice = retail / 2 cube = side ^ 3

24 Special Integer Division Operator
The backslash (\) is used as an integer division operator The result is always an integer, created by doing the division and then discarding any remainder Any floating-point operand is first rounded to the nearest integer

25 Special Modulo (MOD) Operator
This operator follows the same basic rules as the backslash operator, but yields the remainder after the division \ operator yields an integer result of division MOD operator yields the integer remainder (after division using the \ operator)

26 Arithmetic Operator Precedence, I
Which operations are done first -- precedence tells us -- highest to lowest: Exponentiation (^) Multiplicative (* and /) Integer Division (\) Modulus (MOD) Additive (+ and -)

27 Arithmetic Operator Precedence, II
When two operators with the same precedence share an operand, the operator on the left works first, then the operator on the right

28 Arithmetic Operator Precedence, III
Grouping with parentheses () forces the expression within those parentheses to be evaluated before others Roughly speaking, the order of evaluation in Visual Basic .NET is similar to that used in algebra classes (parenthesized expressions first, then exponentiation, then multiplicative operators, then the additive operators)

29 Combined Assignment Operators, I
Frequently program assignment statements are similar to: number = number - 5 That is, modify a variable with one arithmetic operator and store the result back into the same variable

30 Combined Assignment Operators, II
There are special assignment operators to enhance this usage: += add a value to the variable -= subtract a value from the variable *= multiple the variable by some value /= divide the variable by some value \= integer divide the variable by some value &= concatenate the variable with some value

31 More C (Convert) Functions
Cbool Cbyte Cchar Cdate CDbl CDec Cint CLng Cobj Cshort CSng CStr

32 Named Constants, I Whenever a program needs to use a constant (e.g., the local sales tax percentage) it is a good idea to give it a variable name However, a variable does not necessarily have the same value throughout the program as an assignment statement can change the value

33 Named Constants, II Visual Basic .NET provides for a variable whose value, once established in the declaration, cannot be modified afterwards: Const salesTax As Single = 0.06

34 Formatting Numbers for Output
Numbers May Be Formatted in Various Ways for Output

35 FormatNumber Function
FormatNumber(expression [, DecimalPoints]) The expression is evaluated and output as a number The optional second argument gives the number of requested decimal places

36 FormatCurrency Function
FormatCurrency(expression [, DecimalPoints]) The expression is evaluated and output as a currency value based on your PCs local options The optional second argument gives the number of requested decimal places

37 FormatPercent Function
FormatPercent(expression [, DecimalPoints]) The expression is evaluated and output as a percentage value The optional second argument gives the number of requested decimal places


Download ppt "An Application Uses Variables to Hold Information So It May Be Manipulated, Used to Manipulate Other Information, or Remembered for Later Use."

Similar presentations


Ads by Google