Chapter 4 Introduction to Numeric Data Types and Variables.

41
Chapter 4 Introduction to Numeric Data Types and Variables

Transcript of Chapter 4 Introduction to Numeric Data Types and Variables.

Chapter 4

Introduction to Numeric Data Types and Variables

Class 4: Numeric Data Types

• Process numeric data• Declare and use variables• Create user-defined constants and use

expressions• Work with numeric data types and convert

data between numeric and string data types• Define the order in which control instances

get input focus

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– Unsigned data types were new to Visual Studio

2005

Table 4-1: Integral Data Types

Introduction to 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

Table 4-3:Floating-point Data Types

Rounding Error (Introduction)

• 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

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

are faster than operations on floating-point data types

– The Decimal data type is very slow

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)

Introduction to Value Types and Reference Types

• A data type can be characterized as a value type or a reference type– Value types store data in the memory allocated

to the variable– Reference types store a 32-bit memory address

Value Types

• All value types derive from the System.ValueType class

• Integral and floating-point data types are value types

Figure 4-8: Storage of value types

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– Thus, the term pointer is often used to describe

a reference type

Figure 4-9:Storage of reference types

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)

• The naming rules for variables and procedures are the same

– A variable has a data type

Variable Characteristics

• All variables have three characteristics– A variable has a lifetime– A variable has a scope– A variable has accessibility

Declaring a Local Variable

[Dim | Static] varname As type [=initexpr]– The Dim keyword declares a local variable

• The variable and its value are destroyed when the procedure ends

– The Static keyword declares a local variable

• The variable and its value persist from one procedure invocation to the next

– varname contains the name (identifier) of the variable

– type defines the variable’s data type

– initexpr is used to initialize the variable’s value

Initializing Variables

• Variables can be declared and initialized in the same statement

• Example to declare a variable named CurrentYear and initialize its value to 2006:

Dim CurrentYear As Integer = 2006

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

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, the following shorthand notation can be used:

Dim IntegerValue1, IntegerValue2 As Integer

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 whole words– Use lower case for the first letter of the first word– Capitalize the first letter of each remaining word

User-defined 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

User-defined Constant (Example)

• Declare a constant to store the value of PIPrivate Const PI_VALUE As Single _ = 3.14159

• Declare a constant to store the value of PI multiplied by 2

Private Const PI_VALUE As Single _ = PI_VALUE * 2

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

Result = txtExample.Height

Result = Example

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

Parts of an Expression

• Operators– Arithmetic operators such as (+, -, *, /, ^, \)– Comparison and logical operators

• Operands– Literal values– Object properties– Constants– Variables

Table 4-5:Operators and Precedence

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

Strict Type Checking Rules

• 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– Integer to Single is legal, for example– Single to Double is legal– Double to Integer is not legal

Enabling Strict Type Checking

• The Option Strict statement enables or disables strict type checking

• The Option Strict statement must appear at the beginning of a module

• Enable strict type checking to prevent hard to find type conversion errors

• Example:Option Strict OnOption Strict Off

Enforcing Explicit Variable Declaration

• Explicit variable declaration requires variables to be declared before they are used– Explicit variable declaration is enabled, by

default

• Use the Option Explicit statement to enforce explicit variable declaration

• Example:Option Explicit OnOption Explicit Off

Performing Explicit Type Conversion

• The members of the System.Convert class explicitly convert data from one type to another

• Methods exist for each primary data type• Example:Dim DoubleValue1 As Double = 123.44

Dim IntegerResult As Integer

IntegerResult = _ System.Convert.ToInt32(DoubleValue1)

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 types

• ToString converts the argument to a string

Formatting Output Strings

• The ToString method accepts an argument used to format numeric data– Visual Studio supports named formats to supply

common formatting• Currency, for example

– Predefined formats can be created with format specifiers

– Custom formats can be created with placeholder characters

Table 4-6:Format Specifiers

Table 4-7:Placeholder Characters

The Imports Statement

• Use the Imports statement to eliminate the need for 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

Imports Statement (Example)

• Import the System.Convert class

Option Explicit On

Option Strict On

Imports System.Convert

Public Class frmMain

' statements

End Class

The TextBox Control (Introduction)

• The TextBox control gets input and displays output– The control derives from the TextBoxBase

class– Other textual controsl include the MaskedTextBox and RichTextBox controls

The TextBox Control (Methods and Events)

• The Focus method is used to set input focus to a text box

• The Enter event fires when the control instance gets focus

• The Leave event fires when the control instance loses focus

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– The tab order is set using the TabIndex property

– The tab order can also be set using a visual designer