Visual Programming
Visual Programming
Visual Programming
TOOLS
• VB stands for Visual Basic, and is a High-Level Programming
Language.
• A programming language basically allows you to create programs or
applications, such as Microsoft Word.
• Visual Basic one of the most popular programming languages
around. it is also one of the easiest, and is ideal for beginners.
VISUAL BASIC (VB) DATA TYPES,
MODULES & OPERATORS
• Visual Basic uses building blocks such as Variables, Data Types,
Procedures, and Control Structures in its programming environment.
MODULES:
Code in Visual Basic is stored in the form of modules.
The three kind of modules are Form Modules, Standard Modules and Class
Modules.
Form Modules: A simple application may contain a single Form, and the code
resides in that Form module.
standard Module: As the application grows, additional Forms are added and
there may be a common code to be executed in several Forms. To avoid the
duplication of code, a separate module containing a procedure is created that
implements the common code and it called standard module.
Class module: are the foundation of the object oriented programming in Visual
Basic. New objects can be created by writing code in class modules.
VISUAL BASIC (VB) DATA TYPES,
MODULES & OPERATORS(cont…)
Each module can contain:
• Declarations: May include constant, type, variable and DLL procedure
declarations.
• Procedures: A sub function, or property procedure that contain pieces of
code that can be executed as a unit.
3. Date: Use to store date and time values. A variable declared as date
type can store both date and time values.
DATA TYPES IN VISUAL BASIC(Cont…)
4. Boolean: Boolean data types hold either a true or false value. These
are not stored as numeric values and cannot be used as such. Values are
internally stored as -1 (True) and 0 (False) and any non-zero value is
considered as true.
5. Variant: Stores any type of data and is the default Visual Basic data
type. In Visual Basic if we declare a variable without any data type by
default the data type is assigned as default.
VARIABLES
• VARIABLES: Variables are the memory locations which are used to
store values temporarily. A defined naming strategy has to be
followed while naming a variable. It must be unique within the same
scope. It should not contain any special character like %, &, !, #, @ or
$.
LOCAL VARIABLES:
• A local variable is one that is declared inside a procedure.
• This variable is only available to the code inside the procedure and can be
declared using the Dim statements.
• Variables that are declared with keyword Dim exist only as long as the
procedure is being executed.
• Example: Dim sum As Integer
SCOPE OF VARIABLES(cont…)
STATIC VARIABLES:
• Static variables are not reinitialized each time Visual Invokes a procedure
and therefore retains or preserves value even when a procedure ends.
• These static variables are also ideal for making controls alternately visible or
invisible.
• The values in a module-level and public variables are preserved for the
lifetime of an application.
• The value of a local variable can be preserved using the Static keyword.
Example: Static intPermanent As Integer
Function RunningTotal ( )
Static Accumulate
Accumulate = Accumulate + num
RunningTotal = Accumulate
End Function
SCOPE OF VARIABLES(cont…)
- Subtract 10-5 5
/ Divide 25/5 5
\ Integer Division 20\3 6
* Multiply 5*4 20
^ Exponent (power of) 3^3 27
Mod Remainder of division 20 Mod 6 2
& String concatenation "George"&" "&"Bush" "George Bush"
OPERATORS IN VISUAL BASIC(cont…)
2. COMPARISON/CONDITIONAL/RELATIONAL OPERATORS
• Comparison operators compare two expressions and return a Boolean value
that represents the relationship of their values.
• There are operators for comparing numeric values, operators for comparing
strings, and operators for comparing objects.
Operators Description
OR Operation will be true if either of the operands is true
AND Operation will be true only if both the operands are true
Xor One side or other must be true but not both
Not Negates true
DATA TYPE CONVERSION
• Visual Basic functions either to convert a string into an integer or vice
versa and many more conversion functions.
Conversion To Function Meaning
Boolean Cbool The function Cbool converts any data type to
Boolean 0 or 1.
Byte Cbyte The function Cbyte converts any data type to
Byte.
Currency Ccur The function Ccur converts any data type to
currency.
Date Cdate The function Cdate converts any data type to
date.
Decimals Cdec The function Cdec converts any data type to
decimal.
Double CDbl The function CDbl converts, integer, long integer,
and single- precision numbers to double-
precision numbers.
Value val The CVal function is used to convert string to
double-precision numbers
CONTROL STRUCTURES IN VISUAL
PROGRAMMING
• Control Statements are used to control the flow of program's execution.
• Visual Basic supports control structures such as if... Then, if...Then ...Else,
Select...Case.
• Loop structures such as Do While...Loop, While...Wend, For...Next etc
method.
• Decision making process is an important part of programming because it can
help to solve practical problems intelligently.
1. Do...Loop:
• The Do...Loop executes a block of statements for as long as a condition is
True.
• Visual Basic evaluates an expression, and if it’s True, the statements are
executed.
• The Do Loop statements have four different forms, as shown below:
b) The Do...Loop While : statement first executes the statements and then test
the condition after each execution.
Syntax :
Do
Block of one or more VB statements
Loop While condition
Example
Dim number As Long
number = 0
Do number = number + 1
Loop While number < 201
LOOPING STATEMENTS(cont…)
C) The Do Until... Loop : structure tests a condition for falsity. Statements in the body of
a Do Until...Loop are executed repeatedly as long as the loop-continuation test evaluates
to False.
Syntax:
Do Until condition
Block of one or more VB statements
Loop
Example:
Dim number As Long
number=0
Do Until number > 1000
number = number + 1
Print number
Loop
For Example:
2) For....Next Loop:
1)For I=0 To 10 step 5
syntax is:
Statements
For counter = Start To End Step [Increment]
Next I
One or more VB statements
2) For counter = 100 To 0 Step -5
Next [counter]
Statements
Next counter
LOOPING STATEMENTS(cont…)
3. EXISTING LOOP:
• The exit statement allows you to exit directly from For Loop and Do Loop.
• Exit For can appear as many times as needed inside a For loop, and Exit Do can
appear as many times as needed inside a Do loop.
• Sometimes the user might want to get out from the loop before the whole repetitive
process is executed.
Function Description
X= RND Create random number value between 0 and 1
Y=ABS(X) Absolute of X, |X|
Y=SQR(X) Square root of X
Y=SGN(X) -(-1 or 0 or 1) for (X<0 or X=0 or X>0) Y=EXP(X) 𝒆𝒆𝑿𝑿
Y=LOG(X) Natural logarithms, ln𝑋𝑋 Y=LOG(X) / LOG(10) log𝑋𝑋
Y=sin (𝑋𝑋) Trigonometric functions
Y=cos (𝑋𝑋)
Y=tan (𝑋𝑋)
Y=INT(X) Integer of X Y= FIX(X) Take the integer part
Y=ATN(X) Is arc= tan−1(𝑋𝑋) (Where X angle in radian).
VISUAL BASIC FUNCTIONS:
Function of String Variable
Function Description
Y=Len(x) Number of characters of Variable
Y=UCase (x) Change to capital letters
Y=LCase (x) Change to small letters
Y=Left (X,L) Take L character from left
Y=Right (X,L) Take L character from right
Y=Mid (X,S,L) Take only characters between S and R
VISUAL BASIC FUNCTIONS(cont…)
• Visual Basic offers different types of procedures to execute small sections of coding
in applications.
• Visual Basic programs can be broken into smaller logical components called
Procedures.
• Procedures are useful for condensing repeated operations such as the frequently used
calculations, text and control manipulation.
Sub Procedures:
• A sub procedure can be placed in standard, class and form modules. Each time the
procedure is called, the statements between Sub and End Sub are executed.
General Procedures:
• A general procedure is declared when several event procedures perform the
same actions.
• It is a good programming practice to write common statements in a separate
procedure (general procedure) and then call them in the event procedure.
VISUAL BASIC FUNCTIONS(cont…)
. In order to add General procedure:
• The Code window is opened for the module to which the procedure is to be
added.
• The Add Procedure option is chosen from the Tools menu, which opens an
Add Procedure dialog box as shown in the figure given below.
• The name of the procedure is typed in the Name textbox.
• Under Type, Sub is selected to create a Sub procedure, Function to create a
Function procedure or Property to create a Property procedure.
• Under Scope, Public is selected to create a procedure that can be invoked
outside the module, or Private to create a procedure that can be invoked only
from within the module.
VISUAL BASIC FUNCTIONS(cont…)
. Note: We can also create a new procedure in the current module by
typing Sub ProcedureName, Function ProcedureName, or Property
ProcedureName in the Code window. A Function procedure returns a
value and a Sub Procedure does not return a value.
Function Procedures
• Functions are like sub procedures, except they return a value to the
calling procedure.
• They are especially useful for taking one or more pieces of data,
called arguments and performing some tasks with them.
• functions returns a value that indicates the results of the tasks
complete within the function.
VISUAL BASIC FUNCTIONS(cont…)
. Examples: function procedure calculates the third side or hypotenuse
of a right triangle, where A and B are the other two sides. It takes two
arguments A and B (of data type Double) and finally returns the results.
Example
Dim AvgMarks ( 50, 50)
Line-continuation character
Empty command
window
Title bar
Dialog sized to
accommodate
Close box
contents.