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

Slides:



Advertisements
Similar presentations
Objectives Understand the software development lifecycle Perform calculations Use decision structures Perform data validation Use logical operators Use.
Advertisements

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 8- 1 Overview 8.1 An Array Type for Strings 8.2 The Standard string.
Outline IS400: Development of Business Applications on the Internet Fall 2004 Instructor: Dr. Boris Jukic JavaScript: Introduction to Scripting.
Chapter 3 - Java Programming With Supplied Classes1 Chapter 3 Java Programming With Supplied Classes.
Object-Oriented Application Development Using VB.NET 1 Creating a String Array Code to create a String array: ' declare a String array with 4 elements.
Fundamental Programming Structures in Java: Strings.
Chapter 3: Introducing the Microsoft.NET Framework and Visual Basic.NET Visual Basic.NET Programming: From Problem Analysis to Program Design.
JavaScript, Third Edition
Chapter 6: Using VB.NET Supplied Classes Visual Basic.NET Programming: From Problem Analysis to Program Design.
Chapter 7: Working with Arrays
VB .NET Programming Fundamentals
1 An Introduction to Visual Basic Objectives Explain the history of programming languages Define the terminology used in object-oriented programming.
Chapter 5 new The Do…Loop Statement
Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Ten String Manipulation and Menus.
1.
Chapter 8: String Manipulation
Programming with Microsoft Visual Basic th Edition
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to C++ Programming Outline Introduction to C++ Programming A Simple Program: Printing a.
Tutorial 14 Working with Forms and Regular Expressions.
CS0004: Introduction to Programming Input and Output.
Microsoft Visual Basic 2005: Reloaded Second Edition
COMPUTER PROGRAMMING I Objective 7.04 Apply Built-in String Functions (3%)
ASP.NET Programming with C# and SQL Server First Edition Chapter 5 Manipulating Strings with C#
Chapter 2: Using Data.
VB and C# Programming Basics. Overview Basic operations String processing Date processing Control structures Functions and subroutines.
Decisions and Debugging Part06dbg --- if/else, switch, validating data, and enhanced MessageBoxes.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Security Panel Application Introducing the Select Case Multiple-Selection Statement.
Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 3 Variables, Constants, Methods, and Calculations.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Screen Scraping Application Introducing String Processing.
Applications Development
Chapter 7: Characters, Strings, and the StringBuilder.
Variables & Function Calls. Overview u Variables  Programmer Defined & Intrinsic  Data Types  Calculation issues u Using Functions  The val() function.
String String Builder. System.String string is the alias for System.String A string is an object of class string in the System namespace representing.
Programming with Microsoft Visual Basic 2008 Fourth Edition Chapter Eight String Manipulation.
1 Scripting Languages VBScript - Recognized mainly by Internet Explorer only - Netscape does have a plug-in JavaScript - Recognized by Internet Explorer.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
CSCI 3327 Visual Basic Chapter 8: Introduction to LINQ and Collections UTPA – Fall 2011.
1 Chapter 3 – Examples The examples from chapter 3, combining the data types, variables, expressions, assignments, functions and methods with Windows controls.
Programming with Microsoft Visual Basic 2008 Fourth Edition Chapter Eight String Manipulation.
 2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 3 – Introduction to C# Programming Outline 3.1 Introduction 3.2 Simple Program: Printing a Line.
© 2006 Lawrenceville Press Slide 1 Chapter 6 The Post-Test Do…Loop Statement  Loop structure that executes a set of statements as long as a condition.
An Introduction to Programming with C++ Sixth Edition Chapter 13 Strings.
Unit 6 Repetition Processing Instructor: Brent Presley.
COMPUTER PROGRAMMING 2 ArrayLists. Objective/Essential Standard Essential Standard 3.00Apply Advanced Properties of Arrays Essential Indicator 3.02 Apply.
Chapter 6 - More About Problem Domain Classes1 Chapter 6 More About Problem Domain Classes.
Variables and Expressions Programming Right from the Start with Visual Basic.NET 1/e 7.
Object-Oriented Application Development Using VB.NET 1 Chapter 4 VB.NET Programming with Supplied Classes.
Microsoft Visual Basic 2012 CHAPTER FOUR Variables and Arithmetic Operations.
Chapter 3: Introducing the Microsoft.NET Framework and Visual Basic.NET Visual Basic.NET Programming: From Problem Analysis to Program Design.
Chapter 6: Using VB.NET Supplied Classes Visual Basic.NET Programming: From Problem Analysis to Program Design.
An Introduction to Programming with C++ Sixth Edition Chapter 5 The Selection Structure.
An Introduction to Programming with C++1 The Selection Structure Tutorial 6.
© 2010 Lawrenceville Press Slide 1 Chapter 5 The Do…Loop Statement  Loop structure that executes a set of statements as long as a condition is true. 
CSIT 108 Review Visual Basic.NET Programming: From Problem Analysis to Program Design.
.NET MCQs MULTIPLE CHOICE QUESTION
Microsoft Visual Basic 2008: Reloaded Third Edition
Strings, StringBuilder, and Character
Chapter 7: Working with Arrays
Project 9 Creating Pop-up Windows, Adding Scrolling Messages, and Validating Forms.
String String Builder.
Object Oriented System Development with VB .NET
Variables and Arithmetic Operations
WEB PROGRAMMING JavaScript.
Part A – Doing Your Own Input Validation with Simple VB Tools
CIS16 Application Development and Programming using Visual Basic.net
PHP.
Chapter 3 – Introduction to C# Programming
Tonga Institute of Higher Education
Presentation 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 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 & 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 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 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 = 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()