clase 8 - 2 .ppt

Upload: eduardo-vigo-chavez

Post on 03-Jun-2018

219 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/12/2019 Clase 8 - 2 .ppt

    1/38

    Object-Oriented Application Development Using VB .NET 1

    VB .NET Programming

    with Supplied Classes

  • 8/12/2019 Clase 8 - 2 .ppt

    2/38

    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

  • 8/12/2019 Clase 8 - 2 .ppt

    3/38

  • 8/12/2019 Clase 8 - 2 .ppt

    4/38

    Object-Oriented Application Development Using VB .NET 4

    Using the Namespaces and ClassesSupplied with VB .NET

    Namespace : It is a containerto group related

    classes.

    The Namespace is a method for organizingthe

    .NET class library into tree form.

    It looks like the Folder conceptthat is used for

    organizing the files existed on disks in tree form.

    Keyword Imports: gives compiler access to

    classes contained in specific namespaces

  • 8/12/2019 Clase 8 - 2 .ppt

    5/38

    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 Systemnamespace

    Code to declare a string:

    Dim s1 As String = "Hello Again"

    The following steps will be done to execute the abovestatement

    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.

  • 8/12/2019 Clase 8 - 2 .ppt

    6/38

    Object-Oriented Application Development Using VB .NET 6

    Using the String Class

  • 8/12/2019 Clase 8 - 2 .ppt

    7/38

    Object-Oriented Application Development Using VB .NET 7

    Using the String Class

    String values in VB .NET are immutablethey cannot bechanged by the methods appeared as changing methods.

    Methods that appear to change a string value, such as

    Insert, Replace, and ToUpperactually create and return a

    new Stringinstance

    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

  • 8/12/2019 Clase 8 - 2 .ppt

    8/38

    Object-Oriented Application Development Using VB .NET 8

    Using String Methods

    Copymethod returns a second String that is a copy of theString 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)

    Charsmethod 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))

  • 8/12/2019 Clase 8 - 2 .ppt

    9/38

    Object-Oriented Application Development Using VB .NET 9

    Using String Methods

    There are two options to compare two stringvalues:

    Equalsmethod

    Console.WriteLine("s1.Equals(s2) returns " & s1.Equals(s2))

    Equalsign (=) , Compiler will invoke Equalsmethod on behalf of you.

    If s1 = s2 Then Console.WriteLine("s1 = s2")

  • 8/12/2019 Clase 8 - 2 .ppt

    10/38

    Object-Oriented Application Development Using VB .NET 10

    Using String Methods

    SubStringmethod: 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))

  • 8/12/2019 Clase 8 - 2 .ppt

    11/38

    Object-Oriented Application Development Using VB .NET 11

    Using String Methods

    Replacemethod replaces one or more charactersin 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)

  • 8/12/2019 Clase 8 - 2 .ppt

    12/38

    Object-Oriented Application Development Using VB .NET 12

    Using String Methods

    Insertmethod inserts one or more characters intoan existing string beginning at a specified index

    Console.WriteLine("s1.Insert(6, There ) returns " & _

    s1.Insert(6, "There "))

  • 8/12/2019 Clase 8 - 2 .ppt

    13/38

    Object-Oriented Application Development Using VB .NET 13

    Using String Methods

    StartsWithmethod 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"))

  • 8/12/2019 Clase 8 - 2 .ppt

    14/38

    Object-Oriented Application Development Using VB .NET 14

    Using String Methods

    EndsWithmethod compares the endingcharacters with the argument

    Console.WriteLine("s1.EndsWith(Again) returns " & _

    s1.EndsWith("Again"))

    Use ToUppermethod to change the case of astring value to uppercase

    Use ToLowermethod to change the case of astring value to lowercase

  • 8/12/2019 Clase 8 - 2 .ppt

    15/38

    Object-Oriented Application Development Using VB .NET 15

    Using String Methods

    IndexOfmethod Searches a String instance for a specific value

    Returns index of the beginning of the value

    Returns1 if no matching value was found

    Console.WriteLine("s1.indexof(Again) returns " & _s1.IndexOf("Again"))

  • 8/12/2019 Clase 8 - 2 .ppt

    16/38

    Object-Oriented Application Development Using VB .NET 16

    Using String Methods

    Use ToStringmethod to convert numeric valuesto 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 Convertclass to convert Stringvalues containing numeric data to primitive datatypes & vice versa.

  • 8/12/2019 Clase 8 - 2 .ppt

    17/38

    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 ofdata type String

  • 8/12/2019 Clase 8 - 2 .ppt

    18/38

    Object-Oriented Application Development Using VB .NET 18

    Creating a String Array

    Code to create four String instances and populate the arrayelementsstringArray(0) = "Hello"

    stringArray(1) = "World"

    stringArray(2) = "Wide"

    stringArray(3) = "Web"

    A specific element can be accessed using the index of thatelement

    String methods can be invoked for String instances

    referenced by array elements . For example: - console.writeline(Length of first element: & stringArray(0).length)

  • 8/12/2019 Clase 8 - 2 .ppt

    19/38

    Object-Oriented Application Development Using VB .NET 19

    Creating a String Array

  • 8/12/2019 Clase 8 - 2 .ppt

    20/38

    Object-Oriented Application Development Using VB .NET 20

    Using the ArrayList Class

    Major limitation of arrays: fixed size ArrayListclass

    Member of System.Collectionsnamespace

    Creates dynamically resizable arraythe 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( ) ORDim dynArr As New ArrayList( )

  • 8/12/2019 Clase 8 - 2 .ppt

    21/38

    Object-Oriented Application Development Using VB .NET 21

    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")

    Add method is used to populate the elements

    When a fourth element is added, the number of elementsincreases automatically

    dynArr.Add(s1)dynArr.Add(s2)

    dynArr.Add(s3)

    dynArr.Add(s4)

  • 8/12/2019 Clase 8 - 2 .ppt

    22/38

    Object-Oriented Application Development Using VB .NET 22

    Using the ArrayList Class

    The following Methods/Properties are defined in theArrayList 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,

    and1 otherwise.7. Item( i ) -> sets/gets the object at index i . This property

    can be used to loop through the array elements.

  • 8/12/2019 Clase 8 - 2 .ppt

    23/38

    Object-Oriented Application Development Using VB .NET 23

    Working with Dates

    Systemnamespace contains DateTime class andTimeSpan class

    Instance of DateTime class contains a date & time

    value

    Instance of TimeSpan class contains the differencebetween two dates

  • 8/12/2019 Clase 8 - 2 .ppt

    24/38

    Object-Oriented Application Development Using VB .NET 24

    Working with Dates

    DateTimeproperties

    Todayproperty gets system date and returns a

    DateTime instance

    Nowproperty captures the current time

    DateTime methods to perform arithmetic on a

    date value

    AddMonthsadds a value to the month

    AddDaysadds a value to the day

    AddYearsadds a value to the year

  • 8/12/2019 Clase 8 - 2 .ppt

    25/38

    Object-Oriented Application Development Using VB .NET 25

    Working with Dates

    To format a date: Invoke its ToStringmethod

    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 boo k for form att ing date

  • 8/12/2019 Clase 8 - 2 .ppt

    26/38

    Object-Oriented Application Development Using VB .NET 26

    Working with Dates

    Subtractmethod

    Computes the number of days between two DateTimeinstances

    Returns an instance of the TimeSpanclass

    Comparemethod

    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.

  • 8/12/2019 Clase 8 - 2 .ppt

    27/38

    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 astruc ture, which has methods

    When a primitive variable is declared, certain

    methods associated with that variable can beinvoked

    For example: ToStr ingmethod

  • 8/12/2019 Clase 8 - 2 .ppt

    28/38

    Object-Oriented Application Development Using VB .NET 28

    Formatting Numerical Output

    ToStr ingmethod 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

  • 8/12/2019 Clase 8 - 2 .ppt

    29/38

    Object-Oriented Application Development Using VB .NET29

    Formatting Numerical Output

    Using ToString method, a number can beformatted

    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(###-##-###)

  • 8/12/2019 Clase 8 - 2 .ppt

    30/38

    Object-Oriented Application Development Using VB .NET30

    Using the MessageBox Class

    A message box can be used to display a message

    and to get a response.

    MessageBoxclass

    Member of System.Windows.Formsnamespace

    Has a single method named Show

    Show method

    Creates an instance of MessageBoxclass

    Makes a message box appear

  • 8/12/2019 Clase 8 - 2 .ppt

    31/38

  • 8/12/2019 Clase 8 - 2 .ppt

    32/38

    Object-Oriented Application Development Using VB .NET32

    Using the MessageBox Class

    Show method can receive up to four arguments Second argument :Caption to be displayed

  • 8/12/2019 Clase 8 - 2 .ppt

    33/38

    Object-Oriented Application Development Using VB .NET33

    Using the MessageBox Class

    Show method can receive up to four arguments Third argument :Specifies the buttons to be displayed

  • 8/12/2019 Clase 8 - 2 .ppt

    34/38

    Object-Oriented Application Development Using VB .NET34

    Using the MessageBox Class

    Show method can receive up to four arguments Fourth argument :Specifies the type of icon displayed

  • 8/12/2019 Clase 8 - 2 .ppt

    35/38

    Object-Oriented Application Development Using VB .NET35

    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: -

  • 8/12/2019 Clase 8 - 2 .ppt

    36/38

    Object-Oriented Application Development Using VB .NET36

    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

  • 8/12/2019 Clase 8 - 2 .ppt

    37/38

    Object-Oriented Application Development Using VB .NET37

    Displaying a Form

    Form A class in System.Windows.Formsnamespace

    GuiModule.vb module contains instructions to

    Instantiate GuiForm.vb Form Make it visible and invisible

    GuiForm.vb

    A subclass of Form

    Inherits Formattributes and methods

  • 8/12/2019 Clase 8 - 2 .ppt

    38/38

    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, Cancelto stop", "Form Demo", MessageBoxButtons.YesNoCancel)

    If result = DialogResult.Yes Then myForm.Visible = True

    If result = DialogResult.No Then myForm.Visible = False

    Loop

    myForm.Dispose()