BAB 2 CLASS MODULE Dalam bab ini akan dibahas mengenai pengenalan Class Module, fungsi Class Module, dan pembuatan Class Module beserta cara pengaplikasiannya ke sebuah program dalam Visual Basic 6.0. Setelah mempelajari bab ini, pembaca diharapkan mampu : Mengetahui pengertian dan fungsi Class Module. Mengetahui pembuatan Class Module dalam VB 6. Mengetahui pengaplikasian Class Module pada VB 6. BUKU PEMROGRAMAN API _ BAB 2 CLASS MODULE Page 27 Pendahuluan Tujuan BAB 2 2.1. PENGENALAN CLASS MODULE
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
BAB 2
CLASS MODULE
Dalam bab ini akan dibahas mengenai pengenalan Class
Module, fungsi Class Module, dan pembuatan Class Module beserta
cara pengaplikasiannya ke sebuah program dalam Visual Basic 6.0.
Setelah mempelajari bab ini, pembaca diharapkan mampu :
Mengetahui pengertian dan fungsi Class Module.
Mengetahui pembuatan Class Module dalam VB 6.
Mengetahui pengaplikasian Class Module pada VB 6.
BUKU PEMROGRAMAN API _ BAB 2 CLASS MODULE Page 27
Pendahuluan
Tujuan
BAB 22.1. PENGENALAN CLASS MODULE
Buatlah 5 source code berkaitan dengan bangun datar dan 5 source
code berkaitan dengan bangun ruang ! Gunakan class module untuk
menyelesaikannya. Jadikan 1 menu dengan menggunakan menu
editor.
Buka VB 6 kemudian pilih Standar EXE. Buat sebuah class module
dengan nama Class1. Di dalam class module ini berisi rumus
bangun datar dan bangun ruang. Bangun datar yang digunakan,
antara lain pesegi, persegi panjang, segitiga, lingkaran, jajargenjang,
belahketupat, dan layang-layang. Bangun ruang yang digunakan
yaitu kubus, balok, kerucut, limas segiempat, tabung, dan bola.
Berikut source codenya :
Public Function lpersegi(ByVal S As Double) As Double
lpersegi = S * S
End Function
Public Function kpersegi(ByVal S As Double) As Double
kpersegi = S + S
End Function
Public Function lpanjang(ByVal p As Double, l As Double) As
Double
lpanjang = p * l
End Function
Public Function kpanjang(ByVal p As Double, l As Double) As
Double
kpanjang = 2 * (p + l)
End Function
Public Function lsegitiga(ByVal a As Double, t As Double) As
Double
lsegitiga = 0.5 * (a * t)
End Function
Public Function vkubus(ByVal S As Double) As Double
vkubus = S ^ 3
End Function
Public Function vbalok(ByVal p As Double, l As Double, t As
Double) As Double
vbalok = p * l * t
End Function
Public Function llingkaran(ByVal r As Double) As Double
Dim phi As Double
phi = 3.14
BUKU PEMROGRAMAN API _ BAB 2 CLASS MODULE Page 27
SOAL LATIHAN
JAWABAN
llingkaran = phi * (r * r)
End Function
Public Function ljg(ByVal a As Double, t As Double) As Double
ljg = a * t
End Function
Public Function lbkll(ByVal d1 As Double, d2 As Double) As
Double
lbkll = 0.5 * d1 * d2
End Function
Public Function vkerucut(ByVal r As Double, t As Double) As
Double
Dim phi As Double
phi = 3.14
vkerucut = phi * (r ^ 2) * t * (1 / 3)
End Function
Public Function lkerucut(ByVal r As Double, S As Double) As
Double
Dim phi As Double
phi = 3.14
lkerucut = (phi * r) * (S * r)
End Function
Public Function vlimas(ByVal p As Double, l As Double, t As
Double) As Double
vlimas = p * l * t * (1 / 3)
End Function
Public Function llimas(ByVal p As Double, l As Double, t As
Double) As Double
llimas = ((p + l) * t) + (p * l)
End Function
Public Function vtabung(ByVal r As Double, t As Double) As
Double
Dim phi As Double
phi = 3.14
vtabung = phi * (r ^ 2) * t
End Function
Public Function ltabung(ByVal r As Double, t As Double) As
Double
Dim phi As Double
phi = 3.14
ltabung = (phi * (r * 2)) * (t * r)
End Function
Public Function vbola(ByVal r As Double, t As Double) As Double
Dim phi As Double
BUKU PEMROGRAMAN API _ BAB 2 CLASS MODULE Page 27
phi = 3.14
vbola = (phi * r * (t ^ 3)) * (4 / 3)
End Function
Public Function lbola(ByVal r As Double, t As Double) As Double
Dim phi As Double
phi = 3.14
lbola = 4 * (phi * (r ^ 2))
End Function
Buat 13 form yang akan digunakan untuk masing-masing bangun
datar dan bangun ruang.
Form1 :
Pertama buatlah Form1 untuk bangun datar persegi dan simpan
dengan nama bdPersegi.frm. Di dalam Form1, buat object sebagai
berikut :
a. Label1, Name = Label1; Caption = PERSEGI
b. Label2, Name = Label2; Caption = SISI
c. Label3, Name = Label3; Caption = HASIL:
d. Label4, Name = Label4; Caption = -
e. TextBox1, Name = Text1; Caption = -
f. CommandButton1, Name = Command1; Caption = LUAS
g. CommandButton2, Name = Command1; Caption = KELILING
h. CommandButton3, Name = Command2; Caption = HAPUS
Deklarasi class module menjadi variabel bertipe object :
Dim hasil As New Class1
Perhatikan bahwa class module yaitu Class1 akan muncul ketika
kita mengetik coding.
Gambar 2.1 Deklarasi Class1
Berikut source code Form1 :
Private Sub Command1_Click(Index As Integer)
BUKU PEMROGRAMAN API _ BAB 2 CLASS MODULE Page 27
If Not IsNumeric(Text1.Text) Then
MsgBox "Inputan harus angka!"
Else
If Index = 0 Then
Label4.Caption = hasil.lpersegi(Val(Text1.Text))
ElseIf Index = 1 Then
Label4.Caption = hasil.kpersegi(Val(Text1.Text))
End If
End If
End Sub
Private Sub Command2_Click(Index As Integer)
Text1.Text = ""
Label4.Caption = ""
End Sub
Form2 :
Buat Form2 untuk bangun datar persegi panjang dan simpan dengan
nama bdPanjang.frm. Di dalam Form2, buat object sebagai berikut :
a. Label1, Name = Label1; Caption = PERSEGI PANJANG
b. Label2, Name = Label2; Caption = PANJANG
c. Label3, Name = Label3; Caption = LEBAR
d. Label4, Name = Label4; Caption = HASIL:
e. Label5, Name = Label5; Caption = -
f. TextBox1, Name = Text1; Caption = -
g. TextBox2, Name = Text2; Caption = -
h. CommandButton1, Name = Command1; Caption = LUAS
i. CommandButton2, Name = Command1; Caption = KELILING
j. CommandButton3, Name = Command2; Caption = HAPUS
Deklarasi class module menjadi variabel bertipe object :
Dim hasil As New Class1
Berikut source code Form2 :
Private Sub Command1_Click(Index As Integer)
If Not IsNumeric(Text1.Text) Or Not IsNumeric(Text2.Text) Then
MsgBox "Inputan harus angka!"
Else
If Index = 0 Then
Label5.Caption = hasil.lpanjang(Val(Text1.Text),
Val(Text2.Text))
Else
Label5.Caption = hasil.kpanjang(Val(Text1.Text),
Val(Text2.Text))
End If
End If
End Sub
BUKU PEMROGRAMAN API _ BAB 2 CLASS MODULE Page 27
Private Sub Command2_Click(Index As Integer)
Text1.Text = ""
Text2.Text = ""
Label5.Caption = ""
End Sub
Form3 :
Buat Form3 untuk bangun datar segitiga dan simpan dengan nama
bdSegitiga.frm. Di dalam Form3, buat object sebagai berikut :
a. Label1, Name = Label1; Caption = SEGITIGA
b. Label2, Name = Label2; Caption = ALAS
c. Label3, Name = Label3; Caption = TINGGI
d. Label4, Name = Label4; Caption = HASIL:
e. Label5, Name = Label5; Caption = -
f. TextBox1, Name = Text1; Caption = -
g. TextBox2, Name = Text2; Caption = -
h. CommandButton1, Name = Command1; Caption = LUAS
i. CommandButton2, Name = Command2; Caption = HAPUS
Deklarasi class module menjadi variabel bertipe object :
Dim hasil As New Class1
Berikut source code Form3 :
Private Sub Command1_Click(Index As Integer)
If Not IsNumeric(Text1.Text) Or Not IsNumeric(Text2.Text) Then
MsgBox "Inputan harus angka!"
Else
If Index = 0 Then
Label5.Caption = hasil.lsegitiga(Val(Text1.Text),
Val(Text2.Text))
End If
End If
End Sub
Private Sub Command2_Click(Index As Integer)
Text1.Text = ""
Text2.Text = ""
Label5.Caption = ""
End Sub
Form4 :
Buat Form4 untuk bangun ruang Kubus dan simpan dengan nama
brKubus.frm. Di dalam Form4, buat object sebagai berikut :
a. Label1, Name = Label1; Caption = -
b. Label2, Name = Label2; Caption = SISI
c. Label3, Name = Label3; Caption = HASIL:
d. Label4, Name = Label4; Caption = KUBUS
BUKU PEMROGRAMAN API _ BAB 2 CLASS MODULE Page 27
e. TextBox1, Name = Text1; Caption = -
f. CommandButton1, Name = Command1; Caption = VOLUME
g. CommandButton2, Name = Command2; Caption = HAPUS
Deklarasi class module menjadi variabel bertipe object :
Dim hasil As New Class1
Berikut source code Form4 :
Private Sub Command1_Click(Index As Integer)
If Not IsNumeric(Text1.Text) Then
MsgBox "Inputan harus angka!"
Else
Label1.Caption = hasil.vkubus(Val(Text1.Text))
End If
End Sub
Private Sub Command2_Click(Index As Integer)
Text1.Text = ""
Label1.Caption = ""
End Sub
Form5 :
Buat Form5 untuk bangun ruang Balok dan simpan dengan nama
brBalok.frm. Di dalam Form5, buat object sebagai berikut :
a. Label1, Name = Label1; Caption = -
b. Label2, Name = Label2; Caption = PANJANG
c. Label3, Name = Label3; Caption = HASIL:
d. Label4, Name = Label4; Caption = BALOK
e. Label5, Name = Label5; Caption = LEBAR
f. Label6, Name = Label6; Caption = TINGGI
g. TextBox1, Name = Text1; Caption = -
h. TextBox2, Name = Text2; Caption = -
i. TextBox3, Name = Text3; Caption = -
j. CommandButton1, Name = Command1; Caption = VOLUME
k. CommandButton2, Name = Command2; Caption = HAPUS
Deklarasi class module menjadi variabel bertipe object :
Dim hasil As New Class1
Berikut source code Form5 :
Private Sub Command1_Click(Index As Integer)
If Not IsNumeric(Text1.Text) Or Not IsNumeric(Text2.Text) Or Not