Monday 25 May 2015

Class dan Object Pada VB [Pertemuan XVI]


Dasar OOP Visual Basic merupakan bahasa yang Object-Based (komponen-komponen program dibuat dalam bentuk objek), sedangkan VB.NET adalah Object Oriented, hal ini berarti bahwa VB.NET merupakan bahasa yang benar-benar berorientasi object dengan mendukung empat pilar utama dari OOP yaitu Polymorphism, Inheritance, Abstraction dan Encapsulation.

Untuk lebih memahami tentang class dan object, kita akan membuat sebuah aplikasi menggunakan class dan object pada pemrograman visual basic 2008

Pertama buat sebuah project baru dengan cara memilih file – new project – new form windows application




Langkah selanjutnya buat sebuah form baru dengan nama Form1 dan desain seperti berikut ini lengkap dengan penamaan pada propertiesnya



Kemudian buat class baru dengan cara berikut ini :



Setelah class telah dibuat langkah selanjutnya adalah mengetikan sintaks berikut ini :


'@desc: 1 : Classes and objects

'================================================================
'Author: Anoop - anoop@logicmatrixonline.com
'        http://www.logicmatrixonline.com/anoop
'================================================================

'LESSON 1: NAMESPACES, CLASSES AND OBJECTS, MODULES
'====================================================

'// 1) A Namespace
'====================================
'
'In 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 'imports' keyword is used to import a namespace to your project.
'.NET framework provides a rich set of built in classes, grouped
'to various namespaces.

'In this lesson, we are using the system namespace.



'-----------------------------------------------------------------
'Import the System namespace (already available in .NET)
Imports System
'-----------------------------------------------------------------



'// 2) A Class
'====================================
'
'Probably, you are already familiar with classes and objects
'Simply speaking, a class is a definition of a real life
'object. For example, Human is a class for representing all
'human beings. Dog is a class to represent all Dogs.

'Classes can contain functions too.



'-----------------------------------------------------------------
'Animals is a namespace
Namespace Animals

    'Dog is a class in the namespace Animals
    Class Dog

        'Bark is a function in this class
        Function Bark()
            Console.WriteLine("Dog is barking")
            MsgBox("Dog is barking")
        End Function

    End Class

End Namespace
'-----------------------------------------------------------------



'// 3) An Object
'====================================
'
'An object is an instance of a class. For example,
'Jimmy is an object of type Dog. We will create
'an object in the next section. Read on.


'// 4) Modules
'====================================
'
'You can use modules to write common functions. A module is a
'group of functions. Unlike functions in classes, public functions
'in modules can be called directly from some where else.

'VB provides Functions and Subroutines. Functions and Sub
'routines are almost same, but subroutines can't return
'a value.
'

'-----------------------------------------------------------------


Public Module modMain

    'Execution will start from the Main() subroutine

    Sub Main()

        'Call our function. See below
        OurFunction()
    End Sub


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

    Function OurFunction()
        '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).

        Dim Jimmy As Animals.Dog

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

        Jimmy = New Animals.Dog()

        'Another way to create an object is
        'Dim Jimmy as new Dog

        'Call Jimmy's Main Function      
        Jimmy.Bark()

    End Function

End Module

'-----------------------------------------------------------------
Setelah itu, pada Form1.vb ketikan pada btnTampil sintaks berikut ini :
Private Sub btnTampil_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTampil.Click
        Main()
    End Sub
Sekian Tutorial yang bisa saya berikan untuk teman-teman sekalian, mohon maaf apabila masih banyak kekurangan, semoga tutorial ini bisa bermanfaat bagi yang membutuhkan Aamiin.