Presentation is loading. Please wait.

Presentation is loading. Please wait.

Object-Oriented Application Development Using VB.NET 1 Chapter 4 VB.NET Programming with Supplied Classes.

Similar presentations


Presentation on theme: "Object-Oriented Application Development Using VB.NET 1 Chapter 4 VB.NET Programming with Supplied Classes."— Presentation transcript:

1 Object-Oriented Application Development Using VB.NET 1 Chapter 4 VB.NET Programming with Supplied Classes

2 Object-Oriented Application Development Using VB.NET 2 Objectives In this chapter, you will: Use the namespaces and classes supplied with VB.NET Use the String class Create a String array Use the ArrayList class

3 Object-Oriented Application Development Using VB.NET 3 Objectives In this chapter, you will: Work with dates Format numeric output Use the MessageBox class Display a Form

4 Object-Oriented Application Development Using VB.NET 4 Using the Namespaces and Classes Supplied with VB.NET VB.NET is part of Microsoft’s Visual Studio.NET Visual Studio.NET is part of.NET technology Two important pieces of.NET framework: –Common Language Runtime (CLR) –.NET class library CLR supports common services used by Visual Studio.NET languages

5 Object-Oriented Application Development Using VB.NET 5 Using the Namespaces and Classes Supplied with VB.NET.NET class library: predefined classes and their methods, organized into namespaces Namespace: group of related classes Keyword Imports: gives compiler access to classes contained in specific namespaces

6 Object-Oriented Application Development Using VB.NET 6 Using the String Class String: collection of characters VB.NET stores string data in instances of String class String class is a member of System namespace Code to declare a string: Dim s1 As String = "Hello Again"

7 Object-Oriented Application Development Using VB.NET 7 Using the String Class

8 Object-Oriented Application Development Using VB.NET 8 Using the String Class String values in VB.NET are immutable – they cannot be changed –Methods that appear to change a string value actually create and return a new String instance Index –Each character in a String instance has an index –Index values in VB.NET begin with zero

9 Object-Oriented Application Development Using VB.NET 9 Using String Methods Copy method returns a second String that is a copy of the String sent as an argument ' create a copy of s1 Dim s2 As String = String.Copy(s1) Console.WriteLine("s2, a copy of s1, contains " & s2)

10 Object-Oriented Application Development Using VB.NET 10 Using String Methods Chars method returns the character at a specified index Console.WriteLine("char at index 6 of s1 is " & s1.Chars(6))

11 Object-Oriented Application Development Using VB.NET 11 Using String Methods There are two options to see whether two string values are equal –Equal sign (=) If s1 = s2 Then Console.WriteLine("s1 = s2") –Equals method Console.WriteLine("s1.Equals(s2) returns " & s1.Equals(s2))

12 Object-Oriented Application Development Using VB.NET 12 Using String Methods SubString method: –Extracts one or more characters from a String instance –Returns a new String instance containing extracted characters Console.WriteLine("s1.Substring(0, 5) returns " & _ s1.Substring(0, 5))

13 Object-Oriented Application Development Using VB.NET 13 Using String Methods Replace method replaces one or more characters in a string with one or more other characters Console.WriteLine("s1.Replace(Hello, Hi) returns " & _ s1.Replace("Hello", "Hi")) Console.WriteLine("After Replace s1 contains " & s1)

14 Object-Oriented Application Development Using VB.NET 14 Using String Methods Insert method inserts one or more characters into an existing string beginning at a specified index Console.WriteLine("s1.Insert(6, There ) returns " & _ s1.Insert(6, "There "))

15 Object-Oriented Application Development Using VB.NET 15 Using String Methods StartsWith method –Compares the string argument with beginning characters of the String instance –Returns True or False depending on whether there is a match Console.WriteLine("s1.StartsWith(Hi) returns " & _ s1.StartsWith("Hi"))

16 Object-Oriented Application Development Using VB.NET 16 Using String Methods EndsWith method compares the ending characters with the argument Console.WriteLine("s1.EndsWith(Again) returns " & _ s1.EndsWith("Again")) Use ToUpper method to change the case of a string value to uppercase Use ToLower method to change the case of a string value to lowercase

17 Object-Oriented Application Development Using VB.NET 17 Using String Methods IndexOf method –Searches a String instance for a specific value –Returns index of the beginning of the value –Returns –1 if no matching value was found Console.WriteLine("s1.indexof(Again) returns " & _ s1.IndexOf("Again"))

18 Object-Oriented Application Development Using VB.NET 18 Using String Methods Use ToString method to convert numeric values to a string ' convert an integer to String Dim i As Integer = 5 Console.WriteLine("value of Convert.ToString(i) is " & _ Convert.ToString(i)) Use methods in Convert class to convert String values containing numeric data to primitive data types

19 Object-Oriented Application Development Using VB.NET 19 Creating a String Array Code to create a String array: ' declare a String array with 4 elements Dim stringArray(3) As String Elements of stringArray are reference variables of data type String

20 Object-Oriented Application Development Using VB.NET 20 Creating a String Array Code to create four String instances and populate the array elements stringArray(0) = "Hello" stringArray(1) = "World" stringArray(2) = "Wide" stringArray(3) = "Web" A specific element can be accessed using the index of that element String methods can be invoked for String instances referenced by array elements

