Event Driven and Structured Driven Difference
Event Driven and Structured Driven Difference
Event driven programming is just that, the code does not get executed until an event happens. A good
example is a visual basic program, when you click on a button, the code for the button gets executed,
but in any other case that code is never touched by the program.
Structured programming is different, A DOS programming environment called QBasic is a great
example of this. You write a program and it goes through every single line of code before either
looping back to the beginning or exiting. When it gets to the end, it checks to see if a variable contains
a certain value, if it does it exits, otherwise it loops.
Events are caused by actions the user performs. For example, clicking the mouse generates a mouse
event, pressing a key on the keyboard generates a keyboard event, and so on. When a system event
occurs, the operating system is responsible for identifying and responding to the event.
Different operating systems implement this process in various ways. However, all operating systems
respond to the system event by broadcasting a system event message. Any application running on the
system can react to the system event by executing code written for that specific system event.
In an event-driven program, the program first waits for events to occur, responds to those events, then
returns to waiting for the next event. How the program responds depends on the code written for that
specific event. The order in which an event-driven program executes depends on which events occur
and on the order in which those events occur. While the program waits for the next event, it frees up
CPU resources that might be used to perform other processing tasks.
Modal form
The Modal Forms takes the total control of the application where user cannot switch to any other
Forms in the application unless the Form is closed. A modal Form, thus, must have a Close button or
some means to close the Form in order to return to the Form where the Modal Form was loaded.
The FormName variable is the Form's name, and the optional argument mode determines whether the
Form will be Modal or not. It can have one of the following syntax :
* 0-Modeless (default)
* 1-Modal
Form1.show 1
Form1.show vbModal
Option explicit
Use the option explicit statement to avoid common coding errors.vb doesn’t require to declare a
variable before using it.when a variable is first used used in code it will automatically become type
variant.however good programming practice requires the declaration of all variables.the specification
of a data type will save memory space.
When an array is declared by default starting index number is 0. By using this statement the
programmer can change the starting index number. Well but according to coding conventions this
concept should not be used.
option base is used in case of arrays ;to change starting index of array either to 0 or 1 only;
eg
in general declarations do as:
option base 1
or
option base 0
Option Compare
Declares the default comparison method to use when comparing string data.
Binary Optional. Results in string comparisons based on a sort order derived from the internal binary representations of
the characters.
Text Optional. Results in string comparisons based on a case-insensitive text sort order determined by your system's locale.
Option strict
Restricts implicit data type conversions to only widening conversions. Option Strict On provides strong typing, prevents
unintended type conversions with data loss, disallows late binding, and improves performance, its use is strongly
recommended.
Properties of combo box
Enabled By setting this property to True or False user can decide whether user can interact
with this control or not
Specifies the Control array index
String array. Contains the strings displayed in the drop-down list. Starting array
index is 0.
ListCount Integer. Contains the number of drop-down list items
ListIndex Integer. Contains the index of the selected ComboBox item. If an item is not
selected, ListIndex is -1
Locked Boolean. Specifies whether user can type or not in the ComboBox
MousePointer Integer. Specifies the shape of the mouse pointer when over the area of the
ComboBox
NewIndex Integer. Index of the last item added to the ComboBox. If the ComboBox does not
contain any items , NewIndex is -1
Sorted Boolean. Specifies whether the ComboBox's items are sorted or not.
Integer. Specifies the style of the ComboBox's appearance
TabStop Boolean. Specifies whether ComboBox receives the focus or not.
String. Specifies the selected item in the ComboBox
ToolTipIndex String. Specifies what text is displayed as the ComboBox's tool tip
Visible Boolean. Specifies whether ComboBox is visible or not at run time
Control Array
A control array is a group of controls that share the same name type and the same event procedures. Adding controls with
control arrays uses fewer resources than adding multiple control of same type at design time.
A control array can be created only at design time, and at the very minimum at least one control must belong to it. You
create a control array following one of these three methods:
You create a control and then assign a numeric, non-negative value to its Index property; you have thus
created a control array with just one element.
You create two controls of the same class and assign them an identical Name property. Visual Basic
shows a dialog box warning you that there's already a control with that name and asks whether you want to create a
control array. Click on the Yes button.
You select a control on the form, press Ctrl+C to copy it to the clipboard, and then press Ctrl+V to paste a
new instance of the control, which has the same Name property as the original one. Visual Basic shows the warning
mentioned in the previous bullet.
Control arrays are one of the most interesting features of the Visual Basic environment, and they add a lot of flexibility to
your programs:
Controls that belong to the same control array share the same set of event procedures; this often
dramatically reduces the amount of code you have to write to respond to a user's actions.
Creating Controls at Run Time
Where object is the name of the control to add or delete from the control array. Index % is the value of the index in the
array. The control array to be added must be an element of the existing array created at design time with an index value of
0. When a new element of a control array is loaded, most of the property settings are copied from the lowest existing
element in the array.
Explicit Declaration
Declaring a variable tells Visual Basic to reserve space in memory. It is not must that a variable should be declared before
using it. Automatically whenever Visual Basic encounters a new variable, it assigns the default variable type and value.
This is called implicit declaration. Though this type of declaration is easier for the user, to have more control over the
variables, it is advisable to declare them explicitly. The variables are declared with a Dim statement to name the variable
and its type. The As type clause in the Dim statement allows to define the data type or object type of the variable. This is
called explicit declaration.
Syntax
For example,
A Type statement is used to define a user-defined type in the General declaration section of a form or module. User-
defined data types can only be private in form while in standard modules can be public or private. An example for a user
defined data type to hold the product details is as given below.
The user defined data type can be declared with a variable using the Dim statement as in any other variable declaration
statement. An array of these user-defined data types can also be declared. An example to consolidate these two features
is given below.
Text1.Text = ElectronicGoods(i).ProdName
User-defined data types can also be passed to procedures to allow many related items as one argument.
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. The syntax for a sub procedure is as follows:
arglist is a list of argument names separated by commas. Each argument acts like a variable in the procedure. There are
two types of Sub Procedures namely general procedures and event procedures.
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. Then the functions returns a value
that indicates the results of the tasks complete within the function.The following 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.
The above function procedure is written in the general declarations section of the Code window. A function can also be
written by selecting the Add Procedure dialog box from the Tools menu and by choosing the required scope and type.
Property Procedure
A property procedure is used to create and manipulate custom properties. It is used to create read only properties
for Forms, Standard modules and Class modules. Visual Basic provides three kind of property procedures-
Property Let procedure that sets the value of a property, Property Get procedure that returns the value of a
property, and Property Set procedure that sets the references to an object.
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. A simple application may contain a single Form, and the code resides in that Form module itself. 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.
This is a standard Module.Class module (.CLS filename extension) are the foundation of the object oriented programming
in Visual Basic. New objects can be created by writing code in class modules. Each module can contain:
Access Modifier
Meaning in Life
Private Member can only be accessed by other class members. This is the default visibility for members.
Protected Member can be accessed by other class members and members of derived classes.
Friend Member can be accessed from any type within the assembly but not from outside the assembly.
The third keyword is Friend, and its use and meaning are not well understood. You use Friend when you
want a property or method to be accessible to other objects in the project but not to the project as a
whole. Or, within a component, objects can access each other's Friend members but the program that is
using the component cannot.
Suppose you are writing a component as a DLL or ActiveX, and there are several objects in the component
that need to communicate with each other. However you do not want this communication channel to be
available to the program that is calling the component. This is perhaps the most common use of Friend
properties and methods.
Friend properties and methods are not part of a class's interface. They do not appear in the type library of
the class and they are not included when you implement an interface using the Implements keyword.
Timer control
A Timer control is invisible at run time, and its purpose is to send a periodic pulse to the current application. You can trap
this pulse by writing code in the Timer's Timer event procedure and take advantage of it to execute a task in the
background or to monitor a user's actions. This control exposes only two meaningful properties: Interval and Enabled.
Interval stands for the number of milliseconds between subsequent pulses (Timer events), while Enabled lets you activate
or deactivate events. When you place the Timer control on a form, its Interval is 0, which means no events. Therefore,
remember to set this property to a suitable value in the Properties window or in the Form_Load event procedure:
COM
COM in particular stands for Component Object Model. Under this model, the VB application references
one/many components (Compiled in the form of DLL) and uses it to access its functions.
The benefit of using this model is that one single component may be reused by different applications. All the
application have to do is to instantiate an object of this COM Component in its memory.
The ideal example in this case would be a Clipart which is used among all office applications, whether its word,
excel or PowerPoint. So you may say the clipart is a different module which is packed as a different Component
and any program may call it to show clips stored in the common repository.
COM is collection of tools which are executed on client side environment whereas DCOM is a Distributed
component object Model runs at the given server.
Step Into
Executes code one statement at a time.
When not in design mode, Step Into enters break mode at the current line of execution. If the statement is a call
to a procedure, the next statement displayed is the first statement in the procedure.
Step Over
Similar to Step Into. The difference in use occurs when the current statement contains a call to a procedure.
Step Over executes the procedure as a unit, and then steps to the next statement in the current procedure.
Therefore, the next statement displayed is the next statement in the current procedure regardless of whether
the current statement is a call to another procedure. Available in break mode only.
Step Out
Executes the remaining lines of a function in which the current execution point lies. The next statement displayed is
the statement following the procedure call. All of the code is executed between the current and the final execution
points. Available in break mode only.
Holds 32-bit (4-byte) addresses that refer to objects. You can assign any reference type (string, array, class, or interface) to
an Object variable. An Object variable can also refer to data of any value type (numeric, Boolean, Char, Date, structure, or
enumeration).