18
How to Work with Numeric and String Data Murach, Chapter 4, Part III, pp. 116 - 126 9/23/2008 1

Murach, Chapter 4, Part III, pp. 116 - 126 9/23/20081

Embed Size (px)

Citation preview

Page 1: Murach, Chapter 4, Part III, pp. 116 - 126 9/23/20081

1

How to Work with Numeric and String DataMurach, Chapter 4, Part III, pp. 116 - 126

9/23/2008

Page 2: Murach, Chapter 4, Part III, pp. 116 - 126 9/23/20081

2

Two Data TypesValue types : variables

Store their own dataAre supported by a structure

Reference types: variablesDo not store their own dataStore a reference to a memory locationTwo different string variables can reference the

same string location.Supported by a class (object created from class)

9/23/2008

Page 3: Murach, Chapter 4, Part III, pp. 116 - 126 9/23/20081

9/23/2008 Slide 3

Common .NET structures that define value types In the Systems namespace

Structure VB keyword The value type holds…

Byte Byte An 8-bit unsigned integer

Int16 Short A 16-bit signed integer

Int32 Integer A 32-bit signed integer

Int64 Long A 64-bit signed integer

Single Single A single-precision floating-point number

Double Double A double-precision floating-point number

Decimal Decimal A 96-bit decimal value

Boolean Boolean A True or False value

Char Char A single character

Page 4: Murach, Chapter 4, Part III, pp. 116 - 126 9/23/20081

9/23/2008 Slide 4

Common .NET classes that define reference types In the System namespace

Class name VB keyword The reference type holds…

String String A reference to a String object

Object Object A reference to any type of object

Page 5: Murach, Chapter 4, Part III, pp. 116 - 126 9/23/20081

© 2008, Mike Murach & Associates, Inc.Murach’s Visual Basic 2008, C4 Slide 5

How to declare and initialize a string Dim message1 As String = "Invalid data entry" Dim message2 As String = "" Dim message3 As String = Nothing

What is allowed in a string Letters

Numbers (digits)

Special characters like *, &, and #

Page 6: Murach, Chapter 4, Part III, pp. 116 - 126 9/23/20081

© 2008, Mike Murach & Associates, Inc.Murach’s Visual Basic 2008, C4 Slide 6

How to join a string and a number Dim price As Double = 14.95 Dim priceString As String

priceString = "Price: " & price ‘& is the concatenation operator

' priceString is "Price: 14.95"

Page 7: Murach, Chapter 4, Part III, pp. 116 - 126 9/23/20081

9/23/2008 Slide 7

How to append one string to another string Dim firstName As String = "Bob" Dim lastName As String = "Smith" Dim name As String = firstName & " " name = name & lastName

Page 8: Murach, Chapter 4, Part III, pp. 116 - 126 9/23/20081

9/23/2008 Slide 8

How to append one string to another with the &= operator

Dim firstName As String = "Bob" Dim lastName As String = "Smith" Dim name As String = firstName & " " name &= lastName ‘&= operator appends string

‘expression to string variable

Page 9: Murach, Chapter 4, Part III, pp. 116 - 126 9/23/20081

9/23/2008 Slide 9

Some of the Visual Basic functions for data conversion

Function Converts the expression to the…

CDbl(expression) Double data type

CDec(expression) Decimal data type

CInt(expression) Integer data type; any fractional portion is rounded to the nearest whole number using banker’s rounding

CLng(expression) Long data type

CStr(expression) String type

CType(expression, Typename) Specified type, which can be any type of object

Page 10: Murach, Chapter 4, Part III, pp. 116 - 126 9/23/20081

10

Other ways to convert data typesToString methodParse methodShared methods of the Convert class

9/23/2008

Page 11: Murach, Chapter 4, Part III, pp. 116 - 126 9/23/20081

© 2008, Mike Murach & Associates, Inc.Murach’s Visual Basic 2008, C4 Slide 11

Common methods for data conversion (included in all data structures)

Method Description

ToString([format]) Converts the value to its equivalent string representation using the specified format. If the format is omitted, the value isn’t formatted.

Parse(string) A shared method that converts the specified string to an equivalent data value.

*Recognizes standard numeric characters

*Does not recognize $, commas, etc.

Note: The Cdec function removes any non-standard numeric characters.

Page 12: Murach, Chapter 4, Part III, pp. 116 - 126 9/23/20081

© 2008, Mike Murach & Associates, Inc.Murach’s Visual Basic 2008, C4 Slide 12

Conversion statements that use the ToString and Parse methods – recognizes only numeric characters

Dim sales As Decimal = 2574.98D Dim salesString As String = sales.ToString sales = Decimal.Parse(salesString)

An implicit call of the ToString method Dim price As Decimal = 49.5D Dim priceString As String = "Price: $" & price

Page 13: Murach, Chapter 4, Part III, pp. 116 - 126 9/23/20081

© 2008, Mike Murach & Associates, Inc.Murach’s Visual Basic 2008, C4 Slide 13

Some of the shared methods of the Convert class (for converting all built-in types)

Method Description

ToDecimal(value) Converts the value to the Decimal data type.

ToDouble(value) Converts the value to the Double data type.

ToInt32(value) Converts the value to the Integer data type.

ToChar(value) Converts the value to the Char data type.

ToBool(value) Converts the value to the Boolean data type.

ToString(value) Converts the value to the String data type.

Page 14: Murach, Chapter 4, Part III, pp. 116 - 126 9/23/20081

© 2008, Mike Murach & Associates, Inc.Murach’s Visual Basic 2008, C4 Slide 14

Conversion statements that use the Convert class Dim subtotal As Decimal subtotal = Convert.ToDecimal(txtSubtotal.Text) Dim years As Integer = Convert.ToInt32(txtYears.Text) txtSubtotal.Text = Convert.ToString(subtotal) Dim subtotalInt As Integer = Convert.ToInt32(subtotal)

Page 15: Murach, Chapter 4, Part III, pp. 116 - 126 9/23/20081

© 2008, Mike Murach & Associates, Inc.Murach’s Visual Basic 2008, C4 Slide 15

The basic syntax of three of the formatting functions

FormatNumber(expression [, numdigitsafterdecimal]) FormatCurrency(expression [, numdigitsafterdecimal]) FormatPercent(expression [, numdigitsafterdecimal]) Computer default regional settings are used if the second argument is omitted.

Page 16: Murach, Chapter 4, Part III, pp. 116 - 126 9/23/20081

16

Other methods to convert numbers to formatted stringsThe ToString method with formatting codesThe Format method of the String classPp. 126 and 127

9/23/2008

Page 17: Murach, Chapter 4, Part III, pp. 116 - 126 9/23/20081

17

Other chapter topicsVariable Scope (on test)Enumeration (not on test) – defines a set of

related constants.Nullable types (not on test) – useful for

databases

9/23/2008

Page 18: Murach, Chapter 4, Part III, pp. 116 - 126 9/23/20081

18

Optional practiceExercise 4-1

9/23/2008