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.
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








