cis 338: operators and formatting in vb.net dr. ralph d. westfall april, 2011

18
CIS 338: Operators and Formatting in VB.NET Dr. Ralph D. Westfall April, 2011

Upload: noreen-horton

Post on 01-Jan-2016

216 views

Category:

Documents


1 download

TRANSCRIPT

CIS 338: Operators and Formatting in VB.NET

Dr. Ralph D. WestfallApril, 2011

Things Can Do in Statements

nAge = 57 (assign value)

sValue = CStr(1234.56) (VB function) dPrice = ListPrice(dCost) (user

function) [Call] PrintData(sStates()) (procedure) frmInput.Text = "Inputs" (set property) If nAge > 17 Then … (test value[s])

Assignment Statements

nCount = 0sState(1) = "AK"dTotal = dTotal + dItemCost or dTotal += dItemCost 'above is extremely common code pattern

sFullName = "Abraham " & "Lincoln" 'concatenation

sMonth = CStr(Month(Now)) ' function

Math: Operator Precedence

highest to lowest level (PEMDAS)1. ( ) parentheses2. ^ exponentiation (raise to a

power)3. – negation (reverse the sign)4. *, / multiply, divide5. \ integer division (no decimals)6. mod modulus arithmetic (remainder)7. +, – add, subtract

calculate left to right if on same level

Integer Division and Modulus

\ (backslash) gives integer part of result of dividing 2 integers e.g., 7\4 = 1 doesn't work with decimal numbers if

Option Strict On, decimal numbers are rounded to integers if Option Strict Off

Mod gives remainder after integer division e.g., 7 Mod 4 = 3

ASCII Codes (decimal values)

0 – 31 = printer control commands32 – 47, 58 – 64, 91 – 96 are all punctuation marks 32=space, 33=!, 34=", 35=#, 36=$, etc.

48 – 57 = 0 – 9 65 – 90 = A – Z 97 – 122 = a – z Wikipedia article (reasons for choices)

Working with ASCII Codes

sChar = Chr(65) [sChar value is "A"] returns letter of code number in

parenthesesnChar = Asc("a") [nChar value is 97] returns ASCII code number of letter in

parenthesesvbCrLf = carriage return & line feed ASCII characters 13 and 10 together shows text on separate lines e.g.,

MsgBox

Formatting Outputs

general formatting function[string].Format(argument,[format])defaults: Start>Control Panel> Regional and Language (try on own computer)argument is value or variable to formatuse format style name, or format string

TextBox1.Text = _'line continuation Format(CStr(1.0/7.0), "Fixed")

TextBox2.Text=Format(3/0.7,"#.000")

Named Number Format Styles

General Number – as is e.g., 1234.56789Currency – symbol, commas, 2 decimals e.g., $1,234.57 (rounds: 2 decimals)Fixed – 1+ digit left of decimal, 2 on right e.g., 0.91, 1234.57 (rounds: 2 places)Standard – like fixed, but with commas e.g., 1,234.57 (also rounds) 'notes

Number Format Styles - 2

Percent – 0.123 shown as 12.3%Scientific - for very big or very small #s 6.78E12 = 6,780,000,000,000 ' *10^12 6.78E-12 = 0.00000000000678 ' *10^-12

Yes/No: No = 0, all other #s = YesTrue/False: like Yes/No, 0 = FalseOn/Off: like Yes/No, 0 = Off

Format Strings for Numbers

0 – shows the digit, or 0 if it is zero# – shows digits > 0, and spaces for leading and trailing zeros (e.g., 0010.200 10.2). (decimal) , (comma) 'insert punctuation% – shows %, multiplies decimals by 100format strings enclosed in quotesFormat(fCost, "0.00") other examples: "#,###.####" "#.0%"

Format Strings for Numbers - 2

can use 1-3 argument strings to format numeric data (separated by semicolons) 1 string: for all values ' "$#,##0" 2 strings: 1st is for positives & zeros, 2nd is for

negatives ' "$#,##0;($#,##0)" 3 strings: 1st is for positives, 2nd negatives, 3rd

zeros ' "$#,##0;($#,##0);--" skip string: uses previous ' "$#,##0;;--"

Named Date/Time Format Styles

enclose date data with pound signs #yyyy-mm-dd hh:mm:ss# Format(#3/15/2010#, "General Date")General Date – date, time 10/10/01 11:46:04 PM 'VS changes to

2001

Long Date or Medium Date – Wednesday, October 13, 2010

Short Date – 10/11/2010

Named Time Format Styles

Format(#6:30:00 AM#, "Long Time") Long Time or Medium Time –

11:53:38 PM Short Time – 11:53 PM Military Time? used to be available,

but I couldn't find a predefined format for this

Format Strings for Dates

Format(datNow, "mm/dd/yy")month: m 7 mm 07 mmm Julday: d 3 dd 03 ddd Wedyear: yy 05 yyyy 2005/ slash separatorother strings for dates, and also for time formatting

FormatCurrency Function

defaults from Control Panel Regional and Language Optionsnumeric formatsFormatCurrency(Expression 'numeric [,NumDigitsAfterDecimal [,IncludeLeadingDigit * [,UseParensForNegativeNumbers * [,GroupDigits *]]]])

'*TriState.True [False, UseDefault]

Formatting Functions

defaults [English (United States)] FormatCurrency $12,345.67 FormatNumber–12,345.67

' – for negative #s FormatPercent 1234567% 'times 100 FormatDateTime mm/dd/yyyy hh:mm:ss Round 12346

'default = whole # if don't specify decimals

Working with Dates

Now – gives current time/dateMonth(Now), Day(Now), or Year(Now) gives current month, day or year number

Format(Now, "w") day of week (Sun = 1) 1st day of week is different in some

countries e.g., Monday in FranceDim datShop as Date = DateDiff("d", Now, CDate("12/25/2011")))datAddHalfMon = DateAdd("w", 2, Now) w = # of weekdays, ww= # of weeks,

m=# of months, y=# of years in formula