TIPE DATA VISUAL BASIC
Data adalah nilai
mentah yang tidak memiliki arti jika berdiri sendiri. Data pada Visual Basic
dianggap sebagai nilai – nilai yang di manipulasi dalam pembuatan program. Data
dan program saling berkaitan erat. Data adalah nilai yang dibutuhkan oleh
aplikasi (misalnya npm, nama, alamat dan sebagainya), sedangkan program adalah
instruksi yang digunakan untuk memanipulasi data tersebut (misalnya menyimpan
data, menghitung data dan sebagainya). Keduanya adalah unsur – unsur utama
didalam membuat program aplikasi yang sempurna.
Tipe Data
Tipe – tipe data
yang terdapat dalam Visual Basic diantaranya dapat terlihat dibawah ini :
1. Integer, long : tipe data untuk angka bulat
2. Single, double : tipe data untuk angka pecahan/desimal
3. Currency : tipe data untuk angka mata uang
4. String : tipe data untuk teks
5. Boolean : tipe data logika ( True/False)
6. Date : tipe data waktu/tangggal
7. Object : tipe data untuk sebuah objek misalnya gambar
8. Variant : tipe data variant
Sebuah tipe data
menggunakan sejumlah memori dan memiliki jangkauan nilai yang dapat
disimpannya. Sifat – sifat tersebut akan menentukan tipe data mana
yang sebaiknya dipilih oleh pemrograman untuk menyimpan suatu data.
Variabel
Variabel adalah
sebuah lokasi tempat komputer menyimpan data yang akan diolah atau diberi
nilai. Variabel mempengaruhi tipe dan jenis sifat data yang akan
disimpannya.
Konstanta
Seperti halnya
variabel, konstanta adalah data yang disimpan didalam memori komputer. Namun
berbeda dengan variabel nilai, konstanta didefinisikan sekali diawal dan
nilainya tidak dapat diubah.
Konstanta
digunakan untuk menyimpan nilai – nilai tertentu yang akan sering digunakan dan
tidak akan berubah dalam pemrograman, misalnya konstanta phi yang akan
digunakan untuk menghitung luas sebuah lingkaran.
Deklarasi Variabel dan Definisi Konstanta
Sebelum dapat
digunakan, sebuah variabel harus dideklarasikan begitu pula halnya dengan
konstanta harus didefinisikan terlebih dahulu. Pendeklarasian variabel berarti
”memesan” sebuah ruang di memori komputer untuk menempatkan data yang akan
ditampung oleh variabel tersebut.
Visual Basic memiliki
tipe data varian, yaitu tipe data yang dapat menampung semua jenis data. Visual Basic tidak mengharuskan setiap variable dideklarasikan terlebih
dahulu, apabila variabel tidak dideklarasikan, maka Visual Basic akan
menganggapnya bertipe variant.
Tidak
diwajibkannya deklarasi variable memiliki potensi untuk menimbulkan masalah,
seperti kesalahan pada penulisan variabel, akan membuat Visual Basic menganggap
variabel tersebut adalah variabel baru. Oleh sebab itu sebaiknya setiap
variabel harus dideklarasikan. Untuk memaksa Visual Basic memberi peringatan
apabila terdapat variabel yang tidak dideklarasikan, tuliskan pernyataan Option Explicit di awal setiap modul.
Berikut ini
adalah syntax deklarasi variabel Visual Basic :
Dim nama_variabel as tipe_variabel
Public nama_variabel as tipe_variabel
Private nama_variabel as tipe_variabel
Static nama_variabel as tipe_variabel
Empat baris
diatas menunjukkan terdapat 4 cara untuk mendeklarasikan variabel pada Visual
Basic.
Dim, Public,
Private, Static dan As adalah keyword atau reserved word (kata kunci atau kata
simpan), yaitu kata yang telah didefinisikan Visual Basic sehingga tidak boleh
digunakan sebagai nama variabel atau nama konstanta.
·
Dim adalah bentuk
standar pendeklarasian variabel di Visual Basic. Baik variabel dan konstanta
memiliki ruang lingkup, yaitu pada bagian mana di dalam program keduanya dapat
digunakan.
·
Public akan menyebabkan
variabel akan dikenal diseluruh bagian program.
·
Private akan menyebabkan
variabel hanya dikenal pada wilayah tempat variabel atau konstanta
dideklarasikan.
·
Static akan
mempertahankan perubahan nilai variabel, hal ini ditujukan untuk variabel yang
dideklarasikan disebuah prosedur / fungsi, dimana Visual Basic akan menghapus
nilai variabel tersebut setelah prosedur / fungsi dijalankan apabila tidak
dituliskan dalam bentuk static.
Contoh deklarasi
variabel Luas bertipe Integer :
Dim Luas as Integer
Bentuk syntax
definisi konstanta :
Const NAMA_KONSTANTA [As Tipe_Konstanta] =
Nilai_Konstanta
Perhatikan tanda
kurung siku [ ], menyatakan bahwa penulisan nilai di dalamnya bersifat optional
(tidak wajib), Visual Basic akan mengenali tipe data apa yang paling sesuai
dengan nilai konstanta apabila pemrograman tidak menuliskannya.
Contoh :
‘
deklarasi konstanta
Const
phi As Single = 3.14 Sama
dengan : Const phi = 3.14
Perhatikan tanda
petik tunggal (‘) digunakan Visual Basic untuk menandakan komentar yaitu bagian
pada program yang bukan bagian kode program dan tidak akan mempengaruhi proses
pemrograman, komentar dituliskan untuk memberi penjelasan pada program.
Beberapa aturan
pemilihan nama variabel :
1.
Non Case Sensitive (tidak akan membedakan
huruf kecil dan huruf besar).
Catatan : secara otomatis Visual Basic dapat merubah penulisan variabel –
variabel kedalam bentuk huruf besar/kecil sesuai pada saat pendeklarasian.
2.
Diawali huruf.
3. Tidak mengandung spasi atau karakter putih (karakter yang tidak tampak)
lainnya.
4. Tidak mengandung titik (.)
5. Harus unik dalam ruang lingkupnya tidak boleh ada variabel dengan nama
sama.
6. Tidak lebih dari 255 karakter.
Setelah
dideklarasikan, variabel dapat digunakan (diberi nilai, ditampilkan dan
sebagainya).
Syntax penugasan
/ pemberian nilai (assigment)
Nama variabel = nilai variabel
Contoh :
Luas = 1000
Untuk data
string, nilai harus diapit tanda petik ganda.
Contoh :
Nama = ”Wahyu Dwi Afandi”