Page 11 - Open Soource Technologies 304.indd
P. 11
Modern Programming Tools & Techniques-III
Notes 1.1.2 Language Element
Microsoft Visual Basic code is written in units called procedures. A procedure contains a series
of Visual Basic statements that perform an operation or calculate a value. An event procedure is
a procedure that runs in response to an event initiated by the user or program code, or triggered
by the system. Its syntax is,
Private Sub button1_Click()
//code
End Sub
Self Assessment
Fill in the blanks:
1. The purpose of programming is to create a set of ........................ that computers use to
perform specific operations or to exhibit desired behaviors.
2. A ........................ contains a series of Visual Basic statements that perform an operation or
calculate a value.
1.2 Object-oriented Programming in VB.NET
Before moving to the language syntax, let’s formally define the key OO concepts and terms that
will be used in this unit beginning with encapsulation, polymorphism, and inheritance.
1.2.1 Encapsulation
Encapsulation means that an object can hide its internal data structures from consumers of the
object. Therefore, all of the object’s internal data is manipulated through members (methods,
properties, events, fields) of the object, rather than through direct references.
The primary benefits of encapsulation are maintainability and reusability. Code that takes
advantage of encapsulation is more maintainable because consumers of the code work with the
object through its public members. With a fully encapsulated object, for example, code outside
the object cannot directly change a variable declared inside the object. By shutting off this direct
access, fewer bugs are introduced because consumers of the object cannot inadvertently change
the state of an object at run-time.
Abstracting the internal data of the object from consumers also leads to greater reusability. This
follows because encapsulation leads to fewer dependencies between the consumer and the class
and fewer dependencies is a prerequisite for creating reusable software.
1.2.2 Polymorphism
The second characteristic of OO systems is polymorphism. This concept is defined as the ability
to write code that treats objects as if they were the same when in fact they are different. In other
words, polymorphism allows you to write code that is generic across a set of objects that
provide the same public members. Underneath the covers, each object might be implemented
differently. However, as far as the consumer is concerned, each object looks the same and can be
treated as such. In VB.NET, polymorphism can be created using both classes and interfaces.
The benefits of polymorphism revolve around the central fact that consumers of objects do not
have to be aware of how the object performs its work, only that it does so through a specific set
of members. This makes writing code that uses objects simpler by allowing the code to treat the
4 LOVELY PROFESSIONAL UNIVERSITY