43
VB Classes - 2 ISYS 573

VB Classes - 2

  • Upload
    studs

  • View
    27

  • Download
    7

Embed Size (px)

DESCRIPTION

VB Classes - 2. ISYS 573. Creating an Array of Objects. Dim emps(2) As emp Dim i As Integer For i = 0 To emps.GetUpperBound(0) emps(i) = New emp() Next emps(0).Eid = "e1" emps(0).Ename = "peter" emps(0).salary = 5000. Implementing a 1:M Relationship With Object Array. - PowerPoint PPT Presentation

Citation preview

Page 1: VB Classes - 2

VB Classes - 2

ISYS 573

Page 2: VB Classes - 2

Creating an Array of ObjectsDim emps(2) As empDim i As IntegerFor i = 0 To emps.GetUpperBound(0) emps(i) = New emp()Nextemps(0).Eid = "e1"emps(0).Ename = "peter"emps(0).salary = 5000

Page 3: VB Classes - 2

Implementing a 1:M Relationship With Object Array

Public did As StringPublic dname As StringPublic emps(2) As Employee

Public eid As StringPublic ename As StringPublic salary As Double

Class Department

Class Employee

Page 4: VB Classes - 2

Code Example

Dim tempDep As New department()

tempDep.did = "D1"

tempDep.dname = "Accounting"

tempDep.emps(0) = New emp()

tempDep.emps(0).Eid = "E1"

tempDep.emps(0).Ename = "Peter"

MessageBox.Show(tempDep.emps(0).Ename)

Page 5: VB Classes - 2

Nested Classes

• VB .Net lets you nest class definitions:– Class Outer

• …• Class Inner• …• End Class

– End Class

• The inner class can be declared as:– Dim obj As New Outer.Inner

Page 6: VB Classes - 2

Purposes to Have Nested Classes

• They are useful for organizing your classes in groups of related classes, and help solve name ambiguity.– Animal.Mouse, Peripheral.Mouse

• We can encapsulate one or more auxiliary classes inside the class that uses them and to avoid making them visible to other parts of the application. In this case, inner class should be declared as Private.

Page 7: VB Classes - 2

Using Object Array and Nested ClassPublic Class Dept Public did As String Public dname As String Public emps(2) As deptEmp Public Sub New() Dim i As Integer For i = 0 To emps.GetUpperBound(0) emps(i) = New deptEmp() Next End Sub Public Class deptEmp Public eid As String Public ename As String End Class Public Sub addemp(ByVal id As String, ByVal name As String) Static index As Integer If index > 2 Then MessageBox.Show("out of bound") Else emps(index).eid = id emps(index).ename = name index += 1 End If End SubEnd Class

Page 8: VB Classes - 2

Dim test As New Dept()

Private Sub FormTest_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

test.did = "d1"

test.dname = "MIS"

test.addemp("e1", "peter")

test.addemp("e2", "paul")

test.addemp("e3", "mary")

test.addemp("e4", "Linda")

Dim i As Integer

For i = 0 To test.emps.GetUpperBound(0)

MessageBox.Show(test.emps(i).eid & test.emps(i).ename)

Next

End Sub

Page 9: VB Classes - 2

Collections

• Collections are used to store lists of objects.• More flexible than array:

– No need to declare the number of objects in a collection, no need to ReDim.

– Objects can be added, deleted at any position.– Object can be retrieved from a collection by a

key.

• A collection’s name usually end with a “s”.

Page 10: VB Classes - 2

Using Collections• Define a collection:

– Ex. Private Customers as New Collection

• Methods:– ADD: Add object to a collection

• Dim Customer as New clsCustomer• Customers.Add(Customer)• Add an object with a key:

– Customers.Add(Customer, Customer.CID)

– Item: Retrieve an object from a collection with a position index (base 1) or with a key.

• Set Customer = Customers.Item(1)• Set Customer = Customers.Item(“C101”)

– Count: Return the number of objects in a collection.– Remove: Delete an object with a position index or key.

Page 11: VB Classes - 2

Iterating Through a CollectionDim Customer as clsCustomerDim Indx as LongFor Indx = 1 to Customers.Count

set Customer = Customers.Item(Indx)… class operations …

