CIS 338: Operators and Formatting in VB.NET Dr. Ralph D. Westfall April, 2011
Things Can Do in Statements nAge = 57 (assign value) sValue = CStr( ) (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 = 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
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
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 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)
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
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")
Named Number Format Styles General Number – as is e.g., Currency – symbol, commas, 2 decimals e.g., $1, (rounds: 2 decimals) Fixed – 1+ digit left of decimal, 2 on right e.g., 0.91, (rounds: 2 places) Standard – like fixed, but with commas e.g., 1, (also rounds) 'notes
Number Format Styles - 2 Percent – shown as 12.3% Scientific - for very big or very small #s 6.78E12 = 6,780,000,000,000 ' *10^ E-12 = ' *10^-12 Yes/No: No = 0, all other #s = Yes True/False: like Yes/No, 0 = False On/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., 10.2). (decimal), (comma)'insert punctuation % – shows %, multiplies decimals by 100 format strings enclosed in quotes Format(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: 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;;--"
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 Jul day: d 3 dd 03 ddd Wed year: yy 05 yyyy 2005 / slash separator other strings for dates, and also for time formatting
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]
Formatting Functions defaults [English (United States)] FormatCurrency$12, FormatNumber–12, ' – for negative #s FormatPercent % 'times 100 FormatDateTimemm/dd/yyyy hh:mm:ss Round 'default = whole # if don't specify decimals
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