object-oriented application development using vb.net 1 chapter 4 vb.net programming with supplied...
Post on 19-Dec-2015
232 views
TRANSCRIPT
Object-Oriented Application Development Using VB .NET 1
Chapter 4
VB .NET Programming with Supplied Classes
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
• Work with dates
• Format numeric output
• Use the MessageBox class
• Display a Form
Object-Oriented Application Development Using VB .NET 3
Using the Namespaces and Classes Supplied with VB .NET
• Two important pieces of .NET framework:– Common Language Runtime (CLR)
– .NET class library
• CLR supports common services used by Visual Studio .NET languages.
• .NET class library: Huge number of predefined classes are organized into namespaces.
Object-Oriented Application Development Using VB .NET 4
Using the Namespaces and Classes Supplied with VB .NET
• Namespace : It is a container to group related
classes.
• The Namespace is a method for organizing
the .NET class library into tree form.
• It looks like the Folder concept that is used for
organizing the files existed on disks in tree form.
• Keyword Imports: gives compiler access to
classes contained in specific namespaces
Object-Oriented Application Development Using VB .NET 5
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"
• The following steps will be done to execute the above statement
1. Create variable s1 with data type String.
2. Create String instance.
3. Populate the instance with "Hello Again“ value.
4. Assigns the location of this instance to variable s1.
Object-Oriented Application Development Using VB .NET 6
Using the String Class
Object-Oriented Application Development Using VB .NET 7
Using the String Class
• String values in VB .NET are immutable – they cannot be changed by the methods appeared as changing methods.
• Methods that appear to change a string value, such as Insert, Replace, and ToUpper actually create and return a new String instance
– Those methods create and return a new String instance.
• You can change its value directly like primitive variable.– Example: Dim s1 As String = "Hello"
S1 = "Hello Again"
• Index:
– Each character in a String instance has an index
– Index values in VB .NET begin with zero
Object-Oriented Application Development Using VB .NET 8
Using String Methods
• Copy method returns a second String that is a copy of the String sent as an argument
Dim s1 As String = "Hello Again"
' create a copy of s1
Dim s2 As String = String.Copy(s1)
Console.WriteLine("s2, a copy of s1, contains " & s2)
• Chars method returns the character at a specified index • Note: Index will start from Zero value.
Console.WriteLine("char at index 6 of s1 is " & s1.Chars(6))
Object-Oriented Application Development Using VB .NET 9
Using String Methods
• There are two options to compare two string values:– Equals method
Console.WriteLine("s1.Equals(s2) returns " & s1.Equals(s2))
– Equal sign (=) , Compiler will invoke Equals method on behalf of you.
If s1 = s2 Then Console.WriteLine("s1 = s2")
Object-Oriented Application Development Using VB .NET 10
Using String Methods
• SubString method:– Extracts one or more characters from a String
instance, and
– Returns a new String instance containing extracted characters.
Console.WriteLine("s1.Substring(0, 5) returns " & _
s1.Substring(0, 5))
Object-Oriented Application Development Using VB .NET 11
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)
Object-Oriented Application Development Using VB .NET 12
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 "))
Object-Oriented Application Development Using VB .NET 13
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"))
Object-Oriented Application Development Using VB .NET 14
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
Object-Oriented Application Development Using VB .NET 15
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"))
Object-Oriented Application Development Using VB .NET 16
Using String Methods
• Use ToString method to convert numeric values to a string
' convert an integer to StringDim i As Integer = 5Console.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 & vice versa.
Object-Oriented Application Development Using VB .NET 17
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
Object-Oriented Application Development Using VB .NET 18
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 . For example: -– console.writeline(“Length of first element: ” & stringArray(0).length)
Object-Oriented Application Development Using VB .NET 19
Creating a String Array
Object-Oriented Application Development Using VB .NET 20
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
• Code to create an instance of ArrayList class:' create an ArrayList instance
Dim dynArr As ArrayList = New ArrayList( ) ‘ OR
Dim dynArr As New ArrayList( )
Object-Oriented Application Development Using VB .NET 21
Using the ArrayList Class
• Code to create four instances of String class: ' create String instancesDim 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")
• Add method is used to populate the elements• When a fourth element is added, the number of elements
increases automatically dynArr.Add(s1)
dynArr.Add(s2)
dynArr.Add(s3)
dynArr.Add(s4)
Object-Oriented Application Development Using VB .NET 22
Using the ArrayList Class
• The following Methods/Properties are defined in the ArrayList Class :
1. Contains(O) - > return True if O is in ArrayList object, and False if O is not in ArrayList object.
2. Capacity -> Gets/Sets No. of Elements.
3. Count -> return populated elements.
4. Remove(O) -> removes first occurrence of O object.
5. Reverse( ) -> reverses the element sequence.
6. IndexOf( O ) -> return the index of first occurrence of O, and –1 otherwise.
7. Item( i ) -> sets/gets the object at index i . This property can be used to loop through the array elements.
Object-Oriented Application Development Using VB .NET 23
Working with Dates
• System namespace contains DateTime class and TimeSpan class
– Instance of DateTime class contains a date & time value
– Instance of TimeSpan class contains the difference between two dates
Object-Oriented Application Development Using VB .NET 24
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
Object-Oriented Application Development Using VB .NET 25
Working with Dates
• To format a date:– Invoke its ToString method
– Pass arguments that describe the desired format
– For example: Code to return a date in the format “February 15, 2003”:
DateTime.today.ToString("MMMM dd,yyyy")
Note: Look page 147 in book for formatting date
Object-Oriented Application Development Using VB .NET 26
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 -> first DateTime instance is less than the second.• 0 -> they are equal.• +1 -> first DateTime instance greater than the second.
Object-Oriented Application Development Using VB .NET 27
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
Object-Oriented Application Development Using VB .NET 28
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("$#,###.00")
Console.WriteLine("Integer 1234 with $#,##0.00 format is " & s)
Output: $1,234.00
Object-Oriented Application Development Using VB .NET 29
Formatting Numerical Output
• Using ToString method, a number can be formatted– As currency -> argument “C” can be used
– With or without commas -> argument “N”, and “F” can be used respectively .
– As a percentage -> argument “P” can be used
– As a telephone number
– As a Social Security number• Dim sn as double = 1234556• S = sn.Tostring(“###-##-###”)
Object-Oriented Application Development Using VB .NET 30
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
Object-Oriented Application Development Using VB .NET 31
Using the MessageBox Class
• Show method can receive up to four arguments– First argument : Message to be displayed
Object-Oriented Application Development Using VB .NET 32
Using the MessageBox Class
• Show method can receive up to four arguments– Second argument : Caption to be displayed
Object-Oriented Application Development Using VB .NET 33
Using the MessageBox Class
• Show method can receive up to four arguments– Third argument : Specifies the buttons to be displayed
Object-Oriented Application Development Using VB .NET 34
Using the MessageBox Class
• Show method can receive up to four arguments– Fourth argument : Specifies the type of icon displayed
Object-Oriented Application Development Using VB .NET 35
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
– Example: -
Object-Oriented Application Development Using VB .NET 36
Example
Dim res As DialogResult
res = MessageBox.Show("Hello", "Msg", _
MessageBoxButtons.YesNoCancel, _
MessageBoxIcon.Question)
If res = DialogResult.Yes Then
MessageBox.Show("You have presses Yes")
ElseIf res = DialogResult.No Then
MessageBox.Show("You have presses No")
Else
MessageBox.Show("You have presses Cancel")
End If
Object-Oriented Application Development Using VB .NET 37
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
Object-Oriented Application Development Using VB .NET 38
Example
• Dim myForm As GuiForm• myForm = New GuiForm()• myForm.Text = "My GUI Form Demo"• myForm.BackColor = System.Drawing.Color.Brown
• ' use MessageBox to determine what to do• Dim result As DialogResult• Do Until result = DialogResult.Cancel• result = MessageBox.Show("Press Yes to Show, No to Hide, Cancel
to stop", "Form Demo", MessageBoxButtons.YesNoCancel)• If result = DialogResult.Yes Then myForm.Visible = True• If result = DialogResult.No Then myForm.Visible = False• Loop• myForm.Dispose()