Next Indx

For Each Customer In Customers

class operations

Next Customer

Page 12: VB Classes - 2

Implementing a 1:M Relationship With Class Hierarchy

Public did As StringPublic dname As StringPublic emps As New Collection

Public eid As StringPublic ename As StringPublic salary As Currency

Class Department

Class Employee

Page 13: VB Classes - 2

Collection and Nested Class ExamplePublic Class Dept

Public did As String

Public dname As String

Public emps As New Collection()

Public Class deptEmp

Public eid As String

Public ename As String

End Class

Public Sub addemp(ByVal id As String, ByVal name As String)

Dim e As Dept.deptEmp

e.eid = id

e.ename = name

emps.Add(e)

End Sub

End Class

Page 14: VB Classes - 2

Dim test As New Dept()

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

test.did = "d1"

test.dname = "MIS"

test.addemp("e1", "peter")

test.addemp("e2", "paul")

Dim demp As Dept.deptEmp

For Each demp In test.emps

MessageBox.Show(demp.eid & demp.ename)

Next

Page 15: VB Classes - 2

Problem with Using Collection to Model the Entity on the Many Side of the Relationship

• Collection can store different types of objects.

• Because the property is a collection, user may use collection’s Add method to add a object of different type.– Test.Emps.Add(“Other Type”)

Page 16: VB Classes - 2

Collection Class

• A collection class holds references for a series of objects created from the same class.– Create a hidden Private collection to hold data.– Create methods to simulate collection’s Add,

Count, Items,Remove, … etc.

Page 17: VB Classes - 2

Collection Class Example

Page 18: VB Classes - 2

Public Class Dept Public did As String Public dname As String Public emps As New depEmps() Public Class depEmps Private ecol As New Collection() Public Sub add(ByVal id As String, ByVal name As String) Dim e As New emp() e.eid = id e.ename = name ecol.Add(e) End Sub Public ReadOnly Property items() As Collection Get items = ecol End Get End Property Public ReadOnly Property count() Get count = ecol.Count End Get End Property End Class Private Class emp Public eid As String Public ename As String End ClassEnd Class

Page 19: VB Classes - 2

Code Using the Collection Class

Dim test As New Dept()

Private Sub FormTest_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

test.did = "D1"

test.dname = "MIS"

test.emps.add("e1", "peter")

test.emps.add("e2", "paul")

test.emps.add("e3", "mary")

test.emps.add("e4", "linda")

Dim i As Integer

For i = 1 To test.emps.count

MessageBox.Show(test.emps.items(i).eid & test.emps.items(i).ename)

Next

End Sub

Page 20: VB Classes - 2

Public Class Emp

Public Eid As String

Public Ename As String

Public salary As Double

Public dependents As New deps()

Public Class deps

Private dcol As New Collection()

Public Sub add(ByVal did As Integer, ByVal dname As String)

Dim d As New dep()

d.depID = did

d.depName = dname

dcol.Add(d)

End Sub

Public ReadOnly Property items() As Collection

Get

items = dcol

End Get

End Property

Public ReadOnly Property count()

Get

count = dcol.Count

End Get

End Property

End Class

End Class

Page 21: VB Classes - 2

Public Class dep

Public depID As Integer

Public depName As String

End Class

Dim test1 As New Emp()

Dim test2 As dep

test1.dependents.add(1, "peter")

test1.dependents.add(2, "paul")

Dim i As Integer

For i = 1 To test1.dependents.count

MessageBox.Show(test1.dependents.items(i).depname)

Next

For Each test2 In test1.dependents.items

MessageBox.Show(test2.depID.ToString & test2.depName)

Next

Code using collection class

Page 22: VB Classes - 2

Classes and Files

• Two files with 1:M relationship– Dept.dat, and Emp.dat

• Create a Dept class to model the relationship.

• Create a form that:– Display department Ids in a listbox.– Display selected department info and its

employees in textboxes.

Page 23: VB Classes - 2

Code to Create the Listbox

Dim fileNumber As Integer

Dim did As String

Dim dname As String

Private Sub FormTest_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

fileNumber = FreeFile()

FileOpen(fileNumber, "c:\dept.dat", OpenMode.Input)

