Visual Basic (part 2)


Arrays

Introduction of Array
An array is the collection of similar data types.The individual elements
of an array is identified by using an index.

Each index number in an array is allocated individual memory space.
Hence users declare arrays of larger size than required.

Array helps in creating smaller and simpler code in many situation.You
can setup loops that deal efficiently with any number of cases by using
the index number.

A loop is used to execute a group of statements repeatedly, based on a
condition.




Declaration Of Array
Syntax:
Dim arrayname(number) as Datatype

Example:
Dim total(10) as integer


Here total is name of array ,10 is no of elements in the array & integer is the data type.Number 10 included in the parenthesis is the upper limit of the array.

The above declaration creates an array with index numbers ranging from 0 to 9.

If you want to specify lower limit ,then the parenthesis should include both the lower and upper limit along with the 'To' keyword.

An example is shown below

Dim num(1 To 5) As integer
In above statement, an array of 5 element is declared but with the index numbers ranging from 1 to 5.

Mainly Visual Basic supports 2 types of Array:

Fixed-Size array:
The size of array always remains the same.

Dynamic array:
The size of array can be changed at run time.



Conditional statements and Loop structure

If...Then..Statement
The If...Then statement is used to evaluate whether a condition is True or False.
You can use If…Then block to execute one or more statements conditionally.

Syntax:
If <condition> Then
[statements]
Else
[else statements]
End If

Example:

if a>5 Then a=a+10
Here condition is that 'a' should be greater than 5 and if it is true then 'a' should be incremented by 10.
:-The condition is usually a comparison, but it can be any expression that evaluates to a numeric value.

[Statements] :- One or more statements separated by colons; executed if condition is True.

Note that:

1.A single line of If…Then does not use End If statement.

Syntax:-
If condition Then statement.
But if you use this in more than one line ,then syntax will be If…Then…End If.

2.If anything other than a comment appears after Then on the same line, the statement is treated as a single line If statement.

3.A block If statement must be the first statement on a line. The block If must end with an End If statement.

4.When a If condition is tested ,then If condition is True, the statements following Then are executed,otherwise not.


If...Then..Else Statement
It conditionally executes a group of statements depending upon value of the expression.
If...Then…Else block is used to define several blocks of statements ,in order to execute one block.

Syntax:
If then
[statements]
Else if Then
[else if statement] . . .
Else
[elsestatement]
End If

Example:
If a<10 class="textbold">then
a=a+10
elseif a>15 then
a=a-10
else
a=100
end if

Note that:
1.Both Else and ElseIf statements are optional. You can have any number of ElseIf statements as you want in an If block. But no ElseIf statement can appear after the Else.

2.Block If statements can be nested. Nested means a block contained within one another.

3.In fact If…Then is just a special case of If…then…Else.

4.If none of ElseIf statement is True ,then the statement following Else will execute.


Select Case Statement
It is just as the alternative of If…Then…Else statement.It executes one statement in several groups of statements.

A Select Case provides the same functionality as the If…then…Else statement but it makes the code more efficient and readable

A Select Case works with a single test expression.
Value of the statements will be dependent on test expression.

Syntax:

Select Case testexpression
[Case expression1
[statements-n]] . . .
[Case expression2
[statements-n]]
.
.
Case Else
Statement..n
End Select
Any numeric or string expression.
Statement executes if Testexpression matches any part of expressionlist.

Example:

The following example describes the use of the Select Case statement-

Dim num as integer
Dim a(10) as integer
'Calculations containing num
Note "'" Indicates the Comment Line
Select Case num

Case 1 a(1)=num
Case 2 a(2)=num
Case 3 a(3)=num
Case else a(1)=num
End Select

Num is declared as integer variable here and a(10) is declared as an array of ten elements.
Select Case checks values of num, according to that it executes the corresponding case statement.

Note that:

1.Value of the statement will be dependent on expression.

2.The Case Else clause is used to indicate the elsestatements to be executed if no match is found between the testexpression and an expressionlist

3.Select Case statements can be nested.

4.Each nested Select Case statement must have a matching End Select statement.


Do...Loop Structure
Do…loop is used to execute a block of statements for indefinite number of times. There are several variations of Do...Loop statement.
Each variation evaluates a numeric condition to determine whether to continue execution or not.

Syntax:

Do While
statements…
Loop

Note that:

1.When Visual Basic executes Do While loop, it first tests condition.

2.If condition is False (zero), it skips past all the statements.

