1 / 27

CSI 101 Elements of Computing Spring 2009

Lecture #13 Programming in Visual Basic March 25 th – April 4 th , 2009. CSI 101 Elements of Computing Spring 2009. Development Steps. Determine algorithm Know what you're doing before you start Define data What do I get from the user? What do I give to the user? Decide needed functions

ursula
Télécharger la présentation

CSI 101 Elements of Computing Spring 2009

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Lecture #13 Programming in Visual Basic March 25th – April 4th, 2009 CSI 101Elements of ComputingSpring 2009

  2. Development Steps • Determine algorithm • Know what you're doing before you start • Define data • What do I get from the user? • What do I give to the user? • Decide needed functions • What is already available in the language? • What do I need to create? • Design Forms • What should the user see? • Don't put too much on one Form • Make it easy to read 2

  3. Defining Data • List input and output • Determine best data type • For instance, a Name would be a String • Look for opportunities to create composite types • These are the user-defined types you saw earlier, like Ualb_Student or Employee • Why? Easier to catalog and associate • Fewer “loose” variables running around • Figure out source of information • Files, databases, or user input 3

  4. Library Functions • These are functions that come with the compiler • Supposed to make life easier for developers, as they don't have to “reinvent the wheel” • Main categories: • Mathematical • Type conversion • String manipulation • File manipulation (which we've already seen) • Formating • Date manipulation • There are others, but we won't discuss them in this course 4

  5. Type conversion functions • Probably most important, since all data in text boxes on Form are String • Need to convert to appropriate variable to perform calculations and arithmetic operations • All have form Ctype, where type is the data type desired • CByte, CInt, CLng – convert to Byte, Integer, Long • CDec, CSng, CDbl – convert to Decimal, Single, Double • CBool – converts 0, 1, “True”, “False”, “Yes”, “No” • CDate – converts from valid date format string 5

  6. Conversion Examples • CDate(“01/01/09”) • CDate(“September 11, 2001”) • CBool(1) • CBool(“True”) • CBool(“no”) • CInt(Text1.Text) 6

  7. Mathematical Functions • All trig functions • Log() and exp() • These we might use: • Abs() - returns absolute value • Sqrt() - returns Decimal with square root 7

  8. String Manipulation Functions • Len(String) : returns number of characters in string • Left(String, Int) : returns substring of first Int characters of String • Right(String, Int) : returns substring of last Int characters of String • LCase(String) : returns String in all lowercase • UCase(String) : returns String in all upper case • Mid(String,Pos,Len) : returns substring starting at Pos with Len characters 9

  9. String Manipulation Functions, cont • Mid(String, Pos) : returns substring starting at Pos containing the rest of String • Example: Mid(“ABCDEFG”, 3) returns “CDEFG” • StrComp(String1, String2) • Returns 0 if both strings are the same • Returns 1 if String2 would be alphabetically sorted before String1 • Returns -1 if String1 would be alphabetically sorted before String2 • LTrim(String) : returns String without leading blanks 10

  10. Last String Manipulation Functions • RTrim(String) : returns String without trailing blanks • Trim(String) : returns String without extra blanks at either end • StrReverse(String) : returns String in reverse order • InStr(String1, String2) : returns integer start position of String2 in String1 • Returns 0 if not there • InStr(“ABCDEF”,”B”) returns 2 11

  11. Format function • Format(Value, form) : returns Value formatted in the specified form • Form can be a string or a predefined format • String ex: • Format(454, “#,##0.00”) • Format(ThisDate, “mm/dd/yyyy”) • Predefined ex: • Format(ThisDate, “Long Date”) • Format(454, “Currency”) • Note value can be a String, Date, or numerical value or variable • Form is always a String 12

  12. Numerical Predefined Formats • “General” or “G” : displays without thousand separator • “Currency” or “C” : displays dollar sign, separator, and two decimal places • “Fixed” or “F” : displays at least one digit to the left of decimal and two digits after decimal • “Standard” or “S” : displays thousand separator and two decimal digits • “Percent” : Multiplies number by 100 and displays with percent sign immediately following • “P” : Multiplies number by 100 and displays with percent sign behind number, with a space in between • “Scientific” : Displays in scientific notation with two decimal digits • “E” : Displays in scientific notation with up to six decimal digits 13

  13. Special Numerical Formats • “D” : display numerical value as a string with two decimal digits • “X” : display numerical value as a hexadecimal value • Displays in Base 16 with an X following • In Base 16, 10-15 are displayed as A-F • “True/False” : displays False if value = 0, otherwise True • “Yes/No” : displays No if value = 0, otherwise Yes • “On/Off” : displays Off if value = 0, otherwise On 14

  14. Numerical Format symbols • “0” : display that digit location, even if value is 0 • Format(45,”000”) displays 045 • “#” : displays digit if there, nothing if not • Format(45, “##0”) displays 45 • “.” : decimal placeholder. What follows is number of decimal digits to display • Format(45,”0.00”) displays 45.00 • “,” : thousand separator. If value > 1000, puts comma after thousand digit • “%” : multiplies value by 100 and displays percent sign afterwards 15

  15. Predefined Date/Time formats • “General Date” or “G” : Displays in standard m(m)/d(d)/yyyy format with time following • 10/29/2008 6:50:00 PM • “Long Date” or “D” : displays whole date • Wednesday, October 29, 2008 • “Short Date” or “d” : displays standard date • 10/29/2008 • “M” : display just month and day • October 29 • “Y” : display just month and year • October, 2008 16

  16. Date/Time Format symbols • “d” : displays day without leading zero • “dd” : displays two digits for day • “ddd” : displays day as abbreviated text • Ex: “Wed” • “dddd” : displays full name for day • Month has same format, but with M • Format(Today,”MMM dd”) displays Oct 29 • Format(Today, “MM/dd”) displays 10/29 • Year has same format, but with y • Format(Today,”M/d/yy”) displays 10/29/08 17

  17. Special Format functions • FormatCurrency(value) : returns string with value in currency format • FormatPercent(value) : returns string with value in percent format • FormatDateTime(value, DateFormat) : returns string with value in specified date format: • DateFormat.GeneralDate • DateFormat.LongDate • DateFormat.ShortDate 18

  18. Date Manipulation Functions • Now() : returns current day • Today = Now() • Month(date) : returns integer value for month • Month(Today) returns 10 • MonthName(date,abbr) : returns string name of month • Abbr is a Boolean which says whether to return abbreviated name • MonthName(Today,True) returns Oct • Day(date) : returns integer value of day • Year(date) : returns integer value of year 19

  19. More Date Manipulation Functions • Weekday(date) : returns integer value of day of week (Sunday is 1) • Weekday(Today) returns 4 • WeekdayName(date,abbr) : returns string name of day of week • Abbr is Boolean – should I abbreviate? • WeekdayName(Today) returns Wed 20

  20. Date Arithmetic Functions • DateAdd(interval, amount, date) : Adds amount of interval to date and returns new date • Intervals: • “d” for day • “w” for weekday • “m” for month • “q” for quarter (three months) • “yyyy” for year • DateAdd(“m”, 1, Today) returns Nov 29, 2008 • DateDiff(interval, date1, date2) : returns integral number of intervals between date1 and date2 • DateDiff(“d”,Yesterday,Today) is 1 • DateDiff(“d”,Today,Yesterday) is -1 21

  21. Time Functions • There are functions dealing with time, but we won’t cover them in this course • See the Visual Basic Reference on www.msdn.com 22

  22. Let’s Practice • The rest of this lecture is a demonstration of creating the Form and code for the following problem statement: • “Obtain a name from the user. Search for that name in file Employee. When found, display the employee’s date of hire, current Manager, and current salary.” 23

  23. Steps • Get requirements • Determine algorithm • Define data • Map Form • Write code • Test

  24. Get requirements • Initial problem statement seems fine • Check source file to determine additional questions

  25. Employee file • Each record has these fields: • First Name • Last Name • Manager’s Name • Office number • Office phone number • Date of hire • Salary

  26. Questions to user • Since Employee name is split, do you want to search on either first name or last name? • Answer: “Yes” • Does the user need to specify the exact name, or will a “fuzzy” search do? • Answer: “fuzzy search”

  27. Okay, let’s get to work… • The rest is done on Notepad, in Visual Studio, or on whiteboard. • Slides showing Notepad and whiteboard work will be added to the end of this lecture after the Practice is complete.

More Related