contoh penggunaan class pada Visual Studio 2010
Contoh
penggunaan class pada visual studio 2010
Public Class kelinci
Inherits Binatang_Peliharaan
Private
warna As String
= "Putih"
Private
tempat_tinggal As String
ReadOnly Property warna_kelinci As
String
Get
Return
warna
End Get
End Property
Property
tempattinggal_kelinci As String
Set(ByVal value As String)
tempat_tinggal = value
End Set
Get
Return
tempat_tinggal
End Get
End Property
Sub New(ByVal tmpttgl As String)
tempat_tinggal = tmpttgl
End Sub
End Class
Public Class mobil
Public Enum Merk
expander= 0
toyota= 1
kijang = 2
suzuki = 3
End Enum
Private bentuk_garansi
As garansi =garansi.Baru
Private
jumlah_roda As Integer
= 4
ReadOnly Property Jenis_mobil
Get
Return
merk
End Get
End Property
ReadOnly Property roda As Integer
Get
Return
jumlah_ roda
End Get
End Property
Function _garansi(ByVal M As garansi) As String
If a = garansi.Baru Wortel Then
Return
"Garansi Max 5 Tahun"
Else
Return
"Tidak Bergaransi"
End If
End Function
End Class
Public Class Form1
Dim
kelinci1 As New
kelinci("Kandang")
Private Sub Form1_Load(ByVal
sender As System.Object,
ByVal e As
System.EventArgs) Handles
MyBase.Load
TextBox1.Text =
kelinci1.tempattinggal_kelinci
TextBox2.Text = kelinci1.warna_kelinci
TextBox3.Text = "Wortel"
Dim ds As New DataTable
ds.Columns.Add("v")
ds.Columns.Add("t")
ds.Rows.Add({0, "wortel"})
ds.Rows.Add({1, "Daun"})
ds.Rows.Add({2, "Serangga"})
ds.Rows.Add({3, "Daging"})
ComboBox1.DataSource = ds
ComboBox1.DisplayMember = "t"
ComboBox1.ValueMember = "v"
End Sub
Private Sub Button1_Click(ByVal
sender As System.Object,
ByVal e As
System.EventArgs) Handles
Button1.Click
End
End Sub
Private Sub Button2_Click(ByVal
sender As System.Object,
ByVal e As
System.EventArgs) Handles
Button2.Click
TextBox4.Text =
kelinci1.Beri_makan(ComboBox1.SelectedValue)
If
TextBox5.Text = "1" Then
TextBox6.Text = "Max Makanan=300gr"
Else
TextBox5.Text = "2"
TextBox6.Text = "Max Makanan=500gr"
End If
End Sub
Private Sub Button3_Click(ByVal
sender As System.Object,
ByVal e As
System.EventArgs) Handles
Button3.Click
TextBox4.Text = ""
TextBox5.Text = ""
TextBox6.Text = ""
End Sub
Private Sub TextBox5_TextChanged(ByVal
sender As System.Object,
ByVal e As
System.EventArgs) Handles
TextBox5.TextChanged
End Sub
End Class
Digunakan pada
class.vb
Public Class Kelinci
Private nama As String
Private usia As Integer
Private warna As String
Private jeda_makan As Integer
Property NamaKelinci As String
Set(ByVal value As String)
nama = value
End Set
Get
Return nama
End Get
End Property
Property UsiaKelinci As Integer
Set(ByVal value As Integer)
usia = value
End Set
Get
Return usia
End Get
End Property
Property WarnaKelinci As String
Set(ByVal value As String)
warna = value
End Set
Get
Return warna
End Get
End Property
Function Makan() As Integer
jeda_makan = 15
Return jeda_makan
End Function
Function Lapar() As Boolean
If jeda_makan > 0 Then
Return False
Else
Return True
End If
End Function
Function KurangiJeda() As Integer
If jeda_makan > 0 Then
jeda_makan -= 1
End If
Return jeda_makan
End Function
Sub New(ByVal nm As String, ByVal us As
Integer, ByVal wn As String)
nama = nm
usia = us
warna = wn
jeda_makan = 15
End Sub
End Class
digunakan pada form1
Public Class Form1
Dim
kelinci1 As Kelinci
Private Sub Form1_Load(ByVal
sender As System.Object,
ByVal e As
System.EventArgs) Handles
MyBase.Load
kelinci1 = New
Kelinci("lorry",
3, "putih")
Call
Informasi()
End Sub
Sub
Informasi()
TextBox1.Text = kelinci1.NamaKelinci
TextBox2.Text = kelinci1.UsiaKelinci
TextBox3.Text = kelinci1.WarnaKelinci
End Sub
Function
Cek_Lapar() As String
If kelinci1.Lapar = True
Then
Return
"Lapar"
Else
Return
"Kenyang"
End If
End Function
Private Sub Button2_Click(ByVal
sender As System.Object,
ByVal e As
System.EventArgs) Handles
Button2.Click
End
End Sub
Private Sub Timer1_Tick(ByVal
sender As System.Object,
ByVal e As
System.EventArgs) Handles
Timer1.Tick
TextBox4.Text = Cek_Lapar()
kelinci1.KurangiJeda()
End Sub
Private Sub Button1_Click(ByVal
sender As System.Object,
ByVal e As
System.EventArgs) Handles
Button1.Click
kelinci1.Makan()
End Sub
End Class
Komentar
Posting Komentar