Do While Not EOF(fileNumber)

Input(fileNumber, did)

Input(fileNumber, dname)

ListBox1.Items.Add(did)

Loop

FileClose(fileNumber)

End Sub

Page 24: VB Classes - 2

Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged Dim selDep As New Dept() fileNumber = FreeFile() FileOpen(fileNumber, "c:\dept.dat", OpenMode.Input) Do While Not EOF(fileNumber) Input(fileNumber, did) Input(fileNumber, dname) If did = ListBox1.SelectedItem Then selDep.did = did selDep.dname = dname Dim fileNumber2 As Integer Dim eid As String Dim ename As String Dim empdid As String fileNumber2 = FreeFile() FileOpen(fileNumber2, "c:\emp.dat", OpenMode.Input) Do While Not EOF(fileNumber2) Input(fileNumber2, eid) Input(fileNumber2, ename) Input(fileNumber2, empdid) If empdid = ListBox1.SelectedItem Then selDep.emps.add(eid, ename) End If Loop FileClose(fileNumber2) Exit Do End If Loop

Page 25: VB Classes - 2

FileClose(fileNumber)

TextBox1.Text = selDep.dname

Dim result As String

Dim i As Integer

For i = 1 To selDep.emps.count

result = result & selDep.emps.items(i).eid & selDep.emps.items(i).ename & vbCrLf

Next

TextBox2.Text = result

TextBox3.Text = selDep.emps.count.ToString

End Sub

Page 26: VB Classes - 2

Inheritance

• The process in which a new class can be based on an existing class, and will inherit that class’s interface and behaviors. The original class is known as the base class, super class, or parent class. The inherited class is called a subclass, a derived class, or a child class.

• Inherited classes should always have an “is a” relationship with the base class.

Page 27: VB Classes - 2

Inheritance ExamplePublic Class Emp

Public Eid As String

Public Ename As String

Public salary As Double

Public Function tax() As Double

tax = salary * 0.1

End Function

End Class

Public Class secretary

Inherits Emp

Public WordsPerMinute As Integer

End Class

Page 28: VB Classes - 2

Overriding• When a property or method in the base class is not

adequate for a derived class, we can override the base class property or method by writing one with the same name in the derived class.

• The property or method in the base class must be declared with the Overridable keyword.

• The overridden property or method must be declared with the Overrides keyword.

• Note: Keywords Overridable and Overrides apply only to property procedure (not properties declared by public variables) or method.

Page 29: VB Classes - 2

Overriding a MethodPublic Class Emp

Public Eid As String

Public Ename As String

Public salary As Double

Public Overridable Function tax() As Double

tax = salary * 0.1

End Function

End Class

Public Class secretary

Inherits Emp

Public WordsPerMinute As Integer

Public Overrides Function tax() As Double

If salary > 3000 Then

tax = salary * 0.1

Else

tax = salary * 0.05

End If

End Function

End Class

Page 30: VB Classes - 2

Overriding a PropertyPublic Class Emp Public Eid As String Public Ename As String Private hiddenSal As Double Public Overridable Property salary() As Double Get salary = hiddenSal End Get Set(ByVal Value As Double) hiddenSal = Value End Set End PropertyEnd ClassPublic Class secretary Inherits Emp Private sal As Double Public WordsPerMinute As Integer Public Overrides Property salary() As Double Get salary = sal End Get Set(ByVal Value As Double) If Value > 5000 Then sal = 5000 Else sal = Value End If End Set End PropertyEnd Class

Page 31: VB Classes - 2

MyBase

• The MyBase keyword refers to the base class. It is useful when you want to reference a field, property, or method of the base class.

Page 32: VB Classes - 2

Public Overridable Function tax() As Double tax = salary * 0.1 End Function

Public Class secretary

Inherits Emp

Public WordsPerMinute As Integer

Public Overrides Function tax() As Double

If salary > 3000 Then

tax = salary * 0.1

Else

tax = salary * 0.05

End If

End Function

End Class

Public Overrides Function tax() As Double

If salary > 3000 Then

tax = MyBase.tax

Else

tax = salary * 0.05

End If

End Function

Note: With MyBase, we can reuse the code in the base class.

