VB6: Coding Techniques (Option Explicit)

Soal jawab berkenaan pengaturcaraan seperti C/C++, Java, Visual Basic, ASP, .Net, PHP, JSP dan sebagainya.

Moderator: zamri

VB6: Coding Techniques (Option Explicit)

Postby zamri » Thu Feb 17, 2005 8:50 am

1.Why should I use Option Explicit?

Option Explicit is used in the General Declarations section of any module (Class, Form, General, etc.) to direct the compiler not to allow hot variable declaration. Hot, or on-the-fly, variable declaration is the practice of inventing new variables within code without any formal declaration statements. While this can be very convenient, ultimately it can lead to many more troubles than those which are solved by requiring formal declarations.

First of all, there’s simple syntax checking. Nobody is a perfect typist. We all make mistakes. When you are programming, mistakes can lead to unpredictable results. If you are properly using self documenting variable names, like LoopIndex, ClassroomCount, or MaximumOribitCircumference, to name a few examples, then the risk of misspelling one of these variables becomes much greater. However, rather than use this as an argument for using simple variable names like x, y and z, it becomes an argument for Option Explicit. When variables must be declared, the Visual Basic compiler registers an error if you use an unrecognized variable name. So if you type Fiend when you meant Friend, instead of your program acting like the former, it will instead warn you at compile that the variable Fiend is undeclared. This might be a bit of brutal truth, but what are Friends for?

In addition to the hours of debugging that can be avoided by using Option Explicit, proper variable declaration helps to save resources, time, and encourages more efficient programming by forcing the programmer to plan out procedure operations in advance.

There’s one other benefit to Option Explicit which should be mentioned here. If you don’t use it, your life expectancy may be prematurely decreased in a significant fashion by whoever takes over code maintenance after you move on to your next project. :smile:

2.What is passing by reference and by value?

When a variable is passed to a procedure, it may be passed in one of two ways: By Reference or By Value.

A variable is really a block of memory that contains a value, and has a name by which it may be referred to (the variable name). Passing a variable by value means that the number or string stored in memory is passed to the procedure, but the original variable in the calling procedure can not be affected. Visual Basic actually creates a new instance of that variable for use by the receiving procedure.

Passing a variable by reference, on the other hand, allows the procedure which receives the variable to actually change the variable’s value in the calling function. Visual Basic passes the address in memory of the variable to the procedure, and changes made to that value are reflected in the function which called it.

Here’s an example.

Code: Select all
CODE
Private Sub NoChange(ByVal ThisVal as Variant)

Debug.Print ThisVal ‘ Received literal 5

ThisVal = ThisVal + 1



End Sub



Private Sub DoesChange(ByRef ThisVal as Variant)



Debug.Print ThisVal ‘ Received address of ThisVal

ThisVal = ThisVal + 1



End Sub



Sub Main()



Dim x as Integer

X = 5

NoChange X

Debug.Print X ‘ X still = 5

DoesChange X

Debug.Print X ‘ X now = 6



End Sub



By default, Visual Basic passes all variables by Reference, not by value. However, it is generally not recommended that functions change the values they receive, because if the change to the parameter is not known by the calling function, there can be unpredictable results. Typical examples of functions which would properly change a parameter would be: StripSpaces(thestring), ChangeDblQuotetoSingleQuote(TheString), IncrementByOne(TheValue).

3.Why should/shouldn't I use gosub and goto?

Goto and Gosub are the wicked step children left over from the early days of BASIC when it was not nearly as structured or object-oriented as it is now. These statements violate normal, logical program flow (if you've ever seen a nice, complicated program written in BASIC, you'll know what the term "spaghetti code" really means.)

I have never needed to use Goto in Visual Basic; there are always alternatives. I seriously recommend staying away from Gosub and Goto except for one case. Error Trapping--trapping errors in Visual Basic is not something that follows structured programming rules.

When you error trap, you have the option whether to use Goto:

Code: Select all
CODE
*** Code fragment ***



On Error Resume Next

' Do something which may cause an error

' Test for errors & react to them if necessary

' Continue processing



*** or ***



On Error Goto MyErrorTrap

' Do something which may cause an error

' If an error was raised, program flow jumps down to the error trap.

' Continue processing

Exit Sub

MyErrorTrap:

' decide what error was raised & do something about it.

Resume Next





*** End Code fragment ***



The differences in the two techniques are minimal...one conveniently negates the need for Goto; the other is more structured. It will ultimately be your decision to pick a method...whichever you are more comfortable with.

Artikel ini disumbangkan oleh saudara bulan_terang
User avatar
zamri
Pentadbir Forum
Pentadbir Forum
 
Posts: 6260
Joined: Wed Feb 16, 2005 4:18 pm
Location: Bilik Server
Gender: Male

OPTION EXPLICIT...

Postby JIMbo » Thu Jul 21, 2005 1:08 pm

