Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 6: Using VB.NET Supplied Classes Visual Basic.NET Programming: From Problem Analysis to Program Design.

Similar presentations


Presentation on theme: "Chapter 6: Using VB.NET Supplied Classes Visual Basic.NET Programming: From Problem Analysis to Program Design."— Presentation transcript:

1 Chapter 6: Using VB.NET Supplied Classes Visual Basic.NET Programming: From Problem Analysis to Program Design

2 Visual Basic.NET Programming: From Problem Analysis to Program Design2 Objectives Learn more about the Framework Class Library Invoke methods in the String class Display message boxes Format numeric output Work with dates Read and write sequential files

3 Visual Basic.NET Programming: From Problem Analysis to Program Design3 Introducing the Framework Class Library Assembly –File with.dll suffix Framework Class Library –Consists of approximately 100 assemblies –Organized logically into namespaces

4 Visual Basic.NET Programming: From Problem Analysis to Program Design4 Introducing the Framework Class Library (continued) Namespace –Can contain both classes and other namespaces –System is root of all other namespaces Framework compilers do not automatically search all namespaces for classes used by code –Except System namespace –For all other namespaces Use Imports keyword

5 Visual Basic.NET Programming: From Problem Analysis to Program Design5

6 6 Invoking Methods in the String Class String data –Collection of characters String class –Member of System namespace. –Stored in reference variable Property –Special kind of VB.NET method –Access like public variable

7 Visual Basic.NET Programming: From Problem Analysis to Program Design7

8 8

9 9 Invoking Methods in the String Class (continued) String instances in VB.NET are immutable –Cannot be changed –Methods create and return new String Each character in String instance has index –Indicates position –Index for first character is 0

10 Visual Basic.NET Programming: From Problem Analysis to Program Design10

11 Visual Basic.NET Programming: From Problem Analysis to Program Design11 Using the Length Property Length property –Contains number of characters in String To access length for String s1: –s1.length

12 Visual Basic.NET Programming: From Problem Analysis to Program Design12 Using the Copy Method Creates copy of String instance Returns reference to new instance Example: –s2 = String.Copy(s1)

13 Visual Basic.NET Programming: From Problem Analysis to Program Design13 Using the Chars Method Returns character located at specified index Example: –s1.Chars(6)

14 Visual Basic.NET Programming: From Problem Analysis to Program Design14 Using the Equals Method See if two String instances contain same data Example: –s1.Equals(s2) Not same as =

15 Visual Basic.NET Programming: From Problem Analysis to Program Design15 Using the Substring Method Extract one or more characters from a String Return new String containing extracted characters Arguments: –Index of first character –Number of characters Example: –s2 = s1.Substring(0, 5)

16 Visual Basic.NET Programming: From Problem Analysis to Program Design16 Using the Replace Method Replace one or more characters in String –With one or more other characters Arguments: –String containing character(s) to be replaced –String containing replacement character(s) Example: –s2 = s1.Replace(“Hello”, “Hi”)

17 Visual Basic.NET Programming: From Problem Analysis to Program Design17 Using the Insert Method Add one or more characters into existing String –Beginning at specified index Example: –s2 = s1.Insert(6, “There “) Arguments: –Index where to begin insertion –Characters to be inserted

18 Visual Basic.NET Programming: From Problem Analysis to Program Design18 Using the StartsWith and EndsWith Methods StartsWith –Compares String with beginning character(s) of another String –Returns True or False Depending on whether there is a match EndsWith –Compares ending characters Use both methods to search for text containing specified characters

19 Visual Basic.NET Programming: From Problem Analysis to Program Design19 Using the StartsWith and EndsWith Methods (continued) StartsWith example: –s1.StartsWith(“Hi”) EndsWith example: –s1.EndsWith(s3)

20 Visual Basic.NET Programming: From Problem Analysis to Program Design20 Using the ToUpper, ToLower, IndexOf, and ToString Methods Change case of String value to uppercase or lowercase –ToUpper Uppercase –ToLower Lowercase

21 Visual Basic.NET Programming: From Problem Analysis to Program Design21 Using the ToUpper, ToLower, IndexOf, and ToString Methods (continued) IndexOf method –Search String instance for specific value –Returns index of first character of value –Or -1 If no matching value is found ToString –Convert numeric value to String

22 Visual Basic.NET Programming: From Problem Analysis to Program Design22 Formatting Numeric Output Format numeric output to make it: –More attractive –Easier to read Use ToString method to format numeric data –Argument types: Format codes Format mask

23 Visual Basic.NET Programming: From Problem Analysis to Program Design23 –If you want to format the variable d=12345.67 to show as currency, type s=d.ToString(“C”) The formatted value (d) is now contained in the string variable (s)