Page 33: VB Classes - 2

Store the Overridden Property in the Base Class Property

Public Overrides Property salary() As Double

Get

Return MyBase.salary

End Get

Set(ByVal Value As Double)

If Value > 5000 Then

MyBase.salary = 5000

Else

MyBase.salary = Value

End If

End Set

End Property

Page 34: VB Classes - 2

MyClass

• The MyClass keyword ensures that a method in a base class always uses the property and methods in that class as opposed to their overridden version in the inherited class.

Page 35: VB Classes - 2

Public Class Person Public SSN As String Public Name As String Public BirthDate As Date Public Overridable ReadOnly Property age() As Integer Get age = CInt(DateDiff(DateInterval.Year, BirthDate, Now)) If Month(Now) < Month(BirthDate) Or (Month(Now) = Month(BirthDate) And Microsoft.VisualBasic. Day(Now) < Microsoft.VisualBasic. Day(birdate)) Then age = age - 1 End If End Get End Property Public ReadOnly Property CanVote() As Boolean Get Return (age >= 18) End Get End PropertyEnd ClassPublic Class employee Inherits Person Overrides ReadOnly Property age() As Integer Get age = CInt(DateDiff(DateInterval.Year, BirthDate, Now)) End Get End PropertyEnd Class

Note: For an employee, the CanVote property will use the overridden Age in the Employee class to determine its value. To correct it, use MyClass.Age>=18 in the base class.

Page 36: VB Classes - 2

Public Variable or Public Property

• To make a property visible outside the class we should use property procedure instead of public variable because we can always change the internal implementation of a property procedure without any impact on derived classes.

Page 37: VB Classes - 2

The Scope of Class Properties and Methods

• Public: Available within its own class and to client code and subclasses. No restriction on access in the current or other projects.

• Private: Available only within its own class, not accessible from a derived class.

• Protected: Available only within its own class and derived subclasses, not available to client code.

• Friend: Available within its own class and to client code and subclasses, but only within the current project.

Page 38: VB Classes - 2

Test ScopePublic Class ClassScope

Public publicProperty As Integer

Private privateProperty As Integer

Protected protectedProperty As Integer

Friend friendProperty As Integer

End Class

Public Class derivedClassScope

Inherits ClassScope

Public Property test()

Get

test = MyBase.protectedProperty

End Get

Set(ByVal Value)

MyBase.protectedProperty = Value

End Set

End Property

End Class

Note: Test in a form.

Page 39: VB Classes - 2

Abstract Classes (Virtual Classes)

• To prevent users from using your class as is and instead force them to inherit from it, you can create an abstract class.

• An abstract class cannot be instantiated, but is designed to be used only as a base class. An abstract class is declared with the MustInherit keyword:– Public MustInherit Class Person

• Abstract classes may have methods that are declared with the MustOverride keyword. Such methods are not implemented in the abstract class but must be implemented in any derived classes.

Page 40: VB Classes - 2

Abstract Class ExamplePublic MustInherit Class clsEmp

Public Eid As String

Public Ename As String

Public salary As Double

MustOverride Function tax() As Double

End Class

Public Class clsEmpSecretary

Inherits clsEmp

Public Overrides Function tax() As Double

If salary > 5000 Then

tax = salary * 0.1

Else

tax = salary * 0.1

End If

End Function

End Class

Page 41: VB Classes - 2

NonInheritable Classes (Sealed Classes)

• A class declared with the NotInheritable keyword can be instantiated but cannot be subclassed:– Public NotInheritable Class Emp

• Use NotInheritable when you want others to be able to use your class but not base their own classes on it.

Page 42: VB Classes - 2

Base Class and Derived Class Constructors

• It is possible for both a base class and a derived class to have constructors. When an instance of the derived class is created, the base class constructor is called first, and then the derived class constructor is called.

Page 43: VB Classes - 2

Comparing Object Variables with the Is Operator

• Multiple object variables can reference the same object. To determine whether two object variables reference the same object, use the Is operator, not =.– Dim emp1 as new emp()– Dim emp2 as emp– Emp2 = emp1– If emp2 Is emp1 Then

• Msgbox(“Same object”)

– End if