Lecture 5 Inheritance Sorting and searching List Outcomes

  • Slides: 19
Download presentation
Lecture 5 Inheritance Sorting and searching List

Lecture 5 Inheritance Sorting and searching List

Outcomes Understand inheritance n Understand how sorting is implemented in VB. NET n Understand

Outcomes Understand inheritance n Understand how sorting is implemented in VB. NET n Understand how searching is implemented in VB. NET n

Inheritance n Generalization Vs. Specialization Plants Vegetables Fruit Apple Pear Persimmon

Inheritance n Generalization Vs. Specialization Plants Vegetables Fruit Apple Pear Persimmon

Generalization/Specialization The process of finding common general attributes and operations of subclasses is called

Generalization/Specialization The process of finding common general attributes and operations of subclasses is called generalization n Specialization is a process of finding a set of subclasses reflecting special classes of a class. n

Inheritance Represents specialization in Object. Oriented world n Is used to reuse the functionality

Inheritance Represents specialization in Object. Oriented world n Is used to reuse the functionality of an existing class. n. net supports: n ¨ single inheritance ¨ multi-level inheritance.

Inheritance Public Class Child. Class Inherits Base. Class

Inheritance Public Class Child. Class Inherits Base. Class

Access modifiers Private – visible only within current class n Protected – visible only

Access modifiers Private – visible only within current class n Protected – visible only to class and it’s child classes (inheriting from it) n Friend – visible within current project n Protected Friend – visible only to class and it’s child classes defined in current project n Public – visible to everyone n

Inheritance n If a class inherits other class then the members of the class

Inheritance n If a class inherits other class then the members of the class which are defined using Public and Protected (and possibly Friend) modifiers can be accessed directly from the derived class.

Sorting List(Of ) List class defines several functions to sort: n mylist. Sort() –

Sorting List(Of ) List class defines several functions to sort: n mylist. Sort() – sorts using default comparer ¨ Good for simple types (string, integer, date) ¨ Does not work for complex types n mylist. Sort(comparer as IComparer) – uses specific class that knows how to compare complex class

Sorting List(Of ) Private Class By. Name. Comparer Implements IComparer(Of Applicant) Public Function Compare(x

Sorting List(Of ) Private Class By. Name. Comparer Implements IComparer(Of Applicant) Public Function Compare(x As Applicant, y As Applicant) _ As Integer Implements IComparer(Of Applicant). Compare Return String. Compare(x. Name, y. Name) End Function End Class

Searching in List(Of ) n n List class contains a function for searching Note

Searching in List(Of ) n n List class contains a function for searching Note – list must be sorted before call to search Dim to. Search As New Applicant Dim result As Integer = _ applicants. Binary. Search(to. Search, New By. Name. Comparer) If result > -1 Then Return applicants(result) Else Return Nothing End If

Alternative approach LINQ – Language Integrated Query n SQL-like syntax for working with n

Alternative approach LINQ – Language Integrated Query n SQL-like syntax for working with n ¨ Databases ¨ Objects ¨ XML

Sorting – LINQ to objects Dim applicants As New List(Of Applicant) Dim sorted =

Sorting – LINQ to objects Dim applicants As New List(Of Applicant) Dim sorted = From a In applicants _ Order By a. Name _ Select a

Searching – LINQ to objects Dim applicants As New List(Of Applicant) Dim filtered =

Searching – LINQ to objects Dim applicants As New List(Of Applicant) Dim filtered = From a In applicants _ Where a. Name = value _ Select a Or more complex: Dim result = From a In applicants _ Where a. Name. Contains = "a" _ And a. Surname. Starts. With("b") _ Order By a. Address Select a. Name & " " & a. Surname

LINQ IEnumerable(Of T) The result returned from the LINQ query is of special type

LINQ IEnumerable(Of T) The result returned from the LINQ query is of special type - IEnumerable(Of T) n Use any of the following functions available to IEnumerable(Of T): n ¨ Any() – returns true if the result contains at least one element ¨ Contains(a) – returns true if the result contains a specific instance

LINQ IEnumerable(Of T) Count() – returns number of elements in the result n Element.

LINQ IEnumerable(Of T) Count() – returns number of elements in the result n Element. At(i) – returns an element at the specified index. Throws exception if the index is out of bounds n Element. At. Or. Default(i) – same as above, but will return Nothing instead of exception n

LINQ IEnumerable(Of T) First() – returns first element in the collection. Throws exception if

LINQ IEnumerable(Of T) First() – returns first element in the collection. Throws exception if collection is empty n First. Or. Default() – same as above, but will return Nothing instead of exception n Last(), Last. Or. Default() – returns last element of the collection n

LINQ IEnumerable(Of T) To. Array() – converts result to array n To. List() –

LINQ IEnumerable(Of T) To. Array() – converts result to array n To. List() – converts result to List(Of ) n

The End

The End