![]() |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
LESSON 2 for
If you are just starting out with the language, why not pick up a few good habits right now and it may make your life a lot easier down the road.
Naming conventionsThese are the rules to follow when naming elements in VB - variables, constants, controls, procedures, and so on:
Data types
In all probability, in 90% of your applications you will use at most six types: String, Integer, Long, Single, Boolean and Date. The Variant type is often used automatically when type is not important. A Variant-type field can contain text or numbers, depending on the data that is actually entered. It is flexible but it is not very efficient in terms of storage. Declaring variablesDeclaring a variable means giving it a name, a data type and sometimes an initial value. The declaration can be explicit or implicit.An explicit declaration: variable is declared in the Declarations Section or at the beginning of a Procedure. An explicit declaration looks like: Dim MyNumber As Integer Now the variable MyNumber exists and a 2-byte space has been reserved for it. An implicit declaration: the variable is declared "on the fly", its data type is deduced from other variables. For example: Dim Total1 As Integer 'Explicit declaration
Total3 is not formally declared but is implied, it is "arrived at" from the other declarations. Dim Total2 As Integer 'Explicit declaration Total3 = Total1 + Total2 'Implicit declaration It is never a good idea to have implicit declarations. It goes against the rules for clarity, readability and ease of use of the code. To make sure that this rule is followed, start the Declarations with the Option Explicit clause. This tells the compiler to consider implicit declarations as errors and forces the programmer to declare everything explicitly. Other examples of declarations: Dim MyName As String Dim StudentDOB As Date Dim Amount5, Amount6, Amount7 In the last example the type assigned to each variable will be: Variant. It is the default type when none is specified. There can be multiple explicit declarations in a statement: Dim EmpName As String, SalaryMonth As Currency, SalaryYear As Currency In this final example, what are the types assigned to the three variables: Dim Amount1, Amount2, Amount3 As Single All Single-precision floating point, you say. Wrong! Only Amount3 is Single. Amount1 and Amount2 are considered Variant because VB specifies that each variable in a statement must be explicitly declared. Thus Amount1 and Amount2 take the default data type. This is different from what most other languages do. ConstantsA constant is a value that does not change during the execution of a procedure. The constant is defined with:Const ValuePi = 3.1416 The Scope of variablesThe term Scope refers to whether the variable is available outside the procedure in which it appears. The scope is procedure-level or module-level.A variable declared with Dim at the beginning of a procedure is only available in that procedure. When the procedure ends, the variable disappears. Consider the following example: Option Explicit Dim Total2 As Integer Private Sub Command1_Click () Dim Total1 As Integer Static Total3 As Integer Total1 = Total1 + 1 Total2 = Total2 + 1 Total3 = Total3 + 1 End Sub Private Sub Command2_Click () Dim Total1 As Integer Total1 = Total1 + 1 Total2 = Total2 + 1 Total3 = Total3 + 1 End Sub Every time Button1 is clicked, Total1 is declared as a new variable during the execution of that clicked event. It is a procedure-level variable. It will always stay at 1. The same for the Button2 event: Total1 is a new variable in that procedure. When the procedure ends, Total1 disappears. Total2 is declared in the Declarations section. It is a module-level variable, meaning it is available to every control in this Form. When Button1 is clicked, it increments by 1 and it retains that value. When Button2 is clicked, Total2 is incremented from its previous value, even if it came from the Button1 event. Total3 shows another way of retaining the value of a local variable. By declaring it with Static instead of Dim, the variable acts like a module-level variable, although it is declared in a procedure. Another scope indicator that you will see when you study examples of code is Private and Public. This determines whether a procedure is available only in this Form (module) or if it is available to any module in the application. For now, we will work only with Private procedures. OperatorsMathematical and Text operators
Note that the order of operators is determined by the usual rules in programming. When a statement includes multiple operations the order of operations is: Parentheses ( ), ^, *, /, \, Mod, +, - Logical operators
Control StructuresIf...Then...ElseIf condition1 Then Select CaseCan be used as an alternative to the If...Then...Else structure, especially when many comparisons are involved. Do...LoopUsed to execute a block of statements an unspecified number of times. Do While condition statements Loop First, the condition is tested; if condition is True, then the statements are executed. When it gets to the Loop it goes back to the Do and tests condition again. If condition is False on the first pass, the statements are never executed. For...Next
When the number of iterations of the loop is known, it is better to use the For...Next rather than the Do...Loop. For counter = start To end statements Next 1) The counter is set to the value of start. 2) Counter is checked to see if it is greater than end; if yes, control passes to the statement after the Next; if not the statements are executed. 3)At Next, counter is incremented and goes back to step 2). More will be covered on Control strucures as it becomes necessary in upcoming lessons. Meanwhile,if you want to know more, consult the VB Language Reference. For additional resources you might find this site useful: Free Visual Basic 6 tutorials and source code examples . Assignment
If you haven't found the Visual Basic resource you're looking for,
|