Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "CIS 338: Operators and Formatting in VB.NET Dr. Ralph D. Westfall April, 2011."— Presentation transcript:

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

2 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])

3 Assignment Statements nCount = 0 sState(1) = "AK" dTotal = dTotal + dItemCost or dTotal += dItemCost 'above is extremely common code pattern sFullName = "Abraham " & "Lincoln" 'concatenation sMonth = CStr(Month(Now)) ' function

4 Math: Operator Precedence highest to lowest level (PEMDAS) 1. ( ) parentheses 2. ^exponentiation (raise to a power) 3. –negation (reverse the sign) 4. *, / multiply, divide 5. \integer division (no decimals) 6. modmodulus arithmetic (remainder) 7. +, –add, subtract calculate left to right if on same level

5 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

6 ASCII Codes (decimal values) 0 – 31 = printer control commands 32 – 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 articleWikipedia article (reasons for choices)

7 Working with ASCII Codes sChar = Chr(65) [sChar value is "A"] returns letter of code number in parentheses nChar = Asc("a") [nChar value is 97] returns ASCII code number of letter in parentheses vbCrLf = carriage return & line feed ASCII characters 13 and 10 together shows text on separate lines e.g., MsgBox

8 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 format use format style name, or format string TextBox1.Text = _'line continuation Format(CStr(1.0/7.0), "Fixed") TextBox2.Text=Format(3/0.7,"#.000")

9 Named Number Format Styles General Number – as is e.g., 1234.56789 Currency – 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

10 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 = Yes True/False: like Yes/No, 0 = False On/Off: like Yes/No, 0 = Off

11 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 100 format strings enclosed in quotes Format(fCost, "0.00") other examples: "#,###.####" "#.0%"

12 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: 1 st is for positives & zeros, 2 nd is for negatives ' "$#,##0;($#,##0)" 3 strings: 1 st is for positives, 2 nd negatives, 3 rd zeros ' "$#,##0;($#,##0);--" skip string: uses previous ' "$#,##0;;--"

13 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

14 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

15 Format Strings for Dates Format(datNow, "mm/dd/yy") month:m 7 mm 07 mmm Jul day: d 3 dd 03 ddd Wed year: yy 05 yyyy 2005 / slash separator other strings for dates, and also for time formatting

16 FormatCurrency Function defaults from Control Panel Regional and Language OptionsControl Panel Regional and Language Options numeric formatsformats FormatCurrency(Expression 'numeric [,NumDigitsAfterDecimal [,IncludeLeadingDigit * [,UseParensForNegativeNumbers * [,GroupDigits *]]]]) '*TriState.True [False, UseDefault]

17 Formatting Functions defaults [English (United States)] FormatCurrency$12,345.67 FormatNumber–12,345.67 ' – for negative #s FormatPercent 1234567% 'times 100 FormatDateTimemm/dd/yyyy hh:mm:ss Round 12346 'default = whole # if don't specify decimals

18 Working with Dates Now – gives current time/date Month(Now), Day(Now), or Year(Now) gives current month, day or year number Format(Now, "w") day of week (Sun = 1) 1 st day of week is different in some countries e.g., Monday in France Dim 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


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

Similar presentations


Ads by Google