Presentation is loading. Please wait.

Presentation is loading. Please wait.

IS 350 Numeric Data Types.

Similar presentations


Presentation on theme: "IS 350 Numeric Data Types."— Presentation transcript:

1 IS 350 Numeric Data Types

2 Objectives Work with numeric data
Describe the difference between value types and reference types Declare and use variables to store data Create constants Write expressions containing arithmetic operators and operands

3 Objectives (continued)
Convert data between numeric and String data types Use the FV method Use the TextBox control Define the order in which control instances get input focus (Tab Order)

4 Introduction to Data Types
Every object and property has a data type A data type defines what kind of data an object or property can store Data types that store numbers are called numeric data types Integral data types store whole numbers Floating-point data types store values having a fractional part Other data types store strings Many other additional data types exist

5 Integral Data Types Integral data types store data that does not have a decimal point Some integral data types can store 0 or positive values Other integral data types can store both positive and negative values Negative values are indicated by a sign bit

6 Integral data types

7 Integral Data Types Integral values are stored as binary numbers
The SByte data type stores its data in 8 bits 7 bits store the data 1 bit stores the value's sign The largest possible value is 2^7-1 or 127 The value has the binary pattern

8 Floating-point Data Types
Floating-point numbers store values with a decimal point Floating-point numbers are stored in a form of scientific notation One bit is the sign bit A 23-bit mantissa stores the fractional part of a number The exponent is stored in 8 bits

9 Floating-point data types

10 Rounding Errors Floating-point numbers are an approximation of an actual value 1/3 or 2/3 must be rounded, for example Rounding introduces an error called rounding error The greater the precision of the data type, the smaller the rounding error The Decimal data type has the smallest rounding error and the greatest precision

11 Performance of Numeric Data Types
Performance is a key characteristic of any application Different data types have different performance characteristics Arithmetic operations using integral data types is faster than operations on floating-point data types The Decimal data type is very slow Decimal prevents rounding error

12 Properties of Numeric Data Types
Numeric data types have constant fields that store the minimum and maximum possible values for the data type MinValue stores the minimum possible value MaxValue stores the maximum possible value Primary data types are structures having constant values

13 Introduction to the String Data Type
Data is often represented as a string of characters Textual data is represented as a string Data entered by end users is textual Strings are used to display output Strings are represented differently than numbers (numeric data types) Much more later

14 Storage of the String Data Type
Strings are stored as Unicode characters in two bytes as an unsigned short (UShort) The first 127 values store letters, digits, and special characters Values 128 to 255 store special characters such as keyboard arrows Values 256 to 65,535 store international characters and diacritical marks

15 Introduction to Value Types and Reference Types
A data type can be characterized as a value type or reference type Value types store data in the memory allocated to the variable Reference types store a 32-bit memory address The memory address points to the actual object

16 Value Types All value types derive from the System.ValueType class
Integral and floating-point data types are value types Dates and times are value types Structures are value types

17 Storage of value types

18 Reference Types All reference type variables are 32 bits in size
The value stored in a reference type variable is a memory address This memory address points to the memory allocated to the actual data The term pointer is often used to describe a reference type

19 Storage of reference types

20 Introduction to Variables
A variable stores data while an application runs The process of creating a variable is called declaring a variable A variable has a name (identifier) Naming rules for variables and procedures are the same A variable has a data type

21 Variable Characteristics
All variables have three characteristics Lifetime Scope Accessibility

22 Variable Lifetime Variable lifetime refers to the period of time that a variable exists A variable having local lifetime exists only while a procedure executes A variable with a module-level lifetime exists while a class instance exists or a module is loaded

23 Variable Scope and Accessibility
Variable scope refers to the statements that can use a variable Variables declared inside a procedure with the Dim statement have local scope Variables declared outside a procedure with the Private statement have module-level scope

24 Variable Declaration Syntax
[Dim] varName As type [= initexpr] Dim keyword declares a local variable The variable and its value are destroyed when the procedure ends varName contains the name (identifier) of the variable type defines the variable’s data type Optional initexpr is initializes the variable’s value

25 Declaring a Local Variable (example)
Declare a local variable named Total having the Double data type Sub Main() Dim Total As Double End Sub 25

26 Initializing Variables
Variables can be declared and initialized in the same statement Example: Declare a variable named CurrentYear and initialize it’s value to 2010: Dim CurrentYear As Integer = 2010

27 Common Initialization Errors
Numeric initialization values cannot contain formatting characters The following statements are illegal: Dim Value1 As Double = 100,000.52 Dim Value2 As Double = $100,000.52

28 Declaring Multiple Variables in One Statement
Multiple variables can be declared in the same statement Separate each declaration with a comma as follows: Dim IntegerValue As Integer, DoubleValue As Double If variables have the same data type, use the following shorthand notation: Dim IntegerValue1, IntegerValue2 As Integer

29 Variable Naming Conventions
A scheme of naming variables is called a naming convention Naming conventions improve a program’s readability Common naming conventions Hungarian notation Pascal case Camel case

30 Naming Conventions (description)
Hungarian notation: Use a prefix to denote the data type followed by a descriptive name Pascal case Use whole words Capitalize the first letter of each word Camel case Use lower case for the first letter of the first word Capitalize the first letter of each remaining word

31 Introduction to Constants
Constants are similar to variables The value of a constant does not change while an application runs Trying to assign a value to a constant will cause an error Use the Const statement to declare a constant Constants are typically declared at the module-level

32 User-defined Constant (example)
Declare a constant to store the value of PI Private Const PI_VALUE As Single _ = Declare a constant to store the value of PI multiplied by 2 Private Const TWO_PI_VALUE As Single _ = PI_VALUE * 2