3.If it's True (nonzero), Visual Basic executes the statements and then goes back to the Do While statement and tests the condition again.

Example:

Do While count<=5
count = count + 1
Loop

Here loop increments the value of count until it becomes equal or less than 5.
Another variation of Do...Loop statement executes the statement first and then tests condition after each execution. This variation guarantees at least one execution of statements.

Syntax:

Do
Loop While


For...Next Structure
For...Next loop is used when you know how many times you want to execute loop.But in case of Do While it is not the case.
Do While is used when you do not know how many times you want to execute that particular loop/Condition.

Syntax:

For counter = start To end [Step increment]
statements
Next [counter]
The arguments counter, start, end, and increment are all numeric.

In executing the For loop, Visual Basic:

1. Sets counter equal to start.

2. Tests to see if counter is greater than end. If so, Visual Basic exits the loop.

3. Executes the statements.

4. Increments counter by 1 or as it's specified.

5. Repeats steps 2 through 4.

Example:

Dim A(40) as Integer
For i = 0 To 40 step 1
A(i)=i
Next
Print A(9)

Here index values are stored in array a(40).Then it prints the value of a(9).


Procedure

Sub Procedures
A Sub procedure is a block of code that is executed in response to an event.
By breaking the code in a module into Sub procedures, it becomes much easier to find or modify the code in your application.

Syntax:

[Private|Public][Static]Sub procedurename (arguments)
statements
End Sub

Each time when procedure is called, the statements between Sub and End Sub are executed. Sub procedures can be placed in standard modules, class modules, and form modules.
Sub procedures are by default Public in all modules, that means they can be called from anywhere in application.

Note that:

Sub Procedures are the procedures that do not return a value.
The arguments for a procedure are like a variable declaration.

Event Procedures

When an object in Visual Basic recognizes that an event has occurred then it automatically invokes event procedure
That event procedure will use procedure name corresponding to that event.

Syntax:

Private Sub Form_eventname (arguments)
statements
End Sub
CmdSave_Click():-Here Click is an event and CmdSave_Click() is the procedure associated with it.

Syntax for a Control Event:

Private Sub Control_Eventname(arguments)
Statements Block
End sub

Syntax for a Form Event:

Private Sub Form_eventname(arguments)
Statements Block
End sub

Example:

Private Sub Form_Load().
This is the main event procedure for a form .When a form is loaded in application then this procedure is called first.

General Procedures

A general procedure tells the application how to perform a specific task. Once a general procedure is defined, it must be specifically invoked by the application.
General procedure remains idle until called upon to respond to events caused by the user or by system.

Why create general procedures?

One reason is that several different event procedures might need the same task repeatedly.
A good programming strategy is to put common statements in a separate procedure (a general procedure). An event procedure calls that general procedure.
This eliminates the need to duplicate code and also makes the application easier to maintain.

Example:

Suppose you display some message after each event code execution then you will use event procedure.

For adding procedure to your application follow the steps given below:

(1) Go to Tools menu and select Add Procedure form. Or, Use key combination Alt+T+P.

The window will be shown like this:


(2) Write name of procedure Display in the name Box and select type Sub according to figure given above.


(3) Write code for display() after Clicking OK in the previous Step.The code Window will display like this:


(4) Now call procedure in any event associated your application.


( Calling Procedure display() )


Function Procedures
Function Procedures return values. you can use Function statement to write your own Function procedures.


Like a Sub procedure, a Function procedure is a separate procedure that can take arguments, perform a series of statements, and change the value of its arguments.

Syntax:

[Private|Public][Static]Function procedurename (arguments) [As type]
statements
End Function

Note that:

1.You call a function by including the function procedure name and arguments on the right side of a larger statement or expression (returnvalue = function()).

2.Function procedures have data types, just as variables. This determines the type of the return value.

3.You return a value by assigning it to the procedurename itself.

Example:

Public function Addition(a,b)
Addition=a+b
End Function

Here Function addition is declared .It takes two arguments and then add these two values. It returns sum of two values.


Property Procedures
Property procedures are the procedures that return values and also assign values to the property of objects.
Visual Basic provides three kinds of property procedures, as described in the following table.

Procedure Procedure
Property Get Returns the value of a property.
Property Let ets the value of a property.
Property Set Sets the value of an object property.

Property procedures are defined in class modules.

Note that:
If you need to perform a task each time then you need to use a property procedure. Once it is defined, it executes automatically without needing an explicit call each time.

No comments:

Post a Comment