The built-in value types Keyword Bytes Type Description Byte 1 Byte Positive integer value from 0 to 255 SByte 1 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 Integer 4 Int32 Integer from 2,147,483,648 to +2,147,483,647 UInteger 4 UInt32 Unsigned integer from 0 to 4,294,967,295 Long 8 Int64 An integer from 9,223,372,036,854,775,808 to +9,223,372,036,854,775,807 Murach s Visual Basic 2012, C4 2013, Mike Murach & Associates, Inc. Slide 1 The built-in value types (continued) Keyword Bytes Type Description ULong 8 UInt64 An unsigned integer from 0 to +18,446,744,073,709,551,615 Single 4 Single A non-integer number with approximately 7 significant digits Double 8 Double A non-integer number with approximately 14 significant digits Decimal 16 Decimal A non-integer number with up to 28 significant digits (integer and fraction) that can represent values up to 7.9228 x 10 28 Char 2 Char A single Unicode character Boolean 1 Boolean A True or False value Murach s Visual Basic 2012, C4 2013, Mike Murach & Associates, Inc. Slide 2 1
Arithmetic operators Operator Name + Addition - Subtraction * Multiplication / Division \ Integer division Mod Modulus ^ Exponentiation + Positive sign - Negative sign Assignment operators Operator Name = Assignment += Addition -= Subtraction *= Multiplication /= Division \= Integer division Precedence/Order of Operations 1. Exponentiation 2. Positive and negative 3. Multiplication and division 4. Integer division 5. Modulus 6. Addition and subtraction Murach s Visual Basic 2012, C4 2013, Mike Murach & Associates, Inc. Slide 3 How implicit casting works Widening conversions Byte Short Integer Long Decimal Integer Double Short Single Double Char Integer Implicit casts for widening conversions Dim grade As Double = 93 ' convert Integer to Double Dim a As Double = 6.5 Dim b As Integer = 6 Dim c As Integer = 10 Dim average As Double = (a + b + c) / 3 ' convert b and c to Doubles ' average = 7.5 (22.5 / 3) Murach s Visual Basic 2012, C4 2013, Mike Murach & Associates, Inc. Slide 4 2
How implicit casting works (continued) Implicit casts for narrowing conversions Dim grade As Integer = 93.75 ' grade = 94 (rounded) Dim subtotal As Decimal = txtsubtotal.text ' may throw an exception Dim average As Integer = (a + b + c) / 3 ' average = 8 By default, Visual Basic uses permissive type semantics. That means that narrowing conversions can be done implicitly. If you change to strict type semantics, narrowing conversions must be done explicitly. Option Strict On Option Explicit On 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 Murach s Visual Basic 2010, 2012, C4 2010, 2013, Mike Murach & Associates, Inc. Slide 5 Slide 5 Common.NET structures that define value types 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 Common.NET classes that define reference types 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 Murach s Visual Basic 2012, C4 2013, Mike Murach & Associates, Inc. Slide 6 3
Some of the Visual Basic functions for data conversion Function CDbl(expression) CDec(expression) CInt(expression) CLng(expression) CStr(expression) Converts the expression to the Double data type Decimal data type Integer data type; any fractional portion is rounded to the nearest whole number using banker s rounding Long data type String type CType(expression, Typename) Specified type, which can be any type of object Murach s Visual Basic 2012, C4 2013, Mike Murach & Associates, Inc. Slide 7 Examples of the data conversion functions Dim grade As Integer = CInt(93.75) ' grade = 94 Dim subtotal As Decimal = CDec(txtSubtotal.Text) ' may throw an exception Dim a As Double = 6.5 Dim b As Integer = 6 Dim c As Integer = 10 Dim average As Integer = CInt((CInt(a) + b + c) / 3) ' average = 7 Dim salesstring As String = "$2,574.98" Dim salesdecimal As Decimal = CDec(salesString) ' CDec accepts $, and. Dim salesstring = CStr(salesDecimal) ' salesstring value = 2574.98 Dim salesstring = CType(salesDecimal, String) ' same result as CStr Murach s Visual Basic 2012, C4 2013, Mike Murach & Associates, Inc. Slide 8 4
Common methods for data conversion Method ToString([format]) Parse(string) TryParse(string, result) Description A method that converts the value to its equivalent string representation using the specified format. If the format is omitted, the value isn t formatted. A shared method that converts the specified string to an equivalent data value. If the string can t be converted, an exception occurs. A static method that converts the specified string to an equivalent data value and stores it in the result variable. Returns a True value if the string is converted. Otherwise, returns a False value. Murach s Visual Basic 2012, C4 2013, Mike Murach & Associates, Inc. Slide 9 Conversion statements that use ToString, Parse, and TryParse Dim sales As Decimal = 2574.98D Dim salesstring As String = sales.tostring ' decimal to string sales = Decimal.Parse(salesString) ' string to decimal Decimal.TryParse(salesString, sales) ' string to decimal An implicit call of the ToString method Dim price As Decimal = 49.5D Dim pricestring As String = "Price: $" & price ' automatic ToString call A TryParse method that handles invalid data Dim salesstring As String = "$2574.98" Dim sales As Decimal = 0 Decimal.TryParse(salesString, sales) ' sales is 0 Murach s Visual Basic 2012, C4 2013, Mike Murach & Associates, Inc. Slide 10 5
Some of the shared methods of the Convert class Method ToDecimal(value) ToDouble(value) ToInt32(value) ToChar(value) ToBool(value) ToString(value) Description Converts the value to the Decimal data type. Converts the value to the Double data type. Converts the value to the Integer data type. Converts the value to the Char data type. Converts the value to the Boolean data type. Converts the value to a String object. Conversion statements that use the Convert class Dim subtotal As Decimal subtotal = Convert.ToDecimal(txtSubtotal.Text) ' string to decimal Dim years As Integer = Convert.ToInt32(txtYears.Text) ' string to integer txtsubtotal.text = Convert.ToString(subtotal) ' decimal to string Dim subtotalint As Integer = Convert.ToInt32(subtotal) ' decimal to integer Murach s Visual Basic 2012, C4 2013, Mike Murach & Associates, Inc. Slide 11 The basic syntax of three of the formatting functions FormatNumber(expression [, numdigitsafterdecimal]) FormatCurrency(expression [, numdigitsafterdecimal]) FormatPercent(expression [, numdigitsafterdecimal]) Statements that use the formatting functions txtdiscountamount.text = FormatNumber(discountAmount, 2) txttotal.text = FormatNumber(invoiceTotal) txttotal.text = FormatCurrency(invoiceTotal) txtdiscountpercent.text = FormatPercent(discountPct) Results of the three formatting functions Expression Result FormatNumber(-.888) -0.89 FormatNumber(-.888, 2) -0.89 FormatNumber(-.888, 1) -0.9 FormatNumber(3000) 3,000.00 FormatCurrency(3000) $3,000.00 FormatPercent(-.888) -88.80% FormatPercent(-.888, 1) -88.8% Murach s Visual Basic 2012, C4 2013, Mike Murach & Associates, Inc. Slide 12 6
Standard numeric formatting codes Code C or c P or p N or n F or f D or d E or e G or g Format Currency Percent Number Float Digits Exponential General How to use the ToString method to format a number Dim monthlyamount As String = amount.tostring("c") ' Example: $1,547.20 Dim interestrate As String = interest.tostring("p1") ' Example: 2.3% Dim quantitystring As String = quantity.tostring("n0") ' Example: 15,000 Dim paymentstring As String = payment.tostring("f3") ' Example: 432.818 Murach s Visual Basic 2012, C4 2013, Mike Murach & Associates, Inc. Slide 13 Extra 4-1 Calculate area and perimeter In this exercise, you ll create a form that accepts the length and width of a rectangle from the user and then calculates the area and perimeter of the rectangle. Murach s Visual Basic 2012, C4 2013, Mike Murach & Associates, Inc. Slide 14 7
Extra 4-2 Accumulate test score data In this exercise, you ll create a form that accepts one or more scores from the user. Each time a score is added, the score total, score count, and average score are calculated and displayed. Murach s Visual Basic 2012, C4 2013, Mike Murach & Associates, Inc. Slide 15 8