Chapter 5: ASP.NET Object-Oriented Programming - PowerPoint PPT Presentation

1 / 26
About This Presentation
Title:

Chapter 5: ASP.NET Object-Oriented Programming

Description:

Like creating cookies from a recipe. Each inherits properties and ... Oatmeal vs. chocolate chip cookies. Checking vs. savings account. Access Declarations ... – PowerPoint PPT presentation

Number of Views:424
Avg rating:3.0/5.0
Slides: 27
Provided by: conest
Category:

less

Transcript and Presenter's Notes

Title: Chapter 5: ASP.NET Object-Oriented Programming


1
Chapter 5 ASP.NET Object-Oriented Programming
Original slides by Kathleen Kalata Dave
Turton Modified by Meyer Tanuan
2
Basic OOP terms
  • Object
  • A set of related code
  • Properties
  • Methods
  • Class
  • A definition of an object
  • Like a cooking recipe
  • Instantiation creating objects from a class
  • Like creating cookies from a recipe
  • Each inherits properties and methods from the
    class
  • Can vary properties to create different, but
    similar, objects
  • Oatmeal vs. chocolate chip cookies
  • Checking vs. savings account

3
Access Declarations
  • Applied to properties, methods, Classes, etc.
  • Private
  • Accessible only within the class
  • Protected
  • Accessible within class sub-classes (inherit
    from the base class)
  • Friend
  • Accessible anywhere from within same assembly
  • Public
  • Accessible from outside of the class assembly
  • Eg sharing User Control between assignment
    applications

4
Methods or procedures
  • Both accept parameters
  • Values can be passed to procedure
  • Public Sub SetWeightTons(ByVal newValue As
    String)
  • ByVal - pass value of source variable (.NET
    default)
  • ByRef - pass address of source variable
  • 2 types of procedures (aka methods) (camelCase)
  • Functions
  • Return a value using return keyword
  • Subs
  • Do not return a value
  • commonly used for event handlers
  • Premature end of procedure Exit Sub, Exit
    Function
  • Use () after procedure call, even if not passing
    any parameters required in C, but not in VB.NET

5
Class example Using Procedures
  • Public Class FreightCar
  • Private weightTons as Integer
  • Public Function getWeightMetric() As Integer
  • Return weightTons 0.91
  • End Function
  • Public Sub setWeightMetric(ByVal newValue As
    Integer)
  • weightTons newValue / 0.91
  • End Sub
  • End Class

weightTons, being private, is not accessible
outside the class The procedures are public,
used to access the freight car's
weight. Programmer control store in imperial,
deliver in metric
6
Properties
  • Used to indirectly access a variable
  • Can have a set and/or a get action
  • Set
  • Sets the variable to the given value
  • Get
  • Returns the value of the variable
  • When property is to right of "", get assumed
  • When property is to left of "", set assumed

7
Class example Using Property
Note underscore on Private variable allows you
to have a property with the same name
  • Public Class FreightCar
  • Private _weightTons as Integer
  • Public Property weightMetric() As Integer
  • Set(ByVal newValue As Integer)
  • _weightTons newValue / 0.91
  • End Set
  • Get
  • Return _weightTons 0.91
  • End Get
  • End Property
  • End Class

Weight is stored in Imperial measures, but
delivered and reset in metric
8
Using Class Property
  • Dim myCar As New FreightCar
  • (Instantiate an object of type FreightCar)
  • myCar.weightMetric 7388
  • (Set action assumed)
  • lblCarWeight.Text myCar.weightMetric.ToString()
  • (Get action assumed)
  • (notice using ToString method of Integer class)

9
Constructors
  • Methods executed when an instance of a class is
    created
  • Use parameters to pre-set private variables
  • Default Constructor
  • Used when no parameters are passed
  • Assumed present when no constructors coded
  • Must explicitly define when other constructors
    present
  • Can have multiple constructors
  • Which to use
  • Type sequence of passed parameters

