download

Matakuliah
Tahun
Versi
: M0064/Programming I
: 2005
: <<versi/revisi>>
Pertemuan 8
Collection
1
Learning Outcomes
Pada akhir pertemuan ini, diharapkan mahasiswa
akan mampu :
• Mahasiswa dapat Menjelaskan konsep dari
collection
2
Outline Materi
•
•
•
•
•
•
Form Collection
Control Collection
Perulangan dalam Collection
Add and Remove Method
Count, Item, Index Property
Membangun Collection dengan Class Module
3
Collection
• Collection adalah sekumpulan dari object yang
mempunyai struktur yang sama.
• Kelebihan Collection adalah kita bisa membuat
array object.
• Secara umum Collection mempunyai 4 buah
property dan method, yaitu Count, Item, Add dan
Remove
4
Collection
• Contoh :
colMyCollection.Item(n) ‘ atau
collMyCollection(n)
(n adalah nomor dari item yang ada, untuk pemakaian
lebih lanjut bisa menggunakan For Each)
• Form Collection dan Controls Collection adalah
collection yang sudah built-in dalam VB
– Form Collection adalah sebuah Collection
dimana setiap elemennya mewakili setiap
form dalam sebuah aplikasi
– Control Collection berisi semua object control
di dalam sebuah form
5
Cara Mengakses Collection
• Cara mengakses Collection sama dengan Array
For n=0 to colMyCollection.Count-1
colMyCollection(n).Visible= False
Next n
• Menggunakan For Each
For Each objItem in colMyCollection
objItem.Visible= False
Next objItem
6
Membuat Collection
• Untuk membangun sebuah Collection kita perlu
deklarasi variable menggunakan tipe data Collection
Dim colMyCollection as New Collection
Collection yang dibangun tersebut otomatis akan
mempunyai 4 properties dan method :
• Count property - jumlah item dalam collection
• Item property - menunjuk suatu object dalam Collection
• Add method - menempatkan object dalam Collection
• Remove method - menghapus object dalam Collection
7
Membangun Collection
dengan Class Module
• Lebih lanjut jika kita ingin mengembangkan
Collection yang sudah kita buat, kita perlu membuat
Collection kita sebagai Class Module.
• Di dalam Class module ini kita bisa menambahkan
property atau method baru sera memodifikasi
property dan method yang sudah ada
• Langkah awal kita harus buat satu class Module
yang berisi data tunggal (contoh : clsEmployee),
serta data Collection (contoh : clsColEmployee)
• Di dalam Class Module yang kita buat kita
deklarasikan Collection beserta dengan property
dan method yang kita inginkan
8
Membangun Collection
dengan Class Module
• Class module : clsEmployee
Dim xNama As String
Public Property Get Nama() As Variant
Nama = xNama
End Property
Public Property Let Nama(ByVal vNewValue As Variant)
xNama = vNewValue
End Property
9
Class Module : clsColEmployee
Private colEmployee as New Collection
Sub Tambah(objEmp as Object, Optional Key, Optional
Before,Optional After)
colEmployee.Add objEmp, Key, Before, After
End Sub
Sub Hapus
colEmployee.Remove Index
End Sub
10
Function Item(Index) as Object
Set Item = colEmployee.Item(Index)
End Function
Property Get Jumlah() as Integer
Jumlah = colEmployee.Count
End Property
11
• Sedangkan cara menggunakan Class Module yang
sudah kita buat, tinggal deklarasi dari form aplikasi
Dim objColEmployee As clsColEmployee
Dim objEmployee As clsEmployee
Private Sub Form_Load()
Set objColEmployee = New clsColEmployee
Set objEmployee = New clsEmployee
objEmployee.Nama = "Budi"
objColEmployee.Tambah objNama
objColEmployee.Item (1)
MsgBox objColEmployee.Jumlah
objColEmployee.Hapus 0
End Sub
12
SELESAI
13