24 Visual Basic.NET Programming: From Problem Analysis to Program Design24 Formatting Numeric Output (continued) Format mask examples: –s = phoneNo.ToString(“(###) ###-####”) –s = d.ToString(“$#,##0.00”) –ssNo.ToString(“###-##-####”)

25 Visual Basic.NET Programming: From Problem Analysis to Program Design25 Working With Dates FCL structures: –DateTime Contains actual date value –TimeSpan Contains computed difference between two dates –Belong to System namespace

26 Visual Basic.NET Programming: From Problem Analysis to Program Design26 Working With Dates (continued) Today –Property of DateTime –Gets system date –Returns DateTime instance. –Example: todaysDate = DateTime.Today Now property –Captures current time

27 Visual Basic.NET Programming: From Problem Analysis to Program Design27 Working With Dates (continued) Format date display –Invoke ToString method –Pass arguments that describe desired format Creating DateTime instance example: –eleanorsBirthday = New DateTime(1998, 12, 15)

28 Visual Basic.NET Programming: From Problem Analysis to Program Design28

29 Visual Basic.NET Programming: From Problem Analysis to Program Design29 Comparing Dates Subtract() method –DateTime class –Compute number of days between DateTimes –Returns instance of TimeSpan class TotalDays() method –TimeSpan class –Obtain number of days in TimeSpan

30 Visual Basic.NET Programming: From Problem Analysis to Program Design30 Example 6-18: Computing the Difference between Dates 1. ' compute the difference between two dates 2. Dim daysDifference As Double, ageDifference As TimeSpan 3. ageDifference = emilysBirthday.Subtract(eleanorsBirthday) 4. daysDifference = ageDifference.TotalDays() 5. Console.WriteLine(“The age difference is “ & daysDifference)

31 Visual Basic.NET Programming: From Problem Analysis to Program Design31 Comparing Dates (continued) Compare() method –DateTime class –Compares two DateTime instances –Returns either: 0 +1

32 Visual Basic.NET Programming: From Problem Analysis to Program Design32 Performing Arithmetic with Dates DateTime class –Methods add a value to month, day, or year –Named AddMonths, AddDays, AddYears, etc. –Example: todaysDate.AddMonths(1)

33 Visual Basic.NET Programming: From Problem Analysis to Program Design33 Reading and Writing Sequential Files Classes typically employed in sequential file processing: –StreamWriter –StreamReader Database –Data organized into one or more tables –Tables can be related

34 Visual Basic.NET Programming: From Problem Analysis to Program Design34 Reading and Writing Sequential Files (continued) Files –Contain data –Organized into fields and records Field –Individual data item –Can be contained in a primitive variable or String instance Record –Consists of one or more related fields

35 Visual Basic.NET Programming: From Problem Analysis to Program Design35 Reading and Writing Sequential Files (continued) File –Contains one or more related records Sequential file –Data organized with one data item following another –System.IO namespace classes: StreamWriter StreamReader. Must use Imports statement

36 Visual Basic.NET Programming: From Problem Analysis to Program Design36 Reading and Writing Sequential Files (continued) StreamWriter class –WriteLine() method –Can overwrite or append file StreamReader class –ReadLine() method –Write loop when reading from sequential file –Check for existence of data before reading data –Use Peek() method

37 Visual Basic.NET Programming: From Problem Analysis to Program Design37 Example 6-22: Appending to a Sequential File 1. Dim customerFile As New StreamWriter("C:\Customers.txt", True) 2. customerFile.WriteLine("Graham") 3. customerFile.WriteLine("Marietta") 4. customerFile.WriteLine("467-1234") 5. customerFile.Close()

38 Visual Basic.NET Programming: From Problem Analysis to Program Design38 Programming Example: Employee Report Input –Sequential file named C:\Employee.txt containing employee information Output –Employee report containing: 1. Social Security number formatted nnn-nn-nnnn 2. Date employed formatted as monthname dd, yyyy 3. Hourly pay rate formatted as currency 4. Formatted employee name

39 Visual Basic.NET Programming: From Problem Analysis to Program Design39 Displaying Message Boxes Use message box to –Display message –Get response MessageBox class –Member of System.Windows.Forms namespace –Single method named Show Creates instance of MessageBox Makes it visible

40 Visual Basic.NET Programming: From Problem Analysis to Program Design40 Displaying Message Boxes (continued) show() method return value –Data type DialogResult –Compare to specific values using If statement

41 Visual Basic.NET Programming: From Problem Analysis to Program Design41 Displaying Message Boxes (continued) Show() method arguments –Message to display –Caption to display –Buttons to display –Type of icon to show

42 Visual Basic.NET Programming: From Problem Analysis to Program Design42 Example 6-12: Displaying message boxes 1. ' display a message 2. MessageBox.Show(“Hello Again”) 3. 'display a message and a caption 4. MessageBox.Show(“Hello Again”, “MessageBox Demo”) 5. 'display message, caption, Yes/No/Cancel buttons 6. MessageBox.Show(“Hello Again”, “MessageBox Demo”, MessageBoxButtons.YesNoCancel)

43 Visual Basic.NET Programming: From Problem Analysis to Program Design43 Example 6-12: Displaying message boxes (continued) 7. 'display message, caption, Yes/No/Cancel buttons, and Icon 8. MessageBox.Show(“Hello Again”, “MessageBox Demo”, MessageBoxButtons.YesNoCancel, MessageBoxIcon.Information)

44 Visual Basic.NET Programming: From Problem Analysis to Program Design44

45 Visual Basic.NET Programming: From Problem Analysis to Program Design45 Displaying Message Boxes (continued) Button combinations –Automatically displayed by IntelliSense After you type class name MessageBoxButtons –Select specific buttons to be displayed from list Icons –Displayed by IntelliSense when typing MessageBoxIcon

46 Visual Basic.NET Programming: From Problem Analysis to Program Design46

47 Visual Basic.NET Programming: From Problem Analysis to Program Design47

48 Visual Basic.NET Programming: From Problem Analysis to Program Design48

49 Visual Basic.NET Programming: From Problem Analysis to Program Design49 Summary Visual Basic.NET stores string data in instances of String class –Provides several useful methods and properties MessageBox –Used to display message and get response Invoke ToString method in primitive structures to format numeric data

50 Visual Basic.NET Programming: From Problem Analysis to Program Design50 Summary (continued) VB.NET uses two classes in System.IO namespace to work with sequential files: –StreamWriter –StreamReader For in-class practice: Ex. 3, 4, 5, 7, 8, 9 (type the code & run it); Hands-on Test SAMPLE. Homework: p. 212, PEx. 2; Due: 3/14/08


Download ppt "Chapter 6: Using VB.NET Supplied Classes Visual Basic.NET Programming: From Problem Analysis to Program Design."

Similar presentations


Ads by Google