Member Class di VB.NET

Class : adalah blue print dari sebuah object

Sebuah Class mempunyai Anggota Class ( Member Class)

  1. Fields : Merepresentasikan sebuah data
  2. Property : Menyediakan akses terhadap Field / elemen dari class
  3. Method ( Procedure dan Function ) : Aksi yang dapat dilakukan oleh class
  4. Events : Aksi yang dapat dihandle / direspond dalam kode program yang menggunakannya

Berikut contoh Class Person


Public Class Person

'Member class Field
Private intId As Integer
Private strNama As String
Private strTelpon As String

'Member Class Events
Public Event berkata(ByVal nama As String, ByVal pesan As String)

' Konstruktor 1
Public Sub New()

End Sub

'Konstruktor 2
Public Sub New(ByVal id As Integer, ByVal strNama As String)
intId = id
Me.strNama = strNama  ' kata kunci me digunakan akses field member class
 End Sub

'Member Class Property : ID
Public Property ID()
Get
Return intId
End Get
Set(ByVal value)
intId = value
End Set
End Property

 'Member Class Property : Nama
 Public Property Nama()
 Get
 Return strNama
 End Get
 Set(ByVal value)
 strNama = value
 End Set
 End Property

 'Member Class Property : Telpon
 Public Property Telpon()
 Get
 Return strTelpon
 End Get
 Set(ByVal value)
 strTelpon = value
 End Set
 End Property

 'Member class Method ( Procedure ) : void
 Public Sub salamTo(ByVal str As String)
 MsgBox("Assalamaulaikum " + str, MsgBoxStyle.Information)
 End Sub

 'Member class Method ( Function ) : return value
 Public Function naikanGaji(ByVal gaji As Double, ByVal persen As Double) As Double
 Return gaji * (1.0 + persen)
 End Function

End Class

Berikut Class Form1 yang akan menggunakan Class Person


Public Class Form1
 WithEvents person As Person

Private Sub person_berkata(ByVal nama As String, ByVal pesan As String) Handles person.berkata
MsgBox(pesan, MsgBoxStyle.Exclamation, nama & " berkata ")
 End Sub

'Pada event Form Load

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
 Dim orang1 As New person(1, "Sirojul Munir")
 orang1.Telpon = "021-5553144"
 orang1.salamTo("Faiz")

 Dim gaji_pokok, gaji_baru As Double
 gaji_pokok = 5000000
 gaji_baru = orang1.naikanGaji(gaji_pokok, 0.1)
 MsgBox(gaji_baru)

 Dim orang2 As New person()
 orang2.ID = 10
 orang2.Nama = "Lionel Messi"
 orang2.Telpon = "41232232"
 orang2.salamTo(orang1.Nama)
 person_berkata(orang2.Nama, "saya akan bawa argentina juara PD 2014")

End Sub

End Class

Post a Response