21 Object-Oriented Application Development Using VB.NET 21 Creating a String Array

22 Object-Oriented Application Development Using VB.NET 22 Using the ArrayList Class Major limitation of arrays: fixed size ArrayList class –Member of System.Collections namespace –Creates dynamically resizable array – the number of elements can change during runtime

23 Object-Oriented Application Development Using VB.NET 23 Using the ArrayList Class Code to create an instance of ArrayList class: ' create an ArrayList instance with 3 elements Dim anArrayList As ArrayList = New ArrayList(3)

24 Object-Oriented Application Development Using VB.NET 24 Using the ArrayList Class Code to create four instances of String class: ' create String instances Dim s1 As String = New String("Hello") Dim s2 As String = New String("World") Dim s3 As String = New String("Wide") Dim s4 As String = New String("Web")

25 Object-Oriented Application Development Using VB.NET 25 Using the ArrayList Class Add method is used to populate the elements When a fourth element is added, the number of elements increases automatically anArrayList.Add(s1) anArrayList.Add(s2) anArrayList.Add(s3) anArrayList.Add(s4)

26 Object-Oriented Application Development Using VB.NET 26 Working with Dates System namespace contains DateTime class and TimeSpan class –Instance of DateTime class contains a date value –Instance of TimeSpan class contains the difference between two dates

27 Object-Oriented Application Development Using VB.NET 27 Working with Dates DateTime properties –Today property gets system date and returns a DateTime instance –Now property captures the current time DateTime methods to perform arithmetic on a date value –AddMonths adds a value to the month –AddDays adds a value to the day –AddYears adds a value to the year

28 Object-Oriented Application Development Using VB.NET 28 Working with Dates To format a date: –Invoke its ToString method –Pass arguments that describe the desired format –For example: Code to displayed a date in the format February 15, 2003: Console.WriteLine("(MMMM dd, yyyy) is " & _ today.ToString("MMMM dd,yyyy"))

29 Object-Oriented Application Development Using VB.NET 29 Working with Dates Subtract method –Computes the number of days between two DateTime instances –Returns an instance of the TimeSpan class Compare method –Compares two DateTime instances –Returns –1, 0, or +1, depending on whether the first DateTime instance is less than, equal to, or greater than the second

30 Object-Oriented Application Development Using VB.NET 30 Formatting Numerical Output Formatting means inserting commas, decimal places, dollar signs, percent symbols, parentheses, hyphens, etc. Each primitive data type is represented by a structure, which has methods When a primitive variable is declared, certain methods associated with that variable can be invoked –For example: ToString method

31 Object-Oriented Application Development Using VB.NET 31 Formatting Numerical Output ToString method is used to format numeric data Format mask –A series of characters that describes the format to be used –Passed to ToString method to carry out formatting For example: s = i.ToString("$#,##0.00") Console.WriteLine("Integer 1234 with $#,##0.00 format is " & s)

32 Object-Oriented Application Development Using VB.NET 32 Formatting Numerical Output Using ToString method, a number can be formatted –As currency –With or without a dollar sign –With or without commas –As a percentage –As a telephone number –As a Social Security number

33 Object-Oriented Application Development Using VB.NET 33 Using the MessageBox Class A message box can be used to display a message and to get a response MessageBox class –Member of System.Windows.Forms namespace –Has a single method named Show Show method –Creates an instance of MessageBox class –Makes a message box appear

34 Object-Oriented Application Development Using VB.NET 34 Using the MessageBox Class Show method can receive up to four arguments –First argument – Message to be displayed

35 Object-Oriented Application Development Using VB.NET 35 Using the MessageBox Class Show method can receive up to four arguments –Second argument – Caption to be displayed

36 Object-Oriented Application Development Using VB.NET 36 Using the MessageBox Class Show method can receive up to four arguments –Third argument – Specifies the buttons to be displayed

37 Object-Oriented Application Development Using VB.NET 37 Using the MessageBox Class Show method can receive up to four arguments –Fourth argument – Specifies the type of icon displayed

38 Object-Oriented Application Development Using VB.NET 38 Using the MessageBox Class Return value obtained from Show method –Indicates which button was clicked by user –Is of data type DialogResult –Can be compared to specific values using an If statement

39 Object-Oriented Application Development Using VB.NET 39 Displaying a Form Form –A class in System.Windows.Forms namespace GuiModule.vb module contains instructions to –Instantiate GuiForm.vb Form –Make it visible and invisible GuiForm.vb –A subclass of Form –Inherits Form attributes and methods

40 Object-Oriented Application Development Using VB.NET 40 Summary A VB.NET namespace is a library of related classes VB.NET string data is contained in an instance of String class String class contains useful methods to manipulate string data String values are immutable ArrayList class can be used to create an array that is dynamically resizable

41 Object-Oriented Application Development Using VB.NET 41 Summary A DateTime instance contains a date value A TimeSpan instance contains the difference between two dates ToString method is used to format numeric data A format mask is a series of characters that describes the format to be used A message box is used to display a message and, optionally, get a response Form is a class in System.Windows.Forms namespace


Download ppt "Object-Oriented Application Development Using VB.NET 1 Chapter 4 VB.NET Programming with Supplied Classes."

Similar presentations


Ads by Google