10
2-Constructor Example
Public Class FreightCar Private _type as
String "boxcar" Private _weight as Integer
"1200" ' constructor initialise type/weight at
instantiation Public Sub New(ByVal type as
String, ByVal tonnes as Integer) _type
type _weight tonnes / 0.91 End Sub '
default constructor use default type/weight from
class Public Sub New() ' default End
Sub ' properties to return weight or car type
Public Property weightMetric() as Integer
. Public Property type() as String
. End Class
11
Using Constructors- which constructor to use
depends on parameter type order- have one with
String, Integer and one with no parameters
Dim oilTanker as New FreightCar("tanker", 1600) '
use 1st constructor Dim oilTanker as new
FreightCar() ' use default constructor Or
oilTanker.type "tanker" oilTanker.weightmetr
ic 1600 lblMessage.Text "type of car "
oilTanker.type "ltbr /gt" lblMessage.Text
"weight, loaded "oilTanker.weightMetric.ToString
() Later creating/using a collection of these
new objects with ArrayList
12
Inheritance
  • Problem
  • A class has almost everything you need
  • Need 1 or 2 more properties, methods
  • Define a subclass
  • Inherit class's properties methods
  • Add (or override) methods, properties,
    constructors to subclass
  • Use new class instead of original one

13
Inheritance example
  • Public Class MyArray
  • Inherits ArrayList
  • ' place new methods, properties here
  • End Class
  • ' using it, in another program
  • Dim category as New MyArray()
  • category.Clear()
  • category.Add("milk")
  • category.Add("soda")
  • lstCategory.DataSource category
  • lstCategory.DataBind()
  • Your MyArray class inherited Add() and Clear()
    methods from its ArrayList parent

14
Variable Types
intCount
75
  • Primitive Types
  • Reference is address of value in the stack
  • Variable has fixed defined size cannot change
    that
  • Reference Types
  • Reference is address of a pointer to object in
    the heap (area managed by garbage collector)
  • Address is a fixed-length item
  • Garbage collector can move object data
  • Program still ref's address location
  • Which could have a different value
  • Variables memory requirements vary
  • Properties, methods vary by object
  • Value varies in size
  • May be a collection varying number of elements

strName
2ce8f965
Count 4 Value Tara Trim()
15
Variables
  • Naming
  • Begin with a letter/underscore, follow w/
    letters, numbers, underscore
  • No spaces or periods in name, avoid special
    characters
  • Declaring method or local variables
  • Dim
  • accessible only within the scope of the program
    block
  • Method body, loop body, if body
  • Declaring instance variables
  • Private (default if Dim is used)
  • Only available within the class
  • Public
  • Are accessible outside the class

16
Primitive Data Types
  • Byte
  • 8 bits 0 to 255
  • Short
  • 16 bits -32,768 to 32,767
  • Integer
  • 32 bits
  • Long
  • 64 bits
  • DateTime
  • mm/dd/yyyy hhmmss
  • Single
  • Floating point number
  • Double
  • Larger floating point number
  • Decimal
  • 28 decimals currency
  • Char
  • 16 bits 0 to 65,535
  • Boolean
  • True/False

17
String Methods
  • xx.TrimStart(), xx.TrimEnd(), xx.Trim()
  • Remove blanks from left, right, both ends
  • xx.Replace("x", "y")
  • Replace instances of char/string x with
    char/string y
  • xx.Substring(x,y)
  • Return substring starting at index x, for y
    characters
  • xx.IndexOf("", y)
  • Return index of given string, starting search at
    index y
  • xx.ToLower() xx.ToUpper()
  • Convert string to lower or upper case

18
Collections
  • An object that can store other objects (elements)
  • Elements can be any data type
  • Collection types
  • ArrayList indexed collection of same data type
  • HashTable 2-part elements key value
  • SortedList key/value pair, indexed, ordered by
    key
  • Queue sequential access FIFO
  • Stack sequential access LIFO

