Documente Academic
Documente Profesional
Documente Cultură
- Descriptive Naming
Exercise using descriptive names in all controls and variables. This will avoid
confusion and makes programming hassle – free. This one of the rules in
Naming conventions especially for variables.
- Indentation
Indentation is used especially in control structures (conditional
statements). It makes the follow of logic easier for variables.
- Comments
Comments amend source code to make it more understandable. They serve
as inline documentation that helps to read, reuse, understand and maintain
existing code. The alternatives for commenting are self – documenting code
and separate code documents. In reality, few pieces of code are entirely
self – documenting. Separate code documents, are often incomplete. What
is more, they go out of date easily as the code gets modifies. On the
- Capitalization
In Visual Basic, always use capital letters in the beginning of keywords.
- Continuation Character
If you have a long statements, write them in 2 lines using the continuation
character, space underscore (_). When encountering an error in putting 1
statements in 2 lines without putting the continuation character, it is
because Visual Basic reads every line as individual statements.
Variables and constants can be use d or can be called upon at certain ranges
depending on their placement in the program. They
4. Global Scope
Variables are designed to hold different values, but not at the same time.
Variables are dynamic. You set them, you change them, you display them and you
unload them.
All variables must be named in the code. Two different variables cannot have the
same name within one procedure. Unlike controls where VB gives them a default
name when they are placed on a form, variables do not have names until you
name them. Before a variable can be used, you must declare the variable by
telling VB the name and data type the variable is to hold. Variables can only hold
data from their declared type. The only exception is the variant data type.
1. Local variables
A local variable only exist while the function it is local to (the one you
declared it in) is executing. It vanishes when the function returns, and
reappears (with some different value) when the function executes later. If
you call the function recursively, each call of the function has its own value
for the local variable. You may also declare a variable to be local to a block,
in which case it exist only when code inside the block is executing. A local
variable so declared only has meaning inside the function or block it is local
to. This can be done using Private Statement in which variable can be only
be used in form where it is declared.
2. Global Variables
A global variable exists independently of any function. Any function may use
it. It functions declared in different source files use the same global variable,
the variable must be declared in both source files (or in files # included by
both files) before its first use. If the two files have different initializations for
the variable, only the first initialization has an effect.
If a global variable has the same name as a global variable, the local masks the
global variable. All references in the block to a variable of that name, from the
local variable’s definition until the end of the block it is defined in, are to the
Normally a variable in scope or visible for references, throughout the region in which
it was declared. In some cases, the variable’s access level can influences its scope.
After declaring various variables using the Dim statements, we can assign values to
those variables. The general format of an assignment is;
Variable = Expression
The variable can be a declared variable or a control property value. The expression
could be a mathematical expression, a number, a string, a Boolean value (true or
false) etc. The following are some examples:
In Visual basic, one needs to declare the variables before using them by assigning
names and data types. They are normally declared in the general section of the
code’s windows using the Dim statement
If the data type is not specified, VBB will automatically declare the variable as a
Variant. For string declaration, there are two possible formats, one for the variable
length string and another for the fixed – length string.
Constant are named storage locations in memory, the value of which that does not
change during program Execution. The Const statement is used to create a constant.
Constant can be declared in local, form, module or global scope and can be public or
private as for variables.
The expression on the right side of the equal sign (=) is often a number of literal
string, but it also can be an expression that results in a number or string (although
that expression cannot contain calls to functions). You can even define constant in
terms of previously defined constants:
1. Intrinsic Constant
The following table lists and describes the intrinsic enumerations available in
Visual Basic.
You declare a constant with the Const statement, using the same guidelines
you would for creating a variable name. If option Strict is On, you must
explicitly declare the constant type.
The different operators were already tackled in the previous unit of this book.
There are 3 types of mathematical operators namely: mathematical and text
operators, relational and logical
Arithmetic operators involve the common math problems that compute from
single to complex: These include addition (+)r, subtraction (-), multiplication (*),
division (/) and the modulus division (%).
Logical operators solve Boolean math and usually found in compound conditions.
Thought there are 8, the commonly used are AND (&&), OR (II) and NOT (!)
In order to compute inputs from users and to generate results, there is a need to
use various mathematical operators. In Visual Basic, except for + and -, the
symbols for the operators are different from normal mathematical operators.
In this example, three variables are declared as integer and two variables are
declared as variant. Variant means the variable can hold any data type. The
program computes the total and average of the three numbers that are entered
into three text boxes.
Functions
Functions are similar to normal procedures but the main purpose of the functions
is to accept certain inputs and pass them on to the main program to finish the
String Functions
Please take not that Strings are not objects so they do not have methods but
there is a number of functions that manipulate strings
The Date function reads the system clock and returns the current date as a string
in the format: MM/DD/YY (that is, the string returned contains two digit numerals
each for month, day and year, separated by slash marks).
Number Functions
Unlike the date function, number function only reads numbers that are
computable
Input/Output Functions
Input/Output Functions allow the system to handle input from the user and
output that is resulted from the code.
InputBox ( ) Function
Syntax:
MsgBox ( ) Function
Sky Values
The variable holds values that are returned by the MsgBox ( ) function. The values
are determined by the type of buttons being clicked by the user. It has to be
declared as integer data type in the procedure or in the general declaration
section.
If…Then Structure
In Visual Basic, you can write an “If…Then” statement for handling True
conditions in two ways: the “single – line If…Then” statement and the “multi –
line If…Then statement”. Each uses the If..Then keywords to check a condition. If
the condition is True, the program runs the commands associated with the
If…Then statement. If the condition is False, the commands are skipped or ended.
If…Then selection structure performs an indicated action only when the condition
is true; otherwise the action is skipped.
Review Syntax:
If <condition> Then
Statement
End If
The conditional statement is an expression that returns a true of false result. The
conditional statement may be a Boolean value or a longer expression that
includes conditional and possibly one or more logical operators.
Private Sub andCompare_Click ()
If (txtSales > txtTotal) Then ‘sales is greater than total
txtSales.Backcolor = vbGreen
End If
End Sub
The user enters values into the text box and clicks the compare button.
According to the code above, if the scales exceed the total, then the background
color for the scales text box will turn green. In this example, the sales clearly
exceed the total and the backcolor turns green.
Review Syntax:
If <condition> Then
Statement
Else
Statements
End If
If executing a code for only the False portion of the statement, place code
statements between the Else and End if statements; placing any statements
between the If and Else statements are not required.
If x <= 1 then
Else
MsgBox “X is not greater than 1”
End If
Nested If Structure
If the test is needed for a condition that depends on whether another condition is
already True (such as “If statement” is one that’s enclosed within another “If
statement”. Nested If…Then…Else selection structures test for multiple cases by
placing If…Then…Else selection structures inside If…Then…Else structures. You can
use Nested If either of the methods as shown below:
Review Syntax:
If <condition1> Then
Statements
Elself <condition2> Then
Statements
Elself <condition3> Then
Statements
…
Else ‘Elself is also acceptable given another condition
End If
If <condition1> Then
Statements
Elself <condition2> Then
Statements
Elself <condition3> Then
Statements
Else
Statements
End If
End If
End If
Example 1:
Assume you have to find the grade using nested if and display in a text box.
In this window, the student has a high enough average for an exemption, but has
one too may absences
Review Syntax:
Case 100 To 75
txtGrade.Text = “A”
Case 74 To 65
txtGrade.Text = “B”
Case 64 To 55
txtGrade.Text = “C”
Case 54 To 45
txtGrade.Text = “S”
Case 44 To 0
txtGrade.Text = “F”
Case Else
MsgBox “Invalid average marks”
End Select
Do…While Structure
Do…While loop consists of block of code and a condition. First, the code within
the block is executed, and then the condition is evaluated. If the condition is true
the code within the block is executed again. In example, the statement repeats
until the condition becomes false. The following Do Loop counts from 1 to 100.
Review Syntax:
Do While condition
Statement (s)
Loop
OR
Do
Statements/s
Loop While
Condition
Example 1: Example 2:
A variable number is initialized to 1 and then the Do While Loop starts. First, the
condition is tested; if the 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.
Do…Until Structure
The Do…Until Loop is a loop that constructs similar to the while loop, but runts at
least once. The syntax of the do…until loop is exactly what you would expect from
its name. note that Unreal Script doesn’t “do..while”, that means it loops until the
Review Syntax:
Do Until condition
Statement (s)
Loop
OR
Do
Statements
Loop Until
condition
Example:
Numbers between 1 to 1000 will be displayed on the form as soon as you click on
the command button.
For…Next Structure
The For…Next loop is another way to make loops in Visual basic. For…Next
repetition structure handles all the details of counter – controlled repetition. The
following loop counts the numbers from 1 to 100:
Review Syntax:
Example 1:
Dim x As Integer
For x = 1 To 50
Print x
Next
In order to count the numbers from 1 to 50 steps of 2, the following loop can be
used.
For x = 1 To 50 Step 2
Print x
Next
The following loop counts numbers as 1, 3, 7…,etc. The above coding will display
numbers vertically on the form. In order to display numbers horizontally the
following method can be used:
For x = 1 To 50
Print x & Space $ (2);
Next
To increase the space between the numbers increase the value inside the
brackets after the & space $
Example 2:
Following example is a For…Next repetition structure which is with the If
condition used.
For number = 1 To 10
If number = 4 Then
Print “This is number 4”
Else
Print number
End If
Next
In the output instead of number 4 you will get the “This is number 4”.
In an effort to make the codes sleek and elegant, conditions are often combined
into a single statement with the help of a logical operator, as in:
If X > Y And Y
>Z Then
‘Do Something
End If
One has to keep in mind, though, that this merging of conditions may not always
be appropriate. In fact, in some cases, it may even result in an error.