Murach, Chapter 4. Two Data Types Value Types – Store their own data Reference Types Do not store...
-
Upload
hillary-shepherd -
Category
Documents
-
view
226 -
download
0
description
Transcript of Murach, Chapter 4. Two Data Types Value Types – Store their own data Reference Types Do not store...
How to Work with Numeric and String Data
Murach, Chapter 4
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
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
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
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
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
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
8
Defaults for Value TypesNumber : 0Char: Binary 0Boolean: False
1/28/2010
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
10
Declare a Variable: No AS Clause?Dim numberOfTests = 8Dim numberOfTests AS Byte = 8Difference?Which would be best? Why?
1/28/2010
11
Forced Literal TypesDim total As Decimal = 24218.1928
Dim total As Decimal = 24218.1928D
Dim letter As Char = “A”C
1/28/2010
12
GoogleVisual Basic Literal Types?
1/28/2010
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
14
Constant Declaration: Syntax Const ConstantName As type = expressionConst DaysInNovember As Integer = 30
Const SalesTax As Decimal = .075DFollow variable declaration rules.
1/28/2010
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
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
17
Arithmetic Operators
1/28/2010
Operator Name + Addition - Subtraction * Multiplication / Division \ Integer division Mod Modulus ^ Exponentiation + Positive sign - Negative sign
18
Assignment Operators
1/28/2010
Operator Name = Assignment += Addition -= Subtraction *= Multiplication /= Division \= Integer division
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
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
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
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
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
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
251/28/2010
The Compile PropertiesTools>Options – Projects and Solutions, VB Defaults group, Option strict: ON