33 Introduction to Expressions
Variables can be used in assignment statements along with object properties Examples: Dim Result As Integer Dim Example As Integer = 4 Result = 3 ' Literal value Result = txtExample.Height ' Property Result = Example ' Another variable

34 Common Expression Errors
Values on the right and left sides of an assignment statement must have the same data type The Font and Integer data types are not compatible so the following statements will cause an error: Dim SomeInteger As Integer SomeInteger = txtExample.Font

35 Parts of an Expression Operators Operands
Arithmetic operators such as (+, -, *, /, ^, \) Comparison and logical operators Operands Literal values Object properties Constants Variables

36 Operator Precedence Operators have an evaluation order called precedence Visual Basic precedence rules are the same as algebraic precedence rules Use parentheses to override the default precedence rules Use parentheses to clarify evaluation order Extra parentheses do no harm

37 Operators and Precedence
Programming with Microsoft Visual Basic 2008: An Object-Oriented Approach, Third Edition

38 Arithmetic Operators (example)
Divide 10 by 5 and multiply the intermediate result by 100 The value 200 is stored in Result Dim Numerator As Double = 10 Dim Denominator As Double = 5 Dim Result As Double Result = Numerator / Denominator * 100

39 Integer Division The result of an Integer division operation is an integral value The result is truncated rather than rounded Examples: Result = 4 \ 3 ' > 1 Result = 5 \ 2 ' > 2 Result = 14 \ 4 ' > 3

40 The Mod Operator Mod operator calculates the Integer remainder of a division operation The operation is the same as the remainder resulting from a long division operation Examples: Result = 4 Mod 3 ' Remainder is 1 Result = 14 Mod 4 ' Remainder is 2

41 Operator Precedence Arithmetic operators are applied in a default order known as precedence Exponentiation Multiplication and division Integer division Mod operator Addition and subtraction Use parentheses to change the default precedence

42 Numeric Data Types and Type Conversion
Type conversion is used to convert data from one data type to another How type conversion is performed depends on whether strict type checking is enabled or disabled If strict type checking is enabled, less restrictive types will not be implicitly converted to more restrictive types Strings will not be implicitly converted to numeric data types

43 Introduction to Strict Type Checking
Widening type coercion allows more restrictive types to be implicitly converted to less restrictive types Integral data types will be converted to floating-point types Short to Integer is legal Integer to Single is legal Single to Double is legal Double to Integer is not legal Integer to Short is not legal

44 Enabling Strict Type Checking
Enable strict type checking to prevent hard to find type conversion errors Option Strict statement enables or disables strict type checking Option Strict statement must appear at the beginning of a module Example: Option Strict On ' Enable Option Strict Off ' Disable

45 Explicit Type Conversion Methods
ToInt16, ToInt32, and ToInt64 convert the argument to an integral value ToDecimal converts the argument to the Decimal data type ToDouble and ToSingle convert the argument to floating-point data ToString converts the argument to a string

46 Performing Explicit Type Conversion
Members of the System.Convert class explicitly convert data from one type to another Methods exist for each primary data type Example: Dim DoubleValue As Double = Dim IntegerResult As Integer IntegerResult = _ System.Convert.ToInt32(DoubleValue)

47 Formatting Output Strings
The ToString method accepts an argument to format numeric data Named formats supply common formatting actions Currency and Percent for example Custom formats are created with placeholder characters

48 Format specifiers

49 Placeholder characters

50 The Imports Statement Use the Imports statement to eliminate fully qualified references to method names The Imports statement appears after the Option Explicit and Option Strict statements It’s possible to import any number of namespaces or classes The order in which Imports statements appear is not significant Types can also be imported using the Project Properties dialog box

51 Imports Statement (example)
Import the System.Convert class Option Explicit On Option Strict On Imports System.Convert Public Class frmMain ' statements End Class

52 The FV Method and Intrinsic Function
The FV function calculates the future value of an investment Arguments: First argument contains the interest rate Second argument contains the number of periods Third argument contains the amount of the periodic payment Optional fourth argument contains the present (initial) value of the investment Optional fifth argument defines whether payments are made at the beginning or end of the period

53 The FV Function (Example)
Calculate the future value of $1, for 12 periods at 1% per period Dim MonthRate As Integer = 0.01 Dim MonthTerm As Integer = 12 Dim InitialValue As Single = Dim FutureValue As Double FutureValue = FV(0.01, 12, 0, 1000) FutureValue = FV(MonthRate, MonthTerm, 0, _ InitialValue) ' -1,

54 The TextBox Control (Introduction)
The TextBox control gets input and displays output The control derives from the TextBoxBase class Other textual controls include the MaskedTextBox and RichTextBox controls

55 The TextBox Control (properties)
BorderStyle - defines the style of the border ForeColor and BackColor - define text and background colors Enabled - defines whether the control instance can get input focus ReadOnly - controls whether the text in the control instance can be copied to the clipboard MultiLine - defines whether text will appear on multiple lines MaxLength - defines the maximum number of characters

56 The TextBox Control (properties continued)
ScrollBars - defines whether scroll bars appear Scroll bars only appear when the MultiLine property is True Text - stores the visible text TextAlign - defines how text is aligned in the control instance The size and position is defined by the X, Y, Height and Width properties

57 The TextBox Control (Methods and Events)
Focus - used to set input focus to a text box Enter - fires when the control instance gets focus Leave - fires when the control instance loses focus

58 Introduction to Tab Order
Every form has exactly one active control instance at run time This is the control instance having input focus The order in which control instances get focus is called the tab order tab order is set using the TabIndex property tab order can also be set using a visual designer


Download ppt "IS 350 Numeric Data Types."

Similar presentations


Ads by Google