maknanya kalo kita bwat kan OPTION EXPLICIT , kita dah tak payah la declare secara General lagi..

kalo tak silap aku la kan, cuma nama dia je la lain ( antara OPTION EXPLICIT ngan GENERAL)

sebab aku tak nampak pun perbezaan yang nyata lagi

nak kena blaja lagi nie

huahuahuahuahua
- plan wisely, execute accordingly, maintain consistently -
User avatar
JIMbo
Ahli Aktif
Ahli Aktif
 
Posts: 3467
Joined: Wed Jun 08, 2005 3:35 pm
Location: Kuantan
Gender: Male

Postby zamri » Thu Jul 21, 2005 6:56 pm

bukan cam tu. kalau setkan option explicit, memana variable yg nak diguna, kena declare dulu. kalau tak takleh guna. kalau tak set option explicit, maknanya kita boleh guna variable tanpa buat decalaration. kalau tak kena caranya, boleh susah nak debug. mana2 programmer yg baik, mesti declare variable dan setkan option explcit kat atas sekali dalam koding.
| KtnLUG | My Blog |

They don't care how much you know, until they know how much you care.
-----------
Knowledge is better than wealth because knowledge looks after you while you have to look after your wealth; knowledge determines your actions while wealth is determined by your actions; and knowledge increases when you use it but wealth decreases when you use it. -- Saidina Ali Abi Talib
User avatar
zamri
Pentadbir Forum
Pentadbir Forum
 
Posts: 6260
Joined: Wed Feb 16, 2005 4:18 pm
Location: Bilik Server
Gender: Male

Postby NurSyazlianee » Mon Jul 25, 2005 10:33 am

En.zam,saya nak tau psl C shrap(kalo tak salah eja la kan...).dngr kater skrg bnyk company yg gnkn c sharp untuk sistem dieorg menggantikan C++ yang sedia ader.kalo en Zam ader aper2 info psl ni boleh citer skit tak???mcm seronok jer programming br ni....
*NeVer GiVe Up...*
- LiFe is Simple when You Think It Simple -
NurSyazlianee
Semester 3
Semester 3
 
Posts: 160
Joined: Thu Feb 17, 2005 7:58 am
Location: sebuah u di hujung selangor.....
Gender: Female

Postby zamri » Mon Jul 25, 2005 11:28 am

c sharp ni lebih kurang Java koding dia dan lebih kepada Object Oriented programming. nak tahu lebih banyak, beli la buku atau tanya pakcik Google
| KtnLUG | My Blog |

They don't care how much you know, until they know how much you care.
-----------
Knowledge is better than wealth because knowledge looks after you while you have to look after your wealth; knowledge determines your actions while wealth is determined by your actions; and knowledge increases when you use it but wealth decreases when you use it. -- Saidina Ali Abi Talib
User avatar
zamri
Pentadbir Forum
Pentadbir Forum
 
Posts: 6260
Joined: Wed Feb 16, 2005 4:18 pm
Location: Bilik Server
Gender: Male

Postby sharif » Mon Jul 25, 2005 2:42 pm

Daripada Kaab b. Iyad r.a. katanya, aku mendengar Rasulullah s.a.w. bersabda; "Sesungguhnya bagi tiap-tiap umat itu ada ujian dan ujian bagi umatku ialah harta." - Riwayat At-Tirmidzi
User avatar
sharif
Semester 4
Semester 4
 
Posts: 554
Joined: Thu Feb 24, 2005 4:48 am
Location: Alam Siber
Gender: None specified

???

Postby JIMbo » Thu Aug 04, 2005 11:59 am

ini hapa daaaa.. pening lalat nak jadik penin tebuan lak nie...

xplen la cara senang2 sket..
huahuahuahuahua
- plan wisely, execute accordingly, maintain consistently -
User avatar
JIMbo
Ahli Aktif
Ahli Aktif
 
Posts: 3467
Joined: Wed Jun 08, 2005 3:35 pm
Location: Kuantan
Gender: Male

Postby bulan_terang » Thu Aug 04, 2005 1:22 pm

Ada member saya cakap C-Hash... <---kui kui kui kena gelak wehh
Image
bulan_terang
Semester 2
Semester 2
 
Posts: 81
Joined: Wed Feb 16, 2005 9:45 pm
Location: Kuantan, Pahang
Gender: None specified

Postby amd2_duocore » Sun Aug 27, 2006 5:16 pm

Ada member saya cakap C-Hash... <---kui kui kui kena gelak wehh


ada ka gelak kat org tak baik wei......
org br membangun memang cam gitu apa laa bulan_terang ni .....


tulis laa dalam bahasa ibunda takkan nak jd cam sharifah amani kot......

