1 / 63

Introduction to ASP.NET, Second Edition - Object-Oriented Programming

Learn about object-oriented programming in ASP.NET, including creating and accessing objects, using classes and inheritance, and declaring variables and their scopes.

hipolito
Download Presentation

Introduction to ASP.NET, Second Edition - Object-Oriented Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter Objectives Introduction to ASP.NET, Second Edition

  2. Object-Oriented Programming • Programming statements, or code, combined into logical groupings, functions, event handlers, and procedures Introduction to ASP.NET, Second Edition

  3. Object-Oriented Programming (continued) • Objects and Classes • create objects based upon a class • An object is a set of related code that is compartmentalized and built upon these classes • access the object across multiple Web pages • Create an object • the object definition, called the class • creating an instance of the class • use the class as the template for creating the new object Introduction to ASP.NET, Second Edition

  4. Object-Oriented Programming (continued) • Instantiation • Declaring and initializing an object from a class • You create objects from the same definition Public Class TaraStoreClass Private StoreName As String = "Tara Store" End Class Introduction to ASP.NET, Second Edition

  5. Object-Oriented Programming (continued) • Restrict applications access to the class • Public - interact with other objects outside of the base class • Private - called only from within the base class • Protected - called from within the base class, and within subclasses • Subclass – a class that inherits from a base class • Friend - called anywhere from within the same application Introduction to ASP.NET, Second Edition

  6. Object-Oriented Programming (continued) • Instantiate an object based on the class definition • Declare a variable • keyword Dim - to store the object • keyword New - to identify that this is an object based on a class definition Dim Ch5Class As New Chapter5.TaraStoreClass() Introduction to ASP.NET, Second Edition

  7. Object-Oriented Programming (continued) • Decision control structures - organize the order in which the code is executed • Event handlers - execute when the event occurs • Procedures - named grouping of one or more programming statements • Parameters - values passed; multiple values are separated in a comma delimited list within parentheses • Functions - named grouping of programming statements that can return a value Introduction to ASP.NET, Second Edition

  8. Object-Oriented Programming (continued) • Properties • Set the value of a variable defined within an object • Identified by the object name, a period, and the name • Assigned a default value within the object definition, or the value is set as “undefined” • All new objects inherit the same properties as the original object definition Introduction to ASP.NET, Second Edition

  9. Object-Oriented Programming (continued) • Inheritance • Derive the interface and behaviors from another class • Inherits keyword allows you to inherit from another .NET class • All objects are inherited from the System.Object • Properties like ToString apply to most objects • Encapsulation • Inner workings of the object are maintained within the object Introduction to ASP.NET, Second Edition

  10. Introduction to Visual Basic .NET • Windows Application • User interface is called the Form • Windows Forms tab in the Toolbox • Created from System.Windows.Forms • Web Pages • Code behind the page contains class definition • Inherits keyword • Create classes in a file, or code behind the page • Import the class into the page • Create a new object using the class Introduction to ASP.NET, Second Edition

  11. Creating a Class TaraStoreClass.vb (Page 215) • Create Chapter5 Web application, create images folder, import images, and data files • Create the TaraStoreClass class (TaraStoreClass.vb) • Modify the Code View Options • Automatic outlining and add line numbers • Create Ch5Class (in ClassVariables.aspx) • Retrieve values inherited from TaraStoreClass • View the IL using the ILDASM Introduction to ASP.NET, Second Edition

  12. TaraStoreClass.vb (continued) Introduction to ASP.NET, Second Edition

  13. TaraStoreClass.vb (continued) Introduction to ASP.NET, Second Edition

  14. ClassVariables.aspx Introduction to ASP.NET, Second Edition

  15. ClassVariables.aspx (continued) Introduction to ASP.NET, Second Edition

  16. ClassVariables.aspx (continued) Introduction to ASP.NET, Second Edition

  17. Chapter5.dll Introduction to ASP.NET, Second Edition

  18. Variables • Declaring a variable • Declaration keywords, variable name, data type • Data type identifies what kind of data the variable can store • Process of reserving the memory space for the variable before it is used in the program • Declare all variables before they are used Introduction to ASP.NET, Second Edition

  19. Variable Declaration • Where the variable is defined determines where the variable can be used within the application • Scope – where access variable • Lifetime – how long variable and its value persist in memory Introduction to ASP.NET, Second Edition

  20. Variable Declaration (continued) • Keywords specify the variable scope • Private - available only to code within the local class • Public - used outside the class • Friend - used only within the current application or project Introduction to ASP.NET, Second Edition

  21. Variable Declaration (continued) Introduction to ASP.NET, Second Edition

  22. Variable Declaration (continued) • Naming variables • Descriptive name • No commands or keywords • Begin with a letter • Do not use a period or space • Avoid special characters except underscore • Visual Basic .NET commands and variables are not case sensitive • First letter of each word is usually capitalized Introduction to ASP.NET, Second Edition

  23. Variable Declaration (continued) First Three Characters Identify the Data Type Introduction to ASP.NET, Second Edition

  24. Assigning Values to Variables • The assignment operator is the equal sign (=) • Remember Visual Basic .NET is NOT case sensitive. However, Visual Studio .NET will place Dim, As, and String in upper case characters Dim LastName, FirstName as String Dim StoreName As String = "Tara Store" Introduction to ASP.NET, Second Edition

  25. Constants • A variable that does not change • Examples - tax rates, shipping fees, and values used in mathematical equations • Declare a constant • Const keyword - Name is usually all uppercase • When you declare a constant, you need to assign the value to the constant Const TAXRATE As Integer = 8 Introduction to ASP.NET, Second Edition

  26. Concatenation • Process of joining one or more strings • Concatenation operator (&) • Can also use (+) only with strings • Join a literal string, or the result returned from an expression, or a variable that contains a string Dim lblControlContent = Ch5Class.StoreName.ToString() _ & "<br/>" & Ch5Class.StoreEmail.ToString() lblContact.Text = lblControlContent Introduction to ASP.NET, Second Edition

  27. Data Types • Reference types • Strings, classes, arrays, collections, and objects • Memory addresses stored in the managed heap • Value types • Referred to as primitive types or structures • Stores the actual data in the stack • Boolean and Char • DateTime (Date in Visual Basic .NET) • All numeric data types Introduction to ASP.NET, Second Edition

  28. String • Strings are variable in length • Do not have to specify the number of characters • Several built-in methods manipulate strings • LCase and UCase - converts case to lower and upper Dim Password As String Password = LCase(txtPassword.Value) LblPassword.Text = Password Introduction to ASP.NET, Second Edition

  29. Char • Store a single text value as a number between 0 and 65,535 • Represents a character in categories such as digit, letter, punctuation, and control characters Introduction to ASP.NET, Second Edition

  30. Numeric • Byte - stores an integer between 0 and 255 • Short - 16-bit number from -32,768 to 32,767 • Integer - 32-bit whole number • Long - 64-bit number • Real number data types • Single - a single-precision floating point number • Double - larger numbers than the single data type • Decimal - up to 28 decimal places and often used to store currency data Introduction to ASP.NET, Second Edition

  31. DateTime • Dates between 01/01/0001 and 12/31/9999 • Formats are mm/dd/yyyy and hh:mm:ss • Enclosed within a pair of pound signs Dim MyBirthday As DateTime MyBirthday = #3/22/2002# Introduction to ASP.NET, Second Edition

  32. Object Browser Introduction to ASP.NET, Second Edition

  33. Boolean • Two possible values: True or False • In binary math: • 1 represents true • 0 represents false • In Visual Studio .NET • True value is converted to -1 • False value is converted to 0 Introduction to ASP.NET, Second Edition

  34. Using a Property to Retrieve and Set the Value of a Variable • Use a variable outside of a class • Use the property function • Declare the variable public • Property methods • Used to keep private variables • To retrieve and set the variable’s value • Set the value and retrieve them indirectly Introduction to ASP.NET, Second Edition

  35. Using a Property to Retrieve and Set the Value of a Variable (continued) • ReadOnly prevents you from writing to the property • WriteOnly allows the value to be changed, but not retrieved from the property Public ReadOnly Property NewStoreName() As String Public WriteOnly Property NewStoreName()  As String Introduction to ASP.NET, Second Edition

  36. Collections • Data stored in structures called collections • System.Collections namespace provides access to classes that manage data • Each item is referred to as an element • These five collections • ArrayList, HashTable, and SortedList - access any element without having to rotate through the other elements • Queue and Stack - rotate through the collection sequentially to locate an element Introduction to ASP.NET, Second Edition

  37. The ArrayList • Stores each item in sequential order • Each item is indexed with a number • Do not need to define the size of the ArrayList • Each item is identified using an index number that indicates its position • Zero-based - first item is at position 0 • ArrayList size of 3 means it has 4 items Introduction to ASP.NET, Second Edition

  38. The ArrayList (continued) • Create an ArrayList Dim StateAbbrev As New ArrayList StateAbbrev.Add("IL") StateAbbrev.Add("MI") StateAbbrev.Add("IN") • Retrieve the value individually Response.Write(StateAbbrev(0)) • Insert an element into the first position and remove it StateAbbrev.Insert(0, "OK") StateAbbrev.Remove("OK") Introduction to ASP.NET, Second Edition

  39. The ArrayList (continued) • Properties and Methods • Add and Remove - add or delete a single element • Insert and RemoveAt - add and remove elements at a specific index position • AddRange and RemoveRange - add or remove a group of elements • IndexOf - find the position of the element in the list • A value of -1 means the element was not found in the list. • Count - identifies the number of items in the array • which will be the largest index number plus 1. • Clear - remove all of the elements Introduction to ASP.NET, Second Edition

  40. The HashTables • Creates the index of elements using an alphanumeric key like an encyclopedia • keys - a collection of alphanumeric values • values - a collection of elements • Add and Remove method • Items added using key and value pair separated with a comma • Key passed with quotation marks • Second parameter is the value Introduction to ASP.NET, Second Edition

  41. Other Collections • The SortedList Class - Indexed by both the key and the item so the index position will change frequently • The Queue Class - sequential access to the elements • Stores them in First In, First Out (FIFO) • Roller coaster ride • People in the first car are let out first • The Stack class - sequential access • Stores them in Last In, First Out (LIFO) order • Line in a theatre, church, or crowded elevator • First one to enter the room is the last one to leave Introduction to ASP.NET, Second Edition

  42. Procedures • Subprocedures • Do not return values • Cannot be used in an expression value • Event Procedures • Not executed until an event triggers the procedure • Known as an event procedure • Does not return a value • Page_Load event triggered when the page is loaded • Functions • A block of code that is grouped into a named unit. • Built-in functions inherit from a .NET Framework class • User defined functions Introduction to ASP.NET, Second Edition

  43. Subprocedures • Subprocedures • Other keywords - Public or Private, Exit Sub Sub SubprocedureName(parameters) Action and Control Statements End Sub • Call keyword – calls the procedure [Call] SubprocedureName(arguments) Introduction to ASP.NET, Second Edition

  44. Event Procedure • Event Names • Based on object name and event name • Identified with the prefix “on” and event name • Underscore (_) separate object and event name Sub objectName_eventHandler(sender as Object, e as EventArgs) action and control statements End Sub Introduction to ASP.NET, Second Edition

  45. Functions • Are declared • Public functions - visible to all other functions • Private functions - only available within the context where they are declared Public Function GetStoreName() As Integer 'This function returns an integer Return 23422 End Function Introduction to ASP.NET, Second Edition

  46. Passing an Argument to a Function • A pair of parentheses follows with zero or more arguments, also known as parameters, which are passed to the function when it is called • If no arguments are passed, you use an empty pair of parentheses • If multiple arguments are used, you use a comma to separate each argument Introduction to ASP.NET, Second Edition

  47. Returning a Value From a Function& Exiting a Function • Keyword Return • Identify the value returned to the function call • Exiting a Function • Exit Function keywords • A jumping control; jumping controls allow you to temporarily halt the execution of a code block, and move to another section of code outside the function Introduction to ASP.NET, Second Edition

  48. Exiting a Function Public Function GetStoreName() As String Dim UserName As String UserName = txtUserName.Text.ToString If UserName = "Admin" then Return "Welcome Administrator!" Exit Function Else Return "Welcome!" Exit Function End If End Function Introduction to ASP.NET, Second Edition

  49. Creating a Function Public Class TaraStoreClass Public Function GetStoreName() As String Return "Tara Store" End Function End Class GetTheFunction(New TaraStoreClass().GetStoreName) Introduction to ASP.NET, Second Edition

More Related