19
ArrayListDim nameList as New ArrayList
  • Zero-based indexing to each item
  • Properties
  • Count
  • number of elements in array
  • Methods
  • Add(), Remove()
  • add/remove an item
  • Insert(), RemoveAt()
  • add/remove item at specific index location
  • AddRange(), RemoveRange()
  • Add/remove a group of items
  • IndexOf()
  • Returns index of given element, -1 if not found
  • Clear()
  • Remove all elements

20
ArrayListcan contain complex objects
  • Dim railCars as New ArrayList
  • railCars.Add(New FreightCar("flatbed", 1000))
  • railCars.Add(New FreightCar("tanker", 2600))
  • ' accessing properties of each element
  • lblFleet.Text railCars(0).type ", "
    railCars(1).type
  • ' using with a CheckBoxList
  • chkFleet.DataSource railCars
  • chkFleet.DataTextField "type"
  • chkFleet.DataValueField "weightMetric
  • chkFleet.DataBind()

21
HashTableDim nameTitle as New HashTable
  • Indexed by an alphanumeric key (no sorting)
  • Key is first parameter of each element
  • Value is second parameter of each element
  • Properties
  • Count number of elements in array
  • Keys returns a collection of all the keys
  • e.g., myArrayList.Add(myHash.Keys)
  • Methods
  • Add(key, value)
  • Clear()
  • Item()
  • Get or set the value at the given key e.g.,
    myHash.Item(15)
  • Implicit Item reference e.g., myHash(10)
  • ContainsKey(), ContainsValue()
  • Boolean return type determine if list contains
    given key or value

22
HashTable Sample
  • Dim myHash As New Hashtable
  • myHash.Add(12, "FGH")
  • myHash(10) "ABC"
  • myHash.Item(15) "XYZ
  • lblHash.Text myHash.Item(10) ", "
    myHash(12) ", " myHash(15)

23
SortedList
  • Similar to HashTable
  • However
  • Indexed by both key and zero-based index
  • So can use some index-based methods of ArraryList
  • Index position changes as items added
  • Automatically sorts on key
  • Some Methods
  • GetKey(), GetByIndex()
  • IndexOfKey(), IndexOfValue()

24
SortedList Sample
  • Dim mySortedList As New SortedList
  • mySortedList.Add(12, "FGH")
  • mySortedList(10) "ABC"
  • mySortedList.Item(15) "XYZ"
  • ' use index 0, 1, 2 to access sorted list of keys
  • lblSorted.Text mySortedList.GetKey(0).ToString()
    ", " _
  • mySortedList.GetKey(1).ToString() ", "
    _
  • mySortedList.GetKey(2).ToString()
  • lblSorted.Text "ltbr/gt" mySortedList.GetByInde
    x(0) ", " _
  • mySortedList(12) ", "
    mySortedList.GetByIndex(2)

25
Queue
  • Sequential access to elements
  • First in, first out (FIFO)
  • Properties
  • Count
  • Number of elements in the queue
  • Methods
  • Enqueue()
  • Add an item to the tail of the queue
  • Dequeue()
  • Remove return the item at the head of the queue
  • Clear()
  • Remove all elements from the queue
  • Peek()
  • Look at the element at the head of the queue,
    w/out removing
  • Contains()
  • Boolean true if the given element is in the queue

26
Stack
  • Sequential access to elements
  • Last in, first out (LIFO)
  • Properties
  • Count
  • Number of elements in stack
  • Methods
  • Push()
  • Add an item to the top of the stack
  • Pop()
  • Remove return the item at the top of the stack
  • Clear()
  • Remove all elements from the stack
  • Peek()
  • Look at the element at the top of the stack,
    w/out removing
  • Contains()
  • Boolean true if the given element is in the
    queue
Write a Comment
User Comments (0)
About PowerShow.com