:lol: :lol: :lol: :lol: :lol: :lol:
~=| MrBad |=~
Registration FXOpen
Auto Forex Expert Advisor Blog | Zubyte Hosting | SpaceUTM | MyMode (Agen Kedah) - Register RM65
SMS:0194611526 @ yM ID:mr.bad03 | msn ID:mr.bad03
Plan Your Trade and Trade Your Plan
User avatar
amd2_duocore
Semester 3
Semester 3
 
Posts: 316
Joined: Thu Aug 24, 2006 12:55 am
Location: SP, Kedah
Gender: Male

Re: VB6: Coding Techniques (Option Explicit)

Postby NurSyazlianee » Tue Apr 29, 2008 9:45 am

a'kum, mntk tolong en zam. mcm maner nk export daru bckup data using vb? skg ni, data dah boleh backup..cumer mcm maner nak amk blik/export semula dari bckup file tu..coding untuk bckup yg aku guner:

Private Sub cmdbackup_Click()
'coding for backup data

m = MsgBox("Do You Want To Make A Backup Data?", vbYesNo, "Backup")
If m = vbNo Then

Else
csave.FileName = "backup"
csave.ShowSave ' open common dialogbox with save function
path = csave.FileName + ".mdb"
Debug.Print path
path1 = App.path + "\calibration.mdb"
Debug.Print path1

Set fso = CreateObject("Scripting.FileSystemObject") ' copy database calibration
fso.CopyFile path1, path

End If
End Sub
*NeVer GiVe Up...*
- LiFe is Simple when You Think It Simple -
NurSyazlianee
Semester 3
Semester 3
 
Posts: 160
Joined: Thu Feb 17, 2005 7:58 am
Location: sebuah u di hujung selangor.....
Gender: Female

Re: VB6: Coding Techniques (Option Explicit)

Postby zamri » Tue Apr 29, 2008 10:01 am

waalaikumsalam wbt,

Nak restore dalam VB jugak ke? Data yg dibackup tu dari DB ke? Kalau nak restore ke DB, dah tak ingat la coding dia. Dah lama tinggal VB ni. Tp nak restore ke DB, boleh guna function restore dari dalam DB tu. Kalau nak buat coding, buat masa ni kena google dulu la. Apa kata kita sama2 google. :mrgreen:
| KtnLUG | My Blog |

They don't care how much you know, until they know how much you care.
-----------
Knowledge is better than wealth because knowledge looks after you while you have to look after your wealth; knowledge determines your actions while wealth is determined by your actions; and knowledge increases when you use it but wealth decreases when you use it. -- Saidina Ali Abi Talib
User avatar
zamri
Pentadbir Forum
Pentadbir Forum
 
Posts: 6260
Joined: Wed Feb 16, 2005 4:18 pm
Location: Bilik Server
Gender: Male

Re: VB6: Coding Techniques (Option Explicit)

Postby NurSyazlianee » Tue Apr 29, 2008 10:16 am

data tu backup kat maner2 yang pengguna nak..aku try dulu, tak boleh nanti aku ckp..'
*NeVer GiVe Up...*
- LiFe is Simple when You Think It Simple -
NurSyazlianee
Semester 3
Semester 3
 
Posts: 160
Joined: Thu Feb 17, 2005 7:58 am
Location: sebuah u di hujung selangor.....
Gender: Female

Re: VB6: Coding Techniques (Option Explicit)

Postby JIMbo » Tue Apr 29, 2008 1:20 pm

nice..nice..nice..
- plan wisely, execute accordingly, maintain consistently -
User avatar
JIMbo
Ahli Aktif
Ahli Aktif
 
Posts: 3467
Joined: Wed Jun 08, 2005 3:35 pm
Location: Kuantan
Gender: Male

Re: VB6: Coding Techniques (Option Explicit)

Postby alex88 » Thu Apr 08, 2010 10:45 pm

ADVERTISEMENT IS NOT ALLOWED HERE. PLS POST IN APPROPRIATE FORUM. IGNORING THIS WARNING WILL RESULT IN ACCOUNT SUSPENSION OR BANNED FROM THIS FORUM.
[Reveal] Spoiler: SPAM
1.Business apakah yang produknya boleh dijual setiap hari?

Produk kesihatan ?

2.Business apakah yang klien perlukan untuk kehidupan seharian?

Makanan?

3.Business apakah di mana klien jumpa anda daripada anda berjumpa dengan klien?

Buka kedai runcit?

4.Business apakah yang di mana modalnya rendah tapi pasarannya besar?

Jual pen?

5.Business apakah di mana syarikat boleh kekal kukuh selamanya?

Tak pernah dengar syarikat tu?

Jika anda ingin tahu business yang ada 5 elemen di atas, klik di sini.

Image
Image
User avatar
alex88
Semester 1
Semester 1
 
Posts: 1
Joined: Tue Apr 06, 2010 8:10 pm
Gender: Male


Return to Pengaturcaraan dan Pengaturcaraan Internet

Who is online

Users browsing this forum: No registered users and 1 guest