Menggunakan Objek Kontrol Gambar Dan Pewaktu

download Menggunakan Objek Kontrol Gambar Dan Pewaktu

of 28

Transcript of Menggunakan Objek Kontrol Gambar Dan Pewaktu

  • 8/12/2019 Menggunakan Objek Kontrol Gambar Dan Pewaktu

    1/28

    MENGGUNAKAN OBJEK KONTROL GAMBAR DAN PEWAKTUCakupan Materi : Properties, Event, dan Method

    Untuk Menampilkan isi file Gambar, kontrol yang dapat digunakan ada 2 (dua) yaitu Image

    dan Picture Box. Ekstensi dari file gambar antara lain : BMP, JPG, GIF, PNG, dsbnya.

    Untuk pemrograman yang memerlukan pemanfaatan data waktu biasanya menggunakankontrol Timer.

  • 8/12/2019 Menggunakan Objek Kontrol Gambar Dan Pewaktu

    2/28

    OOP pada VB.Net

    1.1. Pengertian Object Oriented Programming (OOP)

    Object Oriented Programming merupakan sebuah sudut pandang pemecahan masalah.

    Masalah yang dimaksud di sini merupakan masalah yang berhubungan dengan computer

    yang dipandang sebagai kumpulan objek yang saling berhubungan.

    1.2. Tujuan OOP

    Tujuan utama dari pengembangan pendekatan berorientasi objek adalah untuk

    menghilangkan kelemahan-kelemahan yang terdapat pada pendekatan prosedural. Pada

    OOP, data dibutuhkan sebagai elemen yang penting dan tidak boleh mengalir secara bebas

    dalam sebuah program. Data yang terikat pada function dan harus dilindungi terhadap

    kemungkinan perubahan dari luar function.

    Catatan :

    Anda dapat melihat konsep OOP seperti suatu objek telebisi, dimana sebagai pemakai kita

    tidak perlu terlalu pusing dengan komponen apa saja yang bekerja didalam televisetersebut, tetapi sebagi pemakai kita cukup mengerti bagaimana menghidupkan, memilih

    channel, mengubah volume suara, dan beberapa setting dasar.

    OOP memungkinkan untuk pemecahan masalah kedalam sejumlah entitas yang disebut

    sebagai objek dan kemudian mengembangkan data dan fungsi disekitar objek tersebut.

    Salah satu manfaat yang terpenting dari OOP adalah pemanfaatan kembali.

    Selain kemampuan yang sudah dijabarkan di atas, beberapa kemampuan utama dari OOP

    yang lain ialah sebagai berikut:

    1. Lebih menekankan pada data daripada prosedur.

    2. Program terbagi atas objek-objek.

    3. Data disembunyikan dari akses oleh fungsi-fungsi eksternal.4. Melalui function objek dapat berkomunikasi satu dengan yang lain.

    5. Mengikuti pendekatan buttom up.

    1.3. Konsep Dasar OOP

    Konsep dasar suatu program termasuk dalam OOP, apabila terdapat pembungkusan

    (Encapsulation), Polymorphisme, turunan (Inheritance) dan abstraction.

    1.3.1. Encapsulation (Pembungkusan)

    Encapsulation (Pembungkusan) yaitu mengemas suatu objek sehingga pada saat kita akan

    memakai objek tersebut kita tidak perlu lagi tahu tentang detail bagaimana suatu action ituterjadi.

    Contoh dalam hal ini, misal siswa (suatu object) sedang menulis (method). Maka detail dari

    tentang cara siswa itu menulis (bagaimana otot tangan itu bekerja untuk menulis,

    bagaimana sistem saraf pusat memerintahkan untuk menulis) kita tidak perlu mengerti.

    Kita hanya melihat si siswa sedang menulis.

  • 8/12/2019 Menggunakan Objek Kontrol Gambar Dan Pewaktu

    3/28

    Contoh :

    class Siswa {

    String nama;

    String nim;

    void isiData ( String namaku,String nimku ) {

    nama = namaku;nim = nimku;

    }

    String ambilNama() {

    return nama;

    }

    String ambilNim() {

    return nim;

    }

    }

    public class Mahasiswa {

    public static void main ( String [] args)

    {

    Siswa mahasiswa_itn = new Siswa();

    mahasiswa_itn.isiData(Dina Damayanti,0412585);System.out.println( Nama : +mahasiswa_itn.ambilNama());System.out.println( Nim : + mahasiswa_itn.ambilNim());}

    }

    1.3.2. Polymorphisme

    Polimorphisme merupakan suatu object bisa bertindak lain terhadap message/method

    yang sama. Misalkan kita punya satu class hewan dan diturunkan menjadi dua class hewan

    berkaki dua dan hewan berkaki empat. Ketika hewan berkaki dua melakukan method

    makan maka yang dilakukan adalah makan dengan paruhnya, dan ketika hewan berkaki

    empat juga makan maka dia akan menggunakan mulutnya. Keduanya melakukan aksi yang

    sama yaitu makan akan tetapi perlakuannya lain.

    Contoh :

    class Binatang{public void info(){

    System.out.println( Info tentang Hewan : );}

    }

  • 8/12/2019 Menggunakan Objek Kontrol Gambar Dan Pewaktu

    4/28

    class Herbivora extends Binatang {

    public void info(){

    System.out.println (Info pada herbivora: Memakan makanan berupa tumbuh tumbuhan);}

    }

    class Kelinci extends Herbivora {

    public void info(){

    System.out.println(Info pada Kelinci: Memakan makanan berupa wortel);}

    }

    public class Polimorfisme {

    public static void main(String[] args) {

    Herbivora herbivora;

    Kelinci kelinciku;Binatang hewan;

    herbivora=new Herbivora();

    kelinciku=new Kelinci();

    hewan=herbivora;

    hewan.info();

    hewan=kelinciku;

    hewan.info();

    }

    }

    1.3.3. Inheritance (Turunan)Turunan merupakan yaitu salah satu bentuk teknis untuk membuat sebuah class lebih

    spesifik lagi. Sebagai contoh dari kasus ini menurut analogi di atas. Misalkan kita akan

    membuat class yang lebih spesifik dari ORANG yaitu siswa/student. Maka class ini

    mempunyai properti tambahan sekolah tempat dia belajar. Akan tetapi, semua properti

    sebagai orang ada juga pada class siswa ini.

    Contoh:

    Public Class ORANG_TUA

    'variabel untuk menampung nama bapak

    Private XBapak As String'variabel untuk menampung nama ibu

    Private XIbu As String

    'property (atribut/informasi) class

    Public Property Nama_Bapak() As String

    'Get : hanya ijinkan akses data

    Get Return XBapak

    End Get

  • 8/12/2019 Menggunakan Objek Kontrol Gambar Dan Pewaktu

    5/28

  • 8/12/2019 Menggunakan Objek Kontrol Gambar Dan Pewaktu

    6/28

    1.4. Keunggulan OOP

    Object Oriented Programming memiliki beberapa keunggulan di atas pendekatan

    konvensioanal, yaitu sebagai berikut:

    1. OOP menyediakan struktur modular yang jelas untuk program, yang mana cocok

    untuk mendefinisikan tipe data abstrak yang merupakan detail dari implementasi.2. OOP memudahkan perawatan atau maintenance dan modifikasi terhadap kode yang

    telah sebagai objek baru yang dapat dibuat dengan perubahan yang minimal.

    3. OOP juga menyediakan suatu kerangka kerja yang baik untuk kode library yang mana

    menyediakan komponen software yang secara mudah dapat disesuaikan dan dimodifikasi

    oleh programmer. Hal ini khususnya sangat berguna untuk pengembangan user interface

    secara grafik.

    1.5. OOP pada Visual Basic

    1.5.1. Membuat Class

    Pemrograman OOP pada Visual Basic dapat dilakukan dengan merancang class dari objekdengan menggunakan struktur class sebagai berikut:

    Class nama

    'member [variable, property, method and event declarations]

    End Class

    Contoh :

    Misalnya kita ingin membuat program animasi bola pantul yang memanfaatkan GDI+

    dengan pendekatan berorientasi objek, maka sebagai langkah awal adalah merancang Class

    untuk bola pantul.

    Pada tahap rancangan, kita harus dapat mendefinisikan aksi apa saja yang harus dapat

    dilakukan oleh objek bola pantul yaitu bergerak (move) yang tentu saja memiliki koordinat

    awal (x,y) yang bergerak kearah tertentu (dx, dy). Dalam pergerakan dilayar, tentu sajadibatasi oleh area tertentu (sx,sy).

    Kemudian sebagai wujud dari animasi gerakan bola dilayar, kita perlu menyediakan aksi

    draw dan clear.

    Adapun rancangan class tersebut diatas dalam bentuk terprogram adalah sebagai berikut:

    Class Pantul

    Protected x As Single 'posisi x dilayar

    Protected y As Single 'posisi y dilayar

    Private sx As Single 'jumlah kolom x

    Private sy As Single 'jumlah baris yPrivate dx As Single 'arah bola terhadap x

    Private dy As Single 'arah bola terhadap y

    Public Sub New(f as form) 'constructor

    sx = f.width \ 30 'lebar form dibagi 30

    sy = f.height \ 30 'tinggi form dibagi 30

    x = rnd*(sx-2) + 2 'posisi awal random

  • 8/12/2019 Menggunakan Objek Kontrol Gambar Dan Pewaktu

    7/28

    y = rnd*(sy-2) + 2

    dx = 1 'arah diagonal ke bawah

    dy = 1

    Draw(f)

    End Sub

    Public Sub Draw(f as form) 'metoda

    'gambar bola ukuran 30x30 dengan warna merah

    Dim MyGraphic As Sistem.Drawing.Graphics = f.CreateGraphics

    Dim MyPen As New

    Sistem.Drawing.Pen(Sistem.Drawing.Color.Red,3)

    MyGraphic.DrawEllipse(MyPen,x*30,y*30,30,30)

    End Sub

    Public Sub Clear(f as form) 'metoda

    'gambar bola ukuran 30x30 dengan warna latar belakang formDim MyGraphic As Sistem.Drawing.Graphics = f.CreateGraphics

    Dim MyPen As New Sistem.Drawing.Pen(f.BackColor,3)

    MyGraphic.DrawEllipse(MyPen, x*30,y*30,30,30)

    End Sub

    Public Sub Move(f as form) 'metoda

    'hapus gambar bola sebelumnya

    Clear(f)

    'gerakan bola dengan mengubah x, y sesuai dengan arah

    x = x + dx

    y = y + dy'jika mengenai daerah bingkai

    If x < 2 or x > (sx-2) Then dx = -dx

    If y < 2 or y > (sy-2) Then dy = -dy

    'gambar bola diposisi baru

    Draw(f)

    End Sub

    End Class

    Catatan : Jika anda perhatikan perhatikan rancangan class diatas, maka konsep dari data

    abstraction akan nampak, dimana sebagai interface keluar hanya terdiri metoda New,Draw, Clear, dan Move, sedangkan pengkodean tersembunyi dari program yang akan

    memanfaatkan Objek dari Class Pantul.

    Kemudian konsep dari data encapsulation, dimana variable x,y,sx,sy,dx, dan dy disatukan

    beserta fungsi New, Draw, Clear, dan Move didalam satu Class Pantul.

  • 8/12/2019 Menggunakan Objek Kontrol Gambar Dan Pewaktu

    8/28

    1.5.1.1. Ruang Lingkup member pada suatu class

    Pada saat pendeklarasian member pada class anda dapat menggunakan keyword seperti

    Friend, Private, Protected, Protected Friend, dan Public, dimana masing- masing memiliki

    kendali akses yang berbeda.

    Friend, member hanya tersedia bagi kode pada lingkup project yang sama.Private,

    membehanya tersedia bagi class itu sendiri Protected, member tersedia bagi class dan class

    turunannya.

    Protected Friend, member tersedua bagi kode pada lingkup project yang sama, dan class

    turunannya. Public, member tersedia bagi semua kode program. Jika anda tidak

    menentukan jenis kendali akses bagai suatu member, maka defaultnya adalah Public.

    1.5.2. Constructors

    Constructor adalah anggota function yang mana memiliki tugas untuk menginisialisasi

    objek dari classnya. Suatu constructor akan dijalankan ketika objek dari class tersebut

    diciptakan. Kita dapat mengirim data kepada konstruktor dengan mencantumkannya

    didalam kurung. Pada Visual Basic suatu konstruktor harus dibuat dengan suatu Sub

    procedure dengan nama New pada class. Koding berikut mendemonstrasikan pemakaian

    dari konstruktor di Visual Basic.

    Public Sub New(f as form) 'constructor

    sx = f.width \ 30 'lebar form dibagi 30

    sy = f.height \ 30 'tinggi form dibagi 30

    x = rnd*(sx-2) + 2 'posisi awal random

    y = rnd*(sy-2) + 2

    dx = 1 'arah diagonal ke bawah

    dy = 1

    Draw(f)

    End Sub

    Pada contoh diatas, konstruktor digunakan untuk menginisialisasi nilai awal untuk variable

    pada Objek seperti sx, sy, x, y, dx, dan dy.

    Untuk konstruktor diatas, pada saat kita membuat instance dari Class dengan

    Dim Bola As Pantul

    Bola = New Pantul(Me)

    Dimana Me mengacu pada form yang sedang aktif.

    1.5.3. Destructors

  • 8/12/2019 Menggunakan Objek Kontrol Gambar Dan Pewaktu

    9/28

    Destructors akan dijalankan ketika suatu objek dimusnahkan. Didalam suatu destructor

    kita dapat menempatkan koding untuk membersihkan objek setelah digunakan. Kita

    menggunakan metode Finalize di Visual Basic untuk membuat destructor yang otomatis

    akan dijalankan ketika runtime. NET menemukan bahwa objek tidak dibutuhkan lagi.

    Contoh implementasi pemakaian destructor adalah misalnya pada konstruktor kita

    membuka data maupun file, sehingga pada saat destructor kita perlu menutup data

    maupun file yang terbuka.

    Catatan: Setiap objek didunia nyata memiliki fase awal yaitu kelahiran, dan fase akhir yaitu

    kematian. Pada saat suatu objek dilahirkan tentu saja telah memiliki nilai property

    bawaan,seperti seorang bayi yang baru dilahirkan telah memiliki jenis kelamin, berat

    badan, dan tinggi.

    Demikian juga objek dalam konsep OOP, yang kadang-kadang membutuhkan pemberian

    nilai awal kepada beberapa property begitu instance dari class dibuat. Tindakan pemberiannilai awal (inisialisasi) ini dapat dilakukan dengan menggunakan metoda khusus yang

    dikenal sebagai

    Constructor.

    1.5.4. Overriding

    Pada derived class kadang-kadang kita perlu mendefinisikan ulang fungsi maupun sub

    yang ada pada base class, dalam hal inilah istilash Overriding muncul, dimana kita dapat

    mendefinisikan ulang fungsi maupun sub pada derived class.

    Misalnya pada Class Pantul1 gambar yang ingin kita hasilkan adalah persegi, maka kita

    perlu melakukan Overiding terhadap Sub Draw dan Clear yang ada pada base class.

    Catatan : Sub maupun Function pada base class harus dideklarasikan sebagai Overridable

    baru dapat di Overrides pada derived class.

    Pada Class pantul kita perlu mencantumkan keyword Overridable

    Overridable Public Sub Draw(f as form) 'metoda

    'gambar bola ukuran 30x30 dengan warna merah

    Dim MyGraphic As Sistem.Drawing.Graphics = f.CreateGraphics

    Dim MyPen As New Sistem.Drawing.Pen(Sistem.Drawing.Color.Red,3)

    MyGraphic.DrawEllipse(MyPen,x*30,y*30,30,30)

    End Sub

    Overridable Public Sub Clear(f as form) 'metoda

    'gambar bola ukuran 30x30 dengan warna latar belakang form

    Dim MyGraphic As Sistem.Drawing.Graphics = f.CreateGraphics

    Dim MyPen As New Sistem.Drawing.Pen(f.BackColor,3)

  • 8/12/2019 Menggunakan Objek Kontrol Gambar Dan Pewaktu

    10/28

  • 8/12/2019 Menggunakan Objek Kontrol Gambar Dan Pewaktu

    11/28

    Pada class turunannya :

    Public Shadows Sub Draw(f as form) 'metoda

    'gambar persegi ukuran 30x30 dengan warna merah

    Dim MyGraphic As Sistem.Drawing.Graphics = f.CreateGraphicsDim MyPen As New Sistem.Drawing.Pen(Sistem.Drawing.Color.Red,3)

    MyGraphic.DrawRectangle(MyPen,x*30,y*30,30,30)

    End Sub

    Catatan :

    Pemakaian cara Shadowing menyebabkan konsep polymorphism tidak dapat berfungsi

    sebagaimana pada cara Overriding.

    1.6. Contoh Keunggulan OOP (Bagian 1)

    Jika sekilas kita melihat uraian yang telah disebutkan diatas seakan-akan segala sesuatu

    yang dilakukan dengan pendekatan OOP dapat juga dilakukan dengan teknik pemrograman

    biasanya. Tetapi misalnya anda bayangkan kita ingin membuat program bola pantul yang

    terdiri dari 15 buah bola pantul dilayar yang masing- masing bergerak tanpa pendekatan

    OOP.

    Berikut ini saya akan membuat contoh dengan pendekatan OOP, dan anda bandingkan

    sendiri.

    Pertama kita akan membuat variable array untuk bola pantul sebagai array 15 elemen :

    Dim Bola(14) As PantulKemudian kita buat instance dari Class Pantul

    For I As Integer = 0 To 14

    Bola(I) = New Pantul(Me)

    Next I

    Selanjutnya kita gerakan masing- masing objek dengan looping :

    For I As Integer = 0 To 14

    Bola.Move(Me)

    Next I

    Dari hal ini jelaslah bahwa dengan pendekatan OOP kita tidak melakukan perubahan

    terhadap

    rancangan Class Pantul sama sekali, melainkan kita membuat 15 instance dari Class Pantul.

    Listing lengkap dari program 15 ola pantul dengan pendekatan OOP.

  • 8/12/2019 Menggunakan Objek Kontrol Gambar Dan Pewaktu

    12/28

    Option Explicit On

    Imports Sistem

    Imports Sistem.Windows.Forms

    Imports Microsoft.VisualBasic

    Public Class MyForm : Inherits Form

    Dim WithEvents MyTimer as New Timer

    Dim Bola(14) As Pantul

    _

    Public Shared Sub Main()

    Application.Run(New MyForm)

    End Sub

    Public Sub New() 'Constructor

    Me.BackColor = Sistem.Drawing.Color.BlackMe.FormBorderStyle = FormBorderStyle.None

    Me.Width = Screen.PrimaryScreen.Bounds.Width

    Me.Height = Screen.PrimaryScreen.Bounds.Height

    For I As Integer = 0 to 14

    Bola(I) = New Pantul(Me)

    Next I

    MyTimer.Interval = 150 '150 millidetik

    MyTimer.Enabled = True

    End Sub'Mengerakan objek dengan event tick pada objek MyTimer

    Private Sub MyTimer_Tick(sender as object, e as eventargs) _

    handles MyTimer.Tick

    For I As Integer = 0 to 14

    Bola(I).Move(Me)

    Next I

    End Sub

    End Class

    Class Pantul

    Protected x As Single 'posisi x dilayar

    Protected y As Single 'posisi y dilayar

    Private sx As Single 'jumlah kolom x

    Private sy As Single 'jumlah baris y

  • 8/12/2019 Menggunakan Objek Kontrol Gambar Dan Pewaktu

    13/28

    Private dx As Single 'arah bola terhadap x

    Private dy As Single 'arah bola terhadap y

    Public Sub New(f as form) 'constructor

    sx = f.width \ 30 'lebar form dibagi 30

    sy = f.height \ 30 'tinggi form dibagi 30x = rnd*(sx-2) + 2 'posisi awal random

    y = rnd*(sy-2) + 2

    dx = 1 'arah diagonal ke bawah

    dy = 1

    Draw(f)

    End Sub

    Overridable Public Sub Draw(f as form) 'metoda

    'gambar bola ukuran 30x30 dengan warna merah

    Dim MyGraphic As Sistem.Drawing.Graphics = f.CreateGraphics

    Dim MyPen As NewSistem.Drawing.Pen(Sistem.Drawing.Color.Red,3)

    MyGraphic.DrawEllipse(MyPen,x*30,y*30,30,30)

    End Sub

    Overridable Public Sub Clear(f as form) 'metoda

    'gambar bola ukuran 30x30 dengan warna latar belakang form

    Dim MyGraphic As Sistem.Drawing.Graphics = f.CreateGraphics

    Dim MyPen As New Sistem.Drawing.Pen(f.BackColor,3)

    MyGraphic.DrawEllipse(MyPen, x*30,y*30,30,30)

    End Sub

    Public Sub Move(f as form) 'metoda

    'hapus gambar bola sebelumnya

    Clear(f)

    'gerakan bola dengan mengubah x, y sesuai dengan arah

    x = x + dx

    y = y + dy

    'jika mengenai daerah bingkai

    If x < 2 or x > (sx-2) Then dx = -dx

    If y < 2 or y > (sy-2) Then dy = -dy

    'gambar bola diposisi baru

    Draw(f)

    End Sub

  • 8/12/2019 Menggunakan Objek Kontrol Gambar Dan Pewaktu

    14/28

    End Class

    Contents Introduction Using the code Lesson 1: Namespaces, Classes & Modules Lesson 2: Access Types Lesson 3: Shared Functions Lesson 4: Overloading Lesson 5: Inheritance Lesson 6: Overriding Lesson 7: Polymorphism

    Lesson 8: Constructors & Destructors Lesson 9: Property Routines Lesson 10: A Simple Application

    Introduction

    VB.NET is completely object oriented. This article uncovers some basic Object Oriented Programming

    features of Visual Basic. NET. The whole article is divided into ten lessons. The source code for these

    lessons is provided with the article.

    This tutorial is designed with the following objectives:

    1. To provide a sound knowledge about Object Oriented Programming in VB.NET.2. To educate how Object Oriented techniques are used in VB.NET.3. To explain the following concepts in an easy and simple way:o Creating and using classes and objects in VB.NET.o Encapsulation, Abstraction, Inheritance and Polymorphism.o Overloading and Overriding.o Constructors and Destructors.o Static functions.

    Go through this tutorial and you will start making sense of almost any .NET code. Also, Java/CPP

    programmers can use this to understand OOPs in VB.NET.

    Using the code

    The source code for each lesson is available as a .vbsource code file. You need Microsoft .NET

    framework SDK installed in your system to compile and execute the exercises in this article. You can

    http://www.codeproject.com/Articles/8825/Object-Oriented-Programming-In-VB-NET#Introductionhttp://www.codeproject.com/Articles/8825/Object-Oriented-Programming-In-VB-NET#Introductionhttp://www.codeproject.com/Articles/8825/Object-Oriented-Programming-In-VB-NET#Using%20the%20codehttp://www.codeproject.com/Articles/8825/Object-Oriented-Programming-In-VB-NET#Using%20the%20codehttp://www.codeproject.com/Articles/8825/Object-Oriented-Programming-In-VB-NET#LESSON%201:%20NAMESPACES,%20CLASSES%20AND%20OBJECTS,%20MODULEShttp://www.codeproject.com/Articles/8825/Object-Oriented-Programming-In-VB-NET#LESSON%201:%20NAMESPACES,%20CLASSES%20AND%20OBJECTS,%20MODULEShttp://www.codeproject.com/Articles/8825/Object-Oriented-Programming-In-VB-NET#LESSON%202:%20ACCESS%20TYPEShttp://www.codeproject.com/Articles/8825/Object-Oriented-Programming-In-VB-NET#LESSON%202:%20ACCESS%20TYPEShttp://www.codeproject.com/Articles/8825/Object-Oriented-Programming-In-VB-NET#LESSON%203:%20SHARED%20FUNCTIONShttp://www.codeproject.com/Articles/8825/Object-Oriented-Programming-In-VB-NET#LESSON%203:%20SHARED%20FUNCTIONShttp://www.codeproject.com/Articles/8825/Object-Oriented-Programming-In-VB-NET#LESSON%204:%20OVERLOADINGhttp://www.codeproject.com/Articles/8825/Object-Oriented-Programming-In-VB-NET#LESSON%204:%20OVERLOADINGhttp://www.codeproject.com/Articles/8825/Object-Oriented-Programming-In-VB-NET#LESSON%205:%20INHERITANCEhttp://www.codeproject.com/Articles/8825/Object-Oriented-Programming-In-VB-NET#LESSON%205:%20INHERITANCEhttp://www.codeproject.com/Articles/8825/Object-Oriented-Programming-In-VB-NET#LESSON%206:%20OVERRIDINGhttp://www.codeproject.com/Articles/8825/Object-Oriented-Programming-In-VB-NET#LESSON%206:%20OVERRIDINGhttp://www.codeproject.com/Articles/8825/Object-Oriented-Programming-In-VB-NET#LESSON%207:%20POLYMORPHISMhttp://www.codeproject.com/Articles/8825/Object-Oriented-Programming-In-VB-NET#LESSON%207:%20POLYMORPHISMhttp://www.codeproject.com/Articles/8825/Object-Oriented-Programming-In-VB-NET#LESSON%208:%20CONSTRUCTORS%20&%20DESTRUCTORShttp://www.codeproject.com/Articles/8825/Object-Oriented-Programming-In-VB-NET#LESSON%208:%20CONSTRUCTORS%20&%20DESTRUCTORShttp://www.codeproject.com/Articles/8825/Object-Oriented-Programming-In-VB-NET#LESSON%209:%20PROPERTY%20ROUTINEShttp://www.codeproject.com/Articles/8825/Object-Oriented-Programming-In-VB-NET#LESSON%209:%20PROPERTY%20ROUTINEShttp://www.codeproject.com/Articles/8825/Object-Oriented-Programming-In-VB-NET#LESSON%2010:%20A%20SIMPLE%20PROGRAMhttp://www.codeproject.com/Articles/8825/Object-Oriented-Programming-In-VB-NET#LESSON%2010:%20A%20SIMPLE%20PROGRAMhttp://www.codeproject.com/Articles/8825/Object-Oriented-Programming-In-VB-NET#LESSON%2010:%20A%20SIMPLE%20PROGRAMhttp://www.codeproject.com/Articles/8825/Object-Oriented-Programming-In-VB-NET#LESSON%209:%20PROPERTY%20ROUTINEShttp://www.codeproject.com/Articles/8825/Object-Oriented-Programming-In-VB-NET#LESSON%208:%20CONSTRUCTORS%20&%20DESTRUCTORShttp://www.codeproject.com/Articles/8825/Object-Oriented-Programming-In-VB-NET#LESSON%207:%20POLYMORPHISMhttp://www.codeproject.com/Articles/8825/Object-Oriented-Programming-In-VB-NET#LESSON%206:%20OVERRIDINGhttp://www.codeproject.com/Articles/8825/Object-Oriented-Programming-In-VB-NET#LESSON%205:%20INHERITANCEhttp://www.codeproject.com/Articles/8825/Object-Oriented-Programming-In-VB-NET#LESSON%204:%20OVERLOADINGhttp://www.codeproject.com/Articles/8825/Object-Oriented-Programming-In-VB-NET#LESSON%203:%20SHARED%20FUNCTIONShttp://www.codeproject.com/Articles/8825/Object-Oriented-Programming-In-VB-NET#LESSON%202:%20ACCESS%20TYPEShttp://www.codeproject.com/Articles/8825/Object-Oriented-Programming-In-VB-NET#LESSON%201:%20NAMESPACES,%20CLASSES%20AND%20OBJECTS,%20MODULEShttp://www.codeproject.com/Articles/8825/Object-Oriented-Programming-In-VB-NET#Using%20the%20codehttp://www.codeproject.com/Articles/8825/Object-Oriented-Programming-In-VB-NET#Introduction
  • 8/12/2019 Menggunakan Objek Kontrol Gambar Dan Pewaktu

    15/28

    download it from the Microsoft website. The VB.NET compiler (vbc.exe) normally resides in

    your FrameworkSDK\binfolder.

    To manually compile a source code file, you may use the command prompt to type: vbc filename.vb

    /out:"filename.exe" /r:"System.Windows.Forms.dll","System.dll"

    Lesson 1: Namespaces, Classes & Objects,

    Modules

    A NamespaceIn VB.NET, classes and other data structures for a specific purpose are grouped together to form a

    namespace. You can use the classes in a namespace, by simply importing the namespace.

    The Importskeyword is used to import a namespace to your project. .NET framework provides a

    rich set of built in classes, grouped together to various namespaces. In this lesson, we are using

    the Systemnamespace. Import the Systemnamespace (already available in .NET).

    Collapse |Copy Code

    ImportsSystem

    A ClassProbably, you are already familiar with classes and objects. Simply speaking, a Classis a definition

    of a real life object. For example, Humanis a class for representing all human beings. Dogis a class to

    represent all Dogs. Classes can contain functions too. Animalsis a namespace.

    Collapse |Copy Code

    NamespaceAnimals

    Dogis a class in the namespace Animals:

    Collapse |Copy Code

    ClassDog

    Barkis a function in this Class:

    Collapse |Copy Code

    FunctionBark()Console.Writeline ("Dog is barking")

    EndFunction

    EndClass

    EndNamespace

    http://www.codeproject.com/Articles/8825/Object-Oriented-Programming-In-VB-NEThttp://www.codeproject.com/Articles/8825/Object-Oriented-Programming-In-VB-NEThttp://www.codeproject.com/Articles/8825/Object-Oriented-Programming-In-VB-NEThttp://www.codeproject.com/Articles/8825/Object-Oriented-Programming-In-VB-NEThttp://www.codeproject.com/Articles/8825/Object-Oriented-Programming-In-VB-NEThttp://www.codeproject.com/Articles/8825/Object-Oriented-Programming-In-VB-NEThttp://www.codeproject.com/Articles/8825/Object-Oriented-Programming-In-VB-NEThttp://www.codeproject.com/Articles/8825/Object-Oriented-Programming-In-VB-NEThttp://www.codeproject.com/Articles/8825/Object-Oriented-Programming-In-VB-NEThttp://www.codeproject.com/Articles/8825/Object-Oriented-Programming-In-VB-NEThttp://www.codeproject.com/Articles/8825/Object-Oriented-Programming-In-VB-NEThttp://www.codeproject.com/Articles/8825/Object-Oriented-Programming-In-VB-NEThttp://www.codeproject.com/Articles/8825/Object-Oriented-Programming-In-VB-NEThttp://www.codeproject.com/Articles/8825/Object-Oriented-Programming-In-VB-NEThttp://www.codeproject.com/Articles/8825/Object-Oriented-Programming-In-VB-NEThttp://www.codeproject.com/Articles/8825/Object-Oriented-Programming-In-VB-NET
  • 8/12/2019 Menggunakan Objek Kontrol Gambar Dan Pewaktu

    16/28

    An ObjectAn object is an instance of a Class. For example, Jimmyis an object of type Dog. We will create an

    object in the next section. Read on.

    ModulesYou can use modules to write common functions. A Moduleis a group of functions. Unlike functions

    in classes,Publicfunctions in modules can be called directly from anywhere else. VB

    provides Functions and Subroutines. Functions and Subroutines are almost the same, but the

    difference is that a subroutine can't return a value.

    Collapse |Copy Code

    PublicModulemodMain

    Execution will start from the Main()subroutine:

    Collapse |Copy Code

    SubMain()

    'Call our function. See belowOurFunction()

    Endsub

    OurFunction: Our own little function to use the class Dog:

    Collapse |Copy Code

    FunctionOurFunction()'Here is how we declare a variable Jimmy of type Dog.

    'We use Animals.Dog because, the class Dog is in the'namespace Animals (see above).

    DimJimmy asAnimals.Dog

    'Create an object. Unlike in VB 6, it is not required to use

    'the 'set' keyword.

    Jimmy = newAnimals.Dog()

    'Another way to create an object is

    'Dim Jimmy as new Dog

    'Call Jimmy's Main FunctionJimmy.Bark()

    EndFunction

    Endmodule

    Lesson 2: Access Types

    http://www.codeproject.com/Articles/8825/Object-Oriented-Programming-In-VB-NEThttp://www.codeproject.com/Articles/8825/Object-Oriented-Programming-In-VB-NEThttp://www.codeproject.com/Articles/8825/Object-Oriented-Programming-In-VB-NEThttp://www.codeproject.com/Articles/8825/Object-Oriented-Programming-In-VB-NEThttp://www.codeproject.com/Articles/8825/Object-Oriented-Programming-In-VB-NEThttp://www.codeproject.com/Articles/8825/Object-Oriented-Programming-In-VB-NEThttp://www.codeproject.com/Articles/8825/Object-Oriented-Programming-In-VB-NEThttp://www.codeproject.com/Articles/8825/Object-Oriented-Programming-In-VB-NEThttp://www.codeproject.com/Articles/8825/Object-Oriented-Programming-In-VB-NEThttp://www.codeproject.com/Articles/8825/Object-Oriented-Programming-In-VB-NEThttp://www.codeproject.com/Articles/8825/Object-Oriented-Programming-In-VB-NEThttp://www.codeproject.com/Articles/8825/Object-Oriented-Programming-In-VB-NET
  • 8/12/2019 Menggunakan Objek Kontrol Gambar Dan Pewaktu

    17/28

    The major access types are Public, Private, Friendand Protected. A Classmay contain

    functions, variables etc., which can be either Publicor Privateor Protectedor Friend. If they

    are Public, they can be accessed by creating objects of

    the Class. Privateand Protectedmembers can be accessed only by the functions inside

    theClass. Protectedmembers are much like Privatemembers, but they have some special use

    while inheriting aClass. We will see this later, in Inheritance (Lesson 5). Friendmembers can beaccessed only by elements of the same project, and not by the ones outside the current project. Let

    us expand our dogclass.

    Import the Systemnamespace (already available in .NET).

    Collapse |Copy Code

    ImportsSystem

    Animalsis a namespace.

    Collapse |Copy Code

    NamespaceAnimals

    Dogis a class in the namespace Animals.

    Collapse |Copy Code

    PublicClassDog'A public variable

    PublicAgeOfDog asInteger

    Barkis a function in this class. It is Public:

    Collapse |Copy Code

    PublicFunctionBark()

    Console.Writeline ("Dog is barking")

    EndFunction

    Walkis a function in this class. It is Private.

    Collapse |Copy Code

    PrivateFunctionWalk()Console.Writeline ("Dog is walking")

    EndFunctionEndClass

    EndNamespace

    Our Module:

    Collapse |Copy Code

    http://www.codeproject.com/Articles/8825/Object-Oriented-Programming-In-VB-NEThttp://www.codeproject.com/Articles/8825/Object-Oriented-Programming-In-VB-NEThttp://www.codeproject.com/Articles/8825/Object-Oriented-Programming-In-VB-NEThttp://www.codeproject.com/Articles/8825/Object-Oriented-Programming-In-VB-NEThttp://www.codeproject.com/Articles/8825/Object-Oriented-Programming-In-VB-NEThttp://www.codeproject.com/Articles/8825/Object-Oriented-Programming-In-VB-NEThttp://www.codeproject.com/Articles/8825/Object-Oriented-Programming-In-VB-NEThttp://www.codeproject.com/Articles/8825/Object-Oriented-Programming-In-VB-NEThttp://www.codeproject.com/Articles/8825/Object-Oriented-Programming-In-VB-NEThttp://www.codeproject.com/Articles/8825/Object-Oriented-Programming-In-VB-NEThttp://www.codeproject.com/Articles/8825/Object-Oriented-Programming-In-VB-NEThttp://www.codeproject.com/Articles/8825/Object-Oriented-Programming-In-VB-NEThttp://www.codeproject.com/Articles/8825/Object-Oriented-Programming-In-VB-NEThttp://www.codeproject.com/Articles/8825/Object-Oriented-Programming-In-VB-NEThttp://www.codeproject.com/Articles/8825/Object-Oriented-Programming-In-VB-NEThttp://www.codeproject.com/Articles/8825/Object-Oriented-Programming-In-VB-NEThttp://www.codeproject.com/Articles/8825/Object-Oriented-Programming-In-VB-NEThttp://www.codeproject.com/Articles/8825/Object-Oriented-Programming-In-VB-NEThttp://www.codeproject.com/Articles/8825/Object-Oriented-Programming-In-VB-NEThttp://www.codeproject.com/Articles/8825/Object-Oriented-Programming-In-VB-NEThttp://www.codeproject.com/Articles/8825/Object-Oriented-Programming-In-VB-NEThttp://www.codeproject.com/Articles/8825/Object-Oriented-Programming-In-VB-NEThttp://www.codeproject.com/Articles/8825/Object-Oriented-Programming-In-VB-NEThttp://www.codeproject.com/Articles/8825/Object-Oriented-Programming-In-VB-NET
  • 8/12/2019 Menggunakan Objek Kontrol Gambar Dan Pewaktu

    18/28

    PublicModulemodMain

    Execution will start from the Main()subroutine:

    Collapse |Copy Code

    SubMain()'Call our function. See below

    OurFunction()Endsub

    'OurFunction: Called from Main()

    FunctionOurFunction()DimJimmy asAnimals.Dog

    Jimmy=new Animals.Dog()

    'This will work, because Bark & Ageofdog are public

    Jimmy.BarkJimmy.AgeOfDog=10

    'Calling the Walk function will not work here, because'Walk() is outside the class Dog

    'So this is wrong. Uncomment this and try to compile, it will

    'cause an error.'Jimmy.Walk

    EndFunctionEndModule

    Additional Notes:

    EncapsulationPutting all the data and related functions in a Classis called Encapsulation.

    Data Hiding or Abstraction:Normally, in a Class, variables used to hold data (like the age of a dog) is declared as Private.

    Functions or property routines are used to access these variables. Protecting the data of an object

    from outside functions is called Abstraction or Data Hiding. This prevents accidental modification of

    data by functions outside the class.

    Lesson 3: Shared Functions

    The shared members in a class (both functions and variables) can be used without creating objects of

    a class as shown. The Sharedmodifier indicates that the method does not operate on a specific

    instance of a type and may be invoked directly from a type rather than through a particular instance

    of a type.

    Import the Systemnamespace (already available in .NET).

    http://www.codeproject.com/Articles/8825/Object-Oriented-Programming-In-VB-NEThttp://www.codeproject.com/Articles/8825/Object-Oriented-Programming-In-VB-NEThttp://www.codeproject.com/Articles/8825/Object-Oriented-Programming-In-VB-NEThttp://www.codeproject.com/Articles/8825/Object-Oriented-Programming-In-VB-NET
  • 8/12/2019 Menggunakan Objek Kontrol Gambar Dan Pewaktu

    19/28

    Collapse |Copy Code

    ImportsSystem

    Animalsis a namespace.

    Collapse |Copy Code

    NamespaceAnimals

    Dogis a class in the namespace Animals.

    Collapse |Copy Code

    ClassDog

    Barkis a now a Public, shared function in this class.

    Collapse |Copy Code

    PublicSharedFunctionBark()

    Console.Writeline ("Dog is barking")

    EndFunction

    Walkis a Publicfunction in this class. It is not shared.

    Collapse |Copy Code

    PublicFunctionWalk()

    Console.Writeline ("Dog is walking")

    EndFunctionEndClass

    EndNamespace

    Our Module:

    Collapse |Copy Code

    PublicModulemodMain

    Execution will start from the Main()subroutine.

    Collapse |Copy Code

    SubMain()'We can call the Bark() function directly,'with out creating an object of type Dog -

    'because it is shared.Animals.Dog.Bark()

    'We can call the Walk() function only

    'after creating an object, because

    'it is not shared.DimJimmy asAnimals.Dog

    Jimmy=new Animals.Dog()

    http://www.codeproject.com/Articles/8825/Object-Oriented-Programming-In-VB-NEThttp://www.codeproject.com/Articles/8825/Object-Oriented-Programming-In-VB-NEThttp://www.codeproject.com/Articles/8825/Object-Oriented-Programming-In-VB-NEThttp://www.codeproject.com/Articles/8825/Object-Oriented-Programming-In-VB-NEThttp://www.codeproject.com/Articles/8825/Object-Oriented-Programming-In-VB-NEThttp://www.codeproject.com/Articles/8825/Object-Oriented-Programming-In-VB-NEThttp://www.codeproject.com/Articles/8825/Object-Oriented-Programming-In-VB-NEThttp://www.codeproject.com/Articles/8825/Object-Oriented-Programming-In-VB-NEThttp://www.codeproject.com/Articles/8825/Object-Oriented-Programming-In-VB-NEThttp://www.codeproject.com/Articles/8825/Object-Oriented-Programming-In-VB-NEThttp://www.codeproject.com/Articles/8825/Object-Oriented-Programming-In-VB-NEThttp://www.codeproject.com/Articles/8825/Object-Oriented-Programming-In-VB-NEThttp://www.codeproject.com/Articles/8825/Object-Oriented-Programming-In-VB-NEThttp://www.codeproject.com/Articles/8825/Object-Oriented-Programming-In-VB-NEThttp://www.codeproject.com/Articles/8825/Object-Oriented-Programming-In-VB-NEThttp://www.codeproject.com/Articles/8825/Object-Oriented-Programming-In-VB-NEThttp://www.codeproject.com/Articles/8825/Object-Oriented-Programming-In-VB-NEThttp://www.codeproject.com/Articles/8825/Object-Oriented-Programming-In-VB-NEThttp://www.codeproject.com/Articles/8825/Object-Oriented-Programming-In-VB-NEThttp://www.codeproject.com/Articles/8825/Object-Oriented-Programming-In-VB-NEThttp://www.codeproject.com/Articles/8825/Object-Oriented-Programming-In-VB-NEThttp://www.codeproject.com/Articles/8825/Object-Oriented-Programming-In-VB-NEThttp://www.codeproject.com/Articles/8825/Object-Oriented-Programming-In-VB-NEThttp://www.codeproject.com/Articles/8825/Object-Oriented-Programming-In-VB-NEThttp://www.codeproject.com/Articles/8825/Object-Oriented-Programming-In-VB-NEThttp://www.codeproject.com/Articles/8825/Object-Oriented-Programming-In-VB-NEThttp://www.codeproject.com/Articles/8825/Object-Oriented-Programming-In-VB-NEThttp://www.codeproject.com/Articles/8825/Object-Oriented-Programming-In-VB-NET
  • 8/12/2019 Menggunakan Objek Kontrol Gambar Dan Pewaktu

    20/28

    Jimmy.Walk()

    'Now Guess? The WriteLine() function we used so far'is a shared function in class Console :)'Also, we can write the Main() function itself as a shared

    'function in a class. i.e Shared Sub Main(). Try

    'moving Main() from this module to the above class

    Endsub

    EndModule

    Lesson 4: Overloading

    Overloading is a simple technique, to enable a single function name to accept parameters of

    different type. Let us see a simple Adderclass. Import the Systemnamespace (already available in

    .NET).

    Collapse |Copy Code

    ImportsSystem

    ClassAdder

    Here, we have two Add()functions. This one adds two integers. Convert.ToString is equivalent

    to the good oldCStr.

    Collapse |Copy Code

    OverloadsPublicSubAdd(A asInteger, B asInteger)

    Console.Writeline ("Adding Integers: "+ Convert.ToString(a + b))

    EndSub

    This one adds two strings.

    Collapse |Copy Code

    OverloadsPublicSubAdd(A asString, B asString)Console.Writeline ("Adding Strings: "+ a + b)

    EndSub

    'And both have the same name. This is possible because, we used the'Overloads keyword, to overload them.

    'Here, we have the Main Function with in this class. When you write.'your main function inside the class, it should be a shared function.

    SharedSubMain()

    DimAdderObj asAdder

    'Create the object

    AdderObj=new Adder'This will invoke first function

    AdderObj.Add(10,20)'This will invoke second function

    AdderObj.Add("hello"," how are you")

    EndSub

    EndClass

    Lesson 5: Inheritance

    http://www.codeproject.com/Articles/8825/Object-Oriented-Programming-In-VB-NEThttp://www.codeproject.com/Articles/8825/Object-Oriented-Programming-In-VB-NEThttp://www.codeproject.com/Articles/8825/Object-Oriented-Programming-In-VB-NEThttp://www.codeproject.com/Articles/8825/Object-Oriented-Programming-In-VB-NEThttp://www.codeproject.com/Articles/8825/Object-Oriented-Programming-In-VB-NEThttp://www.codeproject.com/Articles/8825/Object-Oriented-Programming-In-VB-NEThttp://www.codeproject.com/Articles/8825/Object-Oriented-Programming-In-VB-NEThttp://www.codeproject.com/Articles/8825/Object-Oriented-Programming-In-VB-NEThttp://www.codeproject.com/Articles/8825/Object-Oriented-Programming-In-VB-NEThttp://www.codeproject.com/Articles/8825/Object-Oriented-Programming-In-VB-NEThttp://www.codeproject.com/Articles/8825/Object-Oriented-Programming-In-VB-NEThttp://www.codeproject.com/Articles/8825/Object-Oriented-Programming-In-VB-NET
  • 8/12/2019 Menggunakan Objek Kontrol Gambar Dan Pewaktu

    21/28

    Inheritance is the property in which, a derived class acquires the attributes of its base class. In simple

    terms, you can create or 'inherit' your own class (derived class), using an existing class (base class).

    You can use the Inheritskeyword for this.

    Let us see a simple example. Import the Systemnamespace (already available in .NET).

    Collapse |Copy Code

    ImportsSystem

    Our simple base class:

    Collapse |Copy Code

    ClassHuman'This is something that all humans do

    PublicSubWalk()

    Console.Writeline ("Walking")

    EndSub

    EndClass

    Now, let us derive a class from Human.

    A Programmeris a Human.

    Collapse |Copy Code

    ClassProgrammerInheritsHuman

    'We already have the above Walk() function'This is something that all programmers do ;)

    PublicSubStealCode()

    Console.Writeline ("Stealing code")EndSub

    EndClass

    Just a MainClass.

    Collapse |Copy Code

    ClassMainClass

    'Our main functionSharedSubMain()

    DimTom asProgrammer

    Tom=new Programmer

    'This call is okie because programmer got this function

    'from its base classTom.Walk()

    'This is also correct because Tom is a programmerTom.StealCode()

    EndSub

    EndClass

    http://www.codeproject.com/Articles/8825/Object-Oriented-Programming-In-VB-NEThttp://www.codeproject.com/Articles/8825/Object-Oriented-Programming-In-VB-NEThttp://www.codeproject.com/Articles/8825/Object-Oriented-Programming-In-VB-NEThttp://www.codeproject.com/Articles/8825/Object-Oriented-Programming-In-VB-NEThttp://www.codeproject.com/Articles/8825/Object-Oriented-Programming-In-VB-NEThttp://www.codeproject.com/Articles/8825/Object-Oriented-Programming-In-VB-NEThttp://www.codeproject.com/Articles/8825/Object-Oriented-Programming-In-VB-NEThttp://www.codeproject.com/Articles/8825/Object-Oriented-Programming-In-VB-NEThttp://www.codeproject.com/Articles/8825/Object-Oriented-Programming-In-VB-NEThttp://www.codeproject.com/Articles/8825/Object-Oriented-Programming-In-VB-NEThttp://www.codeproject.com/Articles/8825/Object-Oriented-Programming-In-VB-NEThttp://www.codeproject.com/Articles/8825/Object-Oriented-Programming-In-VB-NEThttp://www.codeproject.com/Articles/8825/Object-Oriented-Programming-In-VB-NEThttp://www.codeproject.com/Articles/8825/Object-Oriented-Programming-In-VB-NEThttp://www.codeproject.com/Articles/8825/Object-Oriented-Programming-In-VB-NEThttp://www.codeproject.com/Articles/8825/Object-Oriented-Programming-In-VB-NET
  • 8/12/2019 Menggunakan Objek Kontrol Gambar Dan Pewaktu

    22/28

    Additional Notes:

    MustInheritThe MustInheritkeyword specifies that a class cannot be instantiated and can be used only as a

    base class. I.e., if you declare our Humanclass as "MustInherit Class Human", then you can'tcreate objects of typeHumanwithout inheriting it.

    NotInheritableThe NotInheritable keyword specifies that a class cannot be inherited. I.e., if you specify

    'NotInheritableClass Human', no derived classes can be made from the Humanclass.

    Lesson 6: Overriding

    By default, a derived class Inheritsmethods from its base class. If an inherited property or methodneeds to behave differently in the derived class it can be overridden; that is, you can define a new

    implementation of the method in the derived class. The Overridablekeyword is used to mark a

    function as overridable. The keyword Overridesis used to mark that a function is overriding some

    base class function. Let us see an example.

    Import the Systemnamespace (already available in .NET).

    Collapse |Copy Code

    ImportsSystem

    Our simple base class:

    Collapse |Copy Code

    ClassHuman'Speak() is declared Overridable

    OverridablePublicSubSpeak()

    Console.Writeline ("Speaking")EndSub

    EndClass

    Now, let us derive a class from Human:

    An Indianis a Human:

    Collapse |Copy Code

    ClassIndianInheritsHuman

    'Let us make Indian speak Hindi, the National Language

    'in India

    'Speak() is overriding Speak() in its base class (Human)

    http://www.codeproject.com/Articles/8825/Object-Oriented-Programming-In-VB-NEThttp://www.codeproject.com/Articles/8825/Object-Oriented-Programming-In-VB-NEThttp://www.codeproject.com/Articles/8825/Object-Oriented-Programming-In-VB-NEThttp://www.codeproject.com/Articles/8825/Object-Oriented-Programming-In-VB-NEThttp://www.codeproject.com/Articles/8825/Object-Oriented-Programming-In-VB-NEThttp://www.codeproject.com/Articles/8825/Object-Oriented-Programming-In-VB-NEThttp://www.codeproject.com/Articles/8825/Object-Oriented-Programming-In-VB-NEThttp://www.codeproject.com/Articles/8825/Object-Oriented-Programming-In-VB-NEThttp://www.codeproject.com/Articles/8825/Object-Oriented-Programming-In-VB-NEThttp://www.codeproject.com/Articles/8825/Object-Oriented-Programming-In-VB-NEThttp://www.codeproject.com/Articles/8825/Object-Oriented-Programming-In-VB-NEThttp://www.codeproject.com/Articles/8825/Object-Oriented-Programming-In-VB-NET
  • 8/12/2019 Menggunakan Objek Kontrol Gambar Dan Pewaktu

    23/28

    OverridesPublicSubSpeak()Console.Writeline ("Speaking Hindi")

    'Important: As you expect, any call to Speak() inside this class'will invoke the Speak() in this class. If you need to

    'call Speak() in base class, you can use MyBase keyword.

    'Like this

    'Mybase.Speak()EndSub

    EndClass

    Just a class to put our Main().

    Collapse |Copy Code

    ClassMainClass

    'Our main functionSharedSubMain()

    'Tom is a generic HumanDimTom asHuman

    Tom=new Human

    'Tony is a human and an IndianDimTony asIndian

    Tony=new Indian'This call will invoke the Speak() function'in class Human

    Tom.Speak()

    'This call will invoke the Speak() function

    'in class Indian

    Tony.Speak()EndSub

    EndClass

    Lesson 7: Polymorphism

    Polymorphism is the property in which a single object can take more than one form. For example, if

    you have a base class named Human, an object of Humantype can be used to hold an object of any

    of its derived type. When you call a function in your object, the system will automatically determine

    the type of the object to call the appropriate function. For example, let us assume that you have a

    function named speak()in your base class. You derived a child class from your base class and

    overloaded the function speak(). Then, you create a child class object and assign it to a base class

    variable. Now, if you call the speak()function using the base class variable, the speak()function

    defined in your child class will work. On the contrary, if you are assigning an object of the base class

    to the base class variable, then thespeak()function in the base class will work. This is achieved

    through runtime type identification of objects. See the example.

    Import the Systemnamespace (already available in .NET).

    Collapse |Copy Code

    ImportsSystem

    http://www.codeproject.com/Articles/8825/Object-Oriented-Programming-In-VB-NEThttp://www.codeproject.com/Articles/8825/Object-Oriented-Programming-In-VB-NEThttp://www.codeproject.com/Articles/8825/Object-Oriented-Programming-In-VB-NEThttp://www.codeproject.com/Articles/8825/Object-Oriented-Programming-In-VB-NEThttp://www.codeproject.com/Articles/8825/Object-Oriented-Programming-In-VB-NEThttp://www.codeproject.com/Articles/8825/Object-Oriented-Programming-In-VB-NEThttp://www.codeproject.com/Articles/8825/Object-Oriented-Programming-In-VB-NEThttp://www.codeproject.com/Articles/8825/Object-Oriented-Programming-In-VB-NET
  • 8/12/2019 Menggunakan Objek Kontrol Gambar Dan Pewaktu

    24/28

    This example is exactly the same as the one we saw in the previous lesson. The only difference is in

    the Shared SubMain()in the class MainClass. So scroll down and see an example:

    Our simple base class:

    Collapse |Copy CodeClassHuman

    'Speak() is declared Overridable

    OverridablePublicSubSpeak()

    Console.Writeline ("Speaking")

    EndSub

    EndClass

    Now, let us derive a class from Human.

    An Indianis a Human.

    Collapse |Copy Code

    ClassIndianInheritsHuman

    'Let us make Indian speak Hindi, the National Language'in India

    'Speak() is overriding Speak() in its base class (Human)

    OverridesPublicSubSpeak()Console.Writeline ("Speaking Hindi")

    'Important: As you expect, any call to Speak() inside this class

    'will invoke the Speak() in this class. If you need to'call Speak() in base class, you can use MyBase keyword.

    'Like this'Mybase.Speak()

    EndSub

    EndClass

    Carefully examine the code in Main():

    Collapse |Copy Code

    ClassMainClass

    'Our main functionSharedSubMain()

    'Let us define Tom as a human (base class)

    DimTom asHuman

    'Now, I am assiging an Indian (derived class)Tom=new Indian

    'The above assignment is legal, because'Indian IS_A human.

    'Now, let me call Speak as

    Tom.Speak()

    'Which Speak() will work? The Speak() in Indian, or the

    'Speak() in human?'The question arises because, Tom is declared as a Human,

    'but an object of type Indian is assigned to Tom.

    'The Answer is, the Speak() in Indian will work. This is because,

    'most object oriented languages like Vb.net can automatically

    'detect the type of the object assigned to a base class variable.

    http://www.codeproject.com/Articles/8825/Object-Oriented-Programming-In-VB-NEThttp://www.codeproject.com/Articles/8825/Object-Oriented-Programming-In-VB-NEThttp://www.codeproject.com/Articles/8825/Object-Oriented-Programming-In-VB-NEThttp://www.codeproject.com/Articles/8825/Object-Oriented-Programming-In-VB-NEThttp://www.codeproject.com/Articles/8825/Object-Oriented-Programming-In-VB-NEThttp://www.codeproject.com/Articles/8825/Object-Oriented-Programming-In-VB-NEThttp://www.codeproject.com/Articles/8825/Object-Oriented-Programming-In-VB-NEThttp://www.codeproject.com/Articles/8825/Object-Oriented-Programming-In-VB-NEThttp://www.codeproject.com/Articles/8825/Object-Oriented-Programming-In-VB-NEThttp://www.codeproject.com/Articles/8825/Object-Oriented-Programming-In-VB-NEThttp://www.codeproject.com/Articles/8825/Object-Oriented-Programming-In-VB-NEThttp://www.codeproject.com/Articles/8825/Object-Oriented-Programming-In-VB-NET
  • 8/12/2019 Menggunakan Objek Kontrol Gambar Dan Pewaktu

    25/28

    'This is called PolymorphismEndSub

    EndClass

    Lesson 8: Constructors & Destructors

    Import the Systemnamespace (already available in .NET).

    Collapse |Copy Code

    ImportsSystem

    A Constructor is a special function which is called automatically when a class is created. In VB.NET,

    you should useuseNew()to create constructors. Constructors can be overloaded (see Lesson 4), but

    unlike the functions, theOverloadskeyword is not required. A Destructor is a special function which

    is called automatically when a class is destroyed. In VB.NET, you should use useFinalize()routine

    to create Destructors. They are similar toClass_Initializeand Class_Terminatein VB 6.0.

    Dogis a class:

    Collapse |Copy Code

    ClassDog

    'The age variable

    PrivateAge asinteger

    The default constructor:

    Collapse |Copy Code

    PublicSubNew()

    Console.Writeline ("Dog is Created With Age Zero")

    Age=0

    EndSub

    The parameterized constructor:

    Collapse |Copy Code

    PublicSubNew(val asInteger)

    Console.Writeline ("Dog is Created With Age "+ Convert.ToString(val))

    Age=val

    EndSub

    This is the destructor:

    Collapse |Copy Code

    OverridesProtectedSubFinalize()

    Console.Writeline ("Dog is Destroyed")

    EndSub

    'The Main Function

    http://www.codeproject.com/Articles/8825/Object-Oriented-Programming-In-VB-NEThttp://www.codeproject.com/Articles/8825/Object-Oriented-Programming-In-VB-NEThttp://www.codeproject.com/Articles/8825/Object-Oriented-Programming-In-VB-NEThttp://www.codeproject.com/Articles/8825/Object-Oriented-Programming-In-VB-NEThttp://www.codeproject.com/Articles/8825/Object-Oriented-Programming-In-VB-NEThttp://www.codeproject.com/Articles/8825/Object-Oriented-Programming-In-VB-NEThttp://www.codeproject.com/Articles/8825/Object-Oriented-Programming-In-VB-NEThttp://www.codeproject.com/Articles/8825/Object-Oriented-Programming-In-VB-NEThttp://www.codeproject.com/Articles/8825/Object-Oriented-Programming-In-VB-NEThttp://www.codeproject.com/Articles/8825/Object-Oriented-Programming-In-VB-NEThttp://www.codeproject.com/Articles/8825/Object-Oriented-Programming-In-VB-NEThttp://www.codeproject.com/Articles/8825/Object-Oriented-Programming-In-VB-NEThttp://www.codeproject.com/Articles/8825/Object-Oriented-Programming-In-VB-NEThttp://www.codeproject.com/Articles/8825/Object-Oriented-Programming-In-VB-NEThttp://www.codeproject.com/Articles/8825/Object-Oriented-Programming-In-VB-NEThttp://www.codeproject.com/Articles/8825/Object-Oriented-Programming-In-VB-NEThttp://www.codeproject.com/Articles/8825/Object-Oriented-Programming-In-VB-NEThttp://www.codeproject.com/Articles/8825/Object-Oriented-Programming-In-VB-NEThttp://www.codeproject.com/Articles/8825/Object-Oriented-Programming-In-VB-NEThttp://www.codeproject.com/Articles/8825/Object-Oriented-Programming-In-VB-NET
  • 8/12/2019 Menggunakan Objek Kontrol Gambar Dan Pewaktu

    26/28

    SharedSubMain()DimJimmy, Jacky asDog

    'Create the objects'This will call the default constructor

    Jimmy=new Dog

    'This will call the parameterized constructor

    Jacky=new Dog(10)EndSub

    'The Destruction will be done automatically, when'the program ends. This is done by the Garbage

    'Collector.

    EndClass

    Lesson 9: Property Routines

    You can use both properties and fields to store information in an object. While fields are

    simply Publicvariables, properties use property procedures to control how values are set or

    returned. You can use the Get/Setkeywords for getting/setting properties. See the following

    example. Import the Systemnamespace (already available in .NET).

    Collapse |Copy Code

    ImportsSystem

    Dogis a class.

    Collapse |Copy Code

    PublicClassDog

    'A private variable to hold the value

    PrivatemAgeOfDog asInteger

    This is our property routine:

    Collapse |Copy Code

    PublicPropertyAge() AsInteger

    'Called when someone tries to retreive the value

    GetConsole.Writeline ("Getting Property")

    ReturnmAgeOfdog

    EndGetSet(ByValValue AsInteger)

    'Called when someone tries to assign a valueConsole.Writeline ("Setting Property")

    mAgeOfDog=ValueEndSet

    EndProperty

    EndClass

    Another class:

    Collapse |Copy Code

    http://www.codeproject.com/Articles/8825/Object-Oriented-Programming-In-VB-NEThttp://www.codeproject.com/Articles/8825/Object-Oriented-Programming-In-VB-NEThttp://www.codeproject.com/Articles/8825/Object-Oriented-Programming-In-VB-NEThttp://www.codeproject.com/Articles/8825/Object-Oriented-Programming-In-VB-NEThttp://www.codeproject.com/Articles/8825/Object-Oriented-Programming-In-VB-NEThttp://www.codeproject.com/Articles/8825/Object-Oriented-Programming-In-VB-NEThttp://www.codeproject.com/Articles/8825/Object-Oriented-Programming-In-VB-NEThttp://www.codeproject.com/Articles/8825/Object-Oriented-Programming-In-VB-NEThttp://www.codeproject.com/Articles/8825/Object-Oriented-Programming-In-VB-NEThttp://www.codeproject.com/Articles/8825/Object-Oriented-Programming-In-VB-NEThttp://www.codeproject.com/Articles/8825/Object-Oriented-Programming-In-VB-NEThttp://www.codeproject.com/Articles/8825/Object-Oriented-Programming-In-VB-NEThttp://www.codeproject.com/Articles/8825/Object-Oriented-Programming-In-VB-NEThttp://www.codeproject.com/Articles/8825/Object-Oriented-Programming-In-VB-NEThttp://www.codeproject.com/Articles/8825/Object-Oriented-Programming-In-VB-NEThttp://www.codeproject.com/Articles/8825/Object-Oriented-Programming-In-VB-NET
  • 8/12/2019 Menggunakan Objek Kontrol Gambar Dan Pewaktu

    27/28

    ClassMainClass

    'Our main function. Execution starts here.SharedSubMain()'Let us create an object.

    DimJimmy asDog

    Jimmy=new Dog

    'We can't access mAgeofDog directly, so we should

    'use Age() property routine.'Set it. The Age Set routine will workJimmy.Age=30

    'Get it back. The Age GEt routine will work

    DimcurAge=Jimmy.Age()

    EndSub

    EndClass

    Lesson 10: A simple program

    Let us analyze a simple program. First, let us import the required namespaces:

    Collapse |Copy Code

    ImportsSystemImportsSystem.ComponentModel

    ImportsSystem.Windows.Forms

    ImportsSystem.Drawing

    'We are inheriting a class named SimpleForm, from the

    'class System.Windows.Forms.Form''i.e, Windows is a namespace in system, Forms is a

    'namespace in Windows, and Form is a class in Forms.

    PublicClassSimpleForm

    InheritsSystem.Windows.Forms.Form

    'Our constructorPublicSubNew()

    'This will invoke the constructor of the base'class

    MyBase.New()

    Set the textproperty of this class. We inherited this property from the base class:

    Collapse |Copy Code

    Me.Text = "Hello, How Are You?"

    EndSub

    EndClass

    Collapse |Copy Code

    PublicClassMainClassSharedSubMain()

    'Create an object from our SimpleForm classDimsf asSimpleFormsf=new SimpleForm

    'Pass this object to the Run() function to start

    System.Windows.Forms.Application.Run(sf)EndSub

    EndClass

    http://www.codeproject.com/Articles/8825/Object-Oriented-Programming-In-VB-NEThttp://www.codeproject.com/Articles/8825/Object-Oriented-Programming-In-VB-NEThttp://www.codeproject.com/Articles/8825/Object-Oriented-Programming-In-VB-NEThttp://www.codeproject.com/Articles/8825/Object-Oriented-Programming-In-VB-NEThttp://www.codeproject.com/Articles/8825/Object-Oriented-Programming-In-VB-NEThttp://www.codeproject.com/Articles/8825/Object-Oriented-Programming-In-VB-NEThttp://www.codeproject.com/Articles/8825/Object-Oriented-Programming-In-VB-NEThttp://www.codeproject.com/Articles/8825/Object-Oriented-Programming-In-VB-NEThttp://www.codeproject.com/Articles/8825/Object-Oriented-Programming-In-VB-NEThttp://www.codeproject.com/Articles/8825/Object-Oriented-Programming-In-VB-NEThttp://www.codeproject.com/Articles/8825/Object-Oriented-Programming-In-VB-NEThttp://www.codeproject.com/Articles/8825/Object-Oriented-Programming-In-VB-NET
  • 8/12/2019 Menggunakan Objek Kontrol Gambar Dan Pewaktu

    28/28

    That is it. Now you can atleast read and understand most of those VB.NET source code, and probably

    implement more OOP features in your VB.NET programs. Now, in my next article, I'll try to cover the

    patterns and practices in VB.NET.