murach, chapter 4. two data types value types – store their own data reference types do not store...

25
How to Work with Numeric and String Data Murach, Chapter 4

Upload: hillary-shepherd

Post on 18-Jan-2018

220 views

Category:

Documents


0 download

DESCRIPTION

The Built-in Integer Value Types Keyword BytesTypeDescription Byte1 Byte Positive integer value from 0 to 255 SByte1SByte Signed integer value from -128 to 127 Short2Int16 Integer from –32,768 to +32,767 UShort2UInt16 Unsigned integer from 0 to 65,535 1/28/20103

TRANSCRIPT

Page 1: Murach, Chapter 4. Two Data Types Value Types – Store their own data Reference Types Do not store their own data Stores a reference to an area of memory

How to Work with Numeric and String Data

Murach, Chapter 4

Page 2: Murach, Chapter 4. Two Data Types Value Types – Store their own data Reference Types Do not store their own data Stores a reference to an area of memory

2

Two Data TypesValue Types – Store their own data

Reference Types Do not store their own dataStores a reference to an area of memory

1/28/2010

Page 3: Murach, Chapter 4. Two Data Types Value Types – Store their own data Reference Types Do not store their own data Stores a reference to an area of memory

3

The Built-in Integer Value TypesKeyword BytesType DescriptionByte 1 Byte Positive integer value from 0 to

255

SByte1 SByte Signed integer value from -128 to 127

Short 2 Int16 Integer from –32,768 to +32,767

UShort 2 UInt16 Unsigned integer from 0 to 65,535

1/28/2010

Page 4: Murach, Chapter 4. Two Data Types Value Types – Store their own data Reference Types Do not store their own data Stores a reference to an area of memory

1/28/2010 Slide 4

The .NET Framework

Operating System and Hardware

Windows 2000/2003 Windows XP Windows Vista Other Operating Systems

.NET Framework

.NET Applications

Visual Basic Visual C# Visual C++

.NET Framework Class Library

Windows Forms classes ASP.NET classes Other classes

Common Language Runtime

Managed applications Common Type System Intermediate Language

Page 5: Murach, Chapter 4. Two Data Types Value Types – Store their own data Reference Types Do not store their own data Stores a reference to an area of memory

5

The Built-in Integer Value TypesKeyword BytesType DescriptionInteger 4 Int32 Integer from –2,147,483,648

to +2,147,483,647UInteger 4 UInt32 Unsigned integer from 0 to 4,294,967,295Long 8 Int64 Integer from

–9,223,372,036,854,775,808 to +9,223,372,036,854,775,807

ULong 8 UInt64 An unsigned integer from 0 to

+18,446,744,073,709,551,615

1/28/2010

Page 6: Murach, Chapter 4. Two Data Types Value Types – Store their own data Reference Types Do not store their own data Stores a reference to an area of memory

6

The Built-in Non-Integer Number Value TypesKeyword BytesType DescriptionSingle 4 Single Approximately

7 significant digits

Double 8 Double Approximately 14 significant

digits

Decimal 16 Decimal Up to 28 significant digits (integer and fraction) that can represent values up to 79,228 x 1024

1/28/2010

Page 7: Murach, Chapter 4. Two Data Types Value Types – Store their own data Reference Types Do not store their own data Stores a reference to an area of memory

7

The Built-in Non-Number Value TypesKeyword Bytes Type DescriptionChar 2 Char A single

Unicode

character

Boolean 1 Boolean A True or False value

1/28/2010

Page 8: Murach, Chapter 4. Two Data Types Value Types – Store their own data Reference Types Do not store their own data Stores a reference to an area of memory

8

Defaults for Value TypesNumber : 0Char: Binary 0Boolean: False

1/28/2010

Page 9: Murach, Chapter 4. Two Data Types Value Types – Store their own data Reference Types Do not store their own data Stores a reference to an area of memory

9

Declare a Variable: No AS Clause?Dim price = 14.95Dim price As Double = 14.95Dim price As Decimal = 14.95Difference in the above three?When to use decimal?

1/28/2010

Page 10: Murach, Chapter 4. Two Data Types Value Types – Store their own data Reference Types Do not store their own data Stores a reference to an area of memory

10

Declare a Variable: No AS Clause?Dim numberOfTests = 8Dim numberOfTests AS Byte = 8Difference?Which would be best? Why?

1/28/2010

Page 11: Murach, Chapter 4. Two Data Types Value Types – Store their own data Reference Types Do not store their own data Stores a reference to an area of memory

11

Forced Literal TypesDim total As Decimal = 24218.1928

Dim total As Decimal = 24218.1928D

Dim letter As Char = “A”C

1/28/2010

Page 12: Murach, Chapter 4. Two Data Types Value Types – Store their own data Reference Types Do not store their own data Stores a reference to an area of memory

12

GoogleVisual Basic Literal Types?

1/28/2010

Page 13: Murach, Chapter 4. Two Data Types Value Types – Store their own data Reference Types Do not store their own data Stores a reference to an area of memory

13

NamesUse the Reddick naming conventions.Use CamelCasing.Do NOT use spaces.Use meaningful, self-documenting names.Use short names.Document variable names with comments.

1/28/2010

Page 14: Murach, Chapter 4. Two Data Types Value Types – Store their own data Reference Types Do not store their own data Stores a reference to an area of memory

14

Constant Declaration: Syntax Const ConstantName As type = expressionConst DaysInNovember As Integer = 30

Const SalesTax As Decimal = .075DFollow variable declaration rules.

1/28/2010

Page 15: Murach, Chapter 4. Two Data Types Value Types – Store their own data Reference Types Do not store their own data Stores a reference to an area of memory

15

Variable Declaration SyntaxDim variableName [As type] [= expression]For Assignments Submitted to Me:Declare at the top of the code (depending upon

scope).Always use the AS clause.Declare only one variable per lineIn the declaration, use only values (no arithmetic

expressions).Use assignment statements to initialize with

arithmetic expressions.1/28/2010

Page 16: Murach, Chapter 4. Two Data Types Value Types – Store their own data Reference Types Do not store their own data Stores a reference to an area of memory

16

Example (rather than p. 105)Dim intX As Integer = 14Dim intY As Integer = 8Dim intResult1 As Integer = 0

intResult1 = x + y

1/28/2010

Page 17: Murach, Chapter 4. Two Data Types Value Types – Store their own data Reference Types Do not store their own data Stores a reference to an area of memory

17

Arithmetic Operators

1/28/2010

Operator Name + Addition - Subtraction * Multiplication / Division \ Integer division Mod Modulus ^ Exponentiation + Positive sign - Negative sign

Page 18: Murach, Chapter 4. Two Data Types Value Types – Store their own data Reference Types Do not store their own data Stores a reference to an area of memory

18

Assignment Operators

1/28/2010

Operator Name = Assignment += Addition -= Subtraction *= Multiplication /= Division \= Integer division

Page 19: Murach, Chapter 4. Two Data Types Value Types – Store their own data Reference Types Do not store their own data Stores a reference to an area of memory

19

Assignment StatementsThe syntax for a simple assignment

statement variableName = expression

Expression can be:A literal valueA variable nameAny other type of expression

1/28/2010

Page 20: Murach, Chapter 4. Two Data Types Value Types – Store their own data Reference Types Do not store their own data Stores a reference to an area of memory

1/28/2010 Slide 20

Equivalent assignment statements total = total + 100 OR total += 100 _________________________________ total = total – 100 OR total -= 100 _________________________________

price = price * .8D OR price *= .8D

Page 21: Murach, Chapter 4. Two Data Types Value Types – Store their own data Reference Types Do not store their own data Stores a reference to an area of memory

211/28/2010

The order of precedence for arithmetic operations 1. Exponentiation 2. Positive and negative 3. Multiplication, division, integer division, and modulus 4. Addition and subtraction

Page 22: Murach, Chapter 4. Two Data Types Value Types – Store their own data Reference Types Do not store their own data Stores a reference to an area of memory

221/28/2010

How implicit casting works Widening conversions Byte Short Integer Long Decimal Single Double

Implicit casts for widening conversions Dim grade As Double = 93 Dim a As Double = 6.5 Dim b As Integer = 6 Dim c As Integer = 10

Dim average As Double = 0 Average = (a + b + c) / 3

Page 23: Murach, Chapter 4. Two Data Types Value Types – Store their own data Reference Types Do not store their own data Stores a reference to an area of memory

231/28/2010

How implicit casting works Narrowing conversions Dim grade As Integer = 93.75 Dim a As Double = 6.5 Dim b As Integer = 6 Dim c As Integer = 10

Dim average As Integer = 0 Dim subtotal As Decimal = 0D average = (a + b + c) / 3 subtotal = txtSubtotal.Text

Page 24: Murach, Chapter 4. Two Data Types Value Types – Store their own data Reference Types Do not store their own data Stores a reference to an area of memory

241/28/2010

How to code explicit casts with the CInt and CDec functions

Dim grade As Integer = CInt(93.75) ' CInt converts to Integer with rounding ' (grade = 94) Dim subtotal As Decimal = CDec(txtSubtotal.Text) ' CDec converts to Decimal ' May throw an exception Dim average As Integer = CInt((CInt(a) + b + c) / 3) ' convert a and the calculated result to Integer values; ' average = 7

Page 25: Murach, Chapter 4. Two Data Types Value Types – Store their own data Reference Types Do not store their own data Stores a reference to an area of memory

251/28/2010

The Compile PropertiesTools>Options – Projects and Solutions, VB Defaults group, Option strict: ON