Escolar Documentos
Profissional Documentos
Cultura Documentos
UNIT 2
Visual Basic 2010
+3
Essential
Unit Objectives
At the end of this Unit, the student shall be able to:
❖ Review Visual Basic 2010;
❖ Identify the different principles of object-oriented programming;
❖ Determine the different classes and objects;
❖ Understand how classes works;
❖ Understand Methods, Properties and Constructor;
❖ Create a program using the classes.
LESSON 4
Review of Visual Basic 2010
iObjectives iKeywords
• OOP - refers to a type of computer programming in which
• Review what visual basic is.
programmers define not only the data type of a data structure,
• Label the parts of the IDE but also the types of operations.
windows and describe each.
• programmer - a person who creates computer software.
• Review how to write code in
• console application – a computer program designed to be used
Visual Basic 2010.
via a text-only computer interface.
_______1. Visual Studio IDE consists of several sections or tools that the developer can use
while creating a program.
_______2. The Properties Windows shows all the control properties to be changed at
design time.
_______3. Visual Basic 2011 is the latest version of Visual Basic launched by Microsoft.
_______4. In writing a code, it must be entered between Private Sub…………End Sub.
_______5. OOP stands for Object-Oriented Program.
_______6. Server Explorer section is used to view and modify the contents of the project.
_______7. IDE stands for Integrated Development Environment.
_______8. ASP.NET, web sites, WPF applications, console applications and Windows
services are the new project choices in VB.NET.
_______9. Visual Basic 2005 is a full-fledge object-oriented programming language.
_______10. Visual Studio IDE has five sections in view.
4.1 Introduction
Visual Basic is always evolving, from earlier versions of
object-based event driven Visual Basic---which ended with the
popular Visual Basic 6.0---to a fully object oriented VB.Net
versions. VB.Net started with Visual Basic, which then evolved
from VB 2005 to VB 2008, to the VB 2010, VB 2012, VB 2013, VB
2015 and the latest, VB 2017.
Visual Basic 2010 is a version of Visual Basic launched by Microsoft. VB2010 is almost
similar to Visual Basic 2008, but it has many new features. If a user is familiar with Visual Basic
6, then they can easily learn VB 2010 easily since they have the same syntax and interface.
Like Visual Basic 2008, Visual Basic 2010 is also a full-fledge Object-Oriented
Programming (OOP) Language, so it caught up with other OOP languages such as C++, Java,
C#, etc.
VB.NET includes an upgraded tool that can assist in converting VB6 projects to .NET.
It also allows the developer to have a framework that is usable in many languages including
C#, C++, and Ruby, etc. VB.NET also provides a number of new project choices such as
ASP.NET, web sites, WPF applications, and console applications and Windows services.
The Visual Studio IDE consists of several sections or tools that the developer can use
while creating a program. The Visual Studio IDE It generally has three sections in View:
To write the code, double click the form or the control for the code editor to appear.
The code must be entered between the Private Sub…………End Sub. Press F5 or click the
Debug menu and start debugging the program.
Me.ForeColor = Color.Chocolate
Me.BackColor = Color.PaleVioletRed
End Sub
End Class
iSummary
Visual Basic 2010 is the latest version of Visual Basic launched by Microsoft 2010. It has
an upgraded tool to assist in converting VB6 projects to .NET. It also allows the user to have
a framework that is usable in many languages including C#, C++, and Ruby, etc. An Integrated
Development Environment (IDE) is a type of computer software that assists computer
programmers to develop software. Visual Studio IDE supports VB.NET for easy programming.
It consists of several sections or tools to use while programming such as Toolbox, Solution
Explorer and Properties window.
iActivity
1. Open the iBook Interactive application in your PC.
2. Click on the iWorksheet tab. Read the instructions carefully and answer the given
questions within the time limit.
3. Next, click on the iLab tab. Follow the step-by-step instructions to produce the
expected output.
iObjectives iKeywords
• Identify the concept of OOP • abstraction – a technique for arranging the complexity of
language. computer systems.
• Define what Encapsulation, • classes – is a template definition of the methods and variables
Inheritance, Polymorphism is. in a particular kind of object.
• Understand how the concept • object – the structure that contains data and methods of
of OOP works. handling data.
iTry List down the items being asked below. Write your answers on the blank.
1.
2.
3.
4.
1.
2.
3.
4.
5.
6.
5.1 Introduction
Visual Basic is Object-Based, VB.NET is Object-Oriented, which means that Visual
Basic.NET is an Object-Oriented Programming Language. Visual Basic .NET supports all the key
OOP features like Polymorphism, Inheritance, Abstraction and Encapsulation.
• Inheritance
• Encapsulation
• Polymorphism
• Abstraction
Each of these concept plays a significant role in VB.NET programming at one level or
another. Encapsulation and abstraction are abstract concepts providing motivation for
object-oriented programming. Inheritance and polymorphism are concepts that are directly
implemented in VB.NET programming.
5.2.1 Encapsulation
• It permits the protection of the properties and methods from outside tampering.
• It allows the inclusion of validation code to catch errors in the use of the public
interface. For instance, it prevents the client of the employee software component
from setting an employee’s salary to a negative number.
• It frees the user from knowing the details of how the properties and methods are
implemented.
Encapsulation therefore guarantees the integrity of the data contained in the Object.
5.2.2 Inheritance
The Inheritance has many advantages, the most important of them is the reusability
of the code. Rather than developing new Objects from scratch, new code can be based on
the work of other developers, adding only the new features that are needed. Reusing of
existing classes saves time and effort.
• Single Inheritance
• Multi-Level Inheritance
• Hierarchical Inheritance
• Hybrid Inheritance
• Multipath Inheritance
• Multiple Inheritance
5.2.3 Polymorphism
Polymorphism is one of the crucial features of VB.NET and one of the key concepts of
Object-Oriented Programming. It is the ability of programming language to process objects
differently depending on the data type or class differently. It is the process of using the same
thing for different purposes which also means Overloading.
With the help of polymorphism, a programmer can create more than one function
with same function name but with different argument list. The functions act differently based
on the argument list in the function call. The type and number of arguments in the functions
will determine which function is invoked.
5.2.4 Abstraction
An abstraction is a view of an entity that includes only those aspects that are relevant
for a particular situation. For instance, to create a software component that provides services
in keeping a company’s employee information, a programmer needs to have the following
entity:
• FullName
• Address
• EmployeeID
• Salary
• IncSalary
iSummary
Visual Basic is the Object-Based, Visual Basic.NET is Object-Oriented, which means
that Visual Basic.NET is an Object-Oriented Programming Language. OOP allows the
decomposition of a problem into a number of entities called objects and then builds data
and functions around these objects. There are four principles of object-oriented
programming: abstraction, encapsulation, polymorphism, inheritance.
iActivity
1. Open the iBook Interactive application in your PC.
2. Click on the iWorksheet tab. Read the instructions carefully and answer the given
questions within the time limit.
3. Next, click on the iLab tab. Follow the step-by-step instructions to produce the
expected output.
iObjectives iKeywords
• Define and describe what • Binding Data Source - a general technique that binds data
class and object are. sources from the provider and synchronizes them.
• Understand how classes • method – is a programming procedure that is defined as part
work. of a class and included in any object of that class.
• Create a class including its • overloading – is the ability to create multiple methods of the
methods, properties, and same name with different implementations.
members. • overrides – use one's authority to reject or cancel.
iTry Identify the words being described below. Write your answers on the blank.
6.1 Introduction
Object-Oriented Programming (OOP) is a
method of software design and construction. It is the
next logical progression, after structured programming
to improve the code reusability and maintainability. In
other words, OOP is a method of designing individual
software components (classes) with associated behavior
(methods) and data limitation (properties) that help to
put components together to create a complete
application.
Each object in Visual Basic is defined by a class. A class describes the variables,
properties, procedures, and events of an object. Objects are instances of classes. A
programmer can create many objects once they have defined the class.
To understand the relationship between an object and its class, think of cookie cutters
and cookies. The cookie cutter is the class. It defines the characteristics of each cookie, for
example, size and shape. The class is used to create objects. The objects are the cookies.
3. Add the New Operator keyword to initialize the variable to a new instance of the class.
4. Programmer can now access the member of the class through the object variable.
nextCustomer.accountNumber = lastAccountNumber + 1
For example, below is a sample code on how to add items in ComboBox without using
Binding DataSource.
End Sub
6.3.1 Events
For example, a Form object has a Click event that occurs when the user clicks the
mouse on the form.
6.4 Constructors
Constructors are a special kind of Sub procedure. A constructor has the following
properties:
Do this:
Create a new form and add a button control. Click the button and insert the code
below:
6.5 Properties
Public/Private/Protected] [ReadOnly][Overloads/Overrides]
[Overridable]/Shadows/Shared] Property PropName As PropType
Get
End Get
End Property
The optional Public, Private, or Protected keywords specify the level of access
to the property. As introduced in the previous lesson, the Public keyword indicate that the
property is available only to the member of its class. The Protected keyword indicate that
the property can be accessed by either the member of its class or the classes derived from it.
The optional Shared keyword permits to use the property without declaring an instance of
its class.
Here is example:
Notice that the As keyword is omitted as well as the data type of the property. If the
data type is not indicated, the property is treated as Objects.
• Assigning a Value – code consuming a ReadOnly property cannot set its value.
But code that has access to the underlying storage can assign or change the value
at any time.
The users can assign a value to a ReadOnly variable only in its declaration or in the
construction of a class or structure in which it is defined.
All the members of the class that has been discussed above are instance members.
This means they belong to the object being created. However, there is another class member
that can be used to access the class or structure. This is known as Shared members.
Shared members belong to the whole class rather than to an individual object. It can
be defined by using the keyword shared. It is also a way of creating a member (a property,
procedure, or field) that is shared among all instances of a class.
• Implicit Sharing – the users cannot use the Shared modifier in a Const Statement,
but constants are implicitly shared. Similarly, users cannot declare a member of a
module or an interface to be Shared, but they are implicitly shared.
iSummary
Object-Oriented Programming (OOP) is a method of designing components
(classes) with associated behavior (methods) and data limitations (properties) that help to
put components together to create a complete application. Object is a combination of code
and data that can be treated as a unit while class describes the variables, properties,
procedures, and events of an object. The action recognized by an object, such as clicking
the mouse or pressing a key is called events. In order for the programmer to instantiate or
initialize an object, they must learn how to use the constructor. A property is a member of
a class that acts as an intermediary to a member variable of a class. Properties can be a
Read-Only if it allows the programmer to make the value it represents available.
iActivity
1. Open the iBook Interactive application in your PC.
2. Click on the iWorksheet tab. Read the instructions carefully and answer the given
questions within the time limit.
3. Next, click on the iLab tab. Follow the step-by-step instructions to produce the
expected output.
Visual Studio IDE enables the developer to create a program with visual tools before
writing the code. It consists of several sections or tools use in creating program.
The most commonly used sections in visual Studio IDE are toolbox, Solution Explorer
and the properties windows.
When writing the code, double click the form or control for the code editor to
appear. Controls are object that can be inserted into the form for various purposes.
Debug the program to see if there is an error after writing the code. Press F5 or click
the Debug menu to start debugging the program.
Polymorphism allows the programmer to create more than one function with the
same function name but with different argument list.
Object is a combination of code and data that can be treated as a unit, it can be a
piece of application, like control or form, while class describes the variables, properties,
procedures and events of an object.
Overloading Constructors is used by defining more than one Sub New (). The only
condition while overloading the constructor is that the constructor it should have a
signature which is different from other constructors.
Read-Only properties allow the programmer to make the value available to all
members. It is used by indicating the ReadOnly keyword.
When creating a ReadOnly statement, the As keyword and the data type of the
property is omitted. If not, the property is treated as an Object.
There are several rules when creating a ReadOnly property. This includes the
declaration context, combined modifiers and the rules in assigning value.
The users can assign a value to a ReadOnly variable only in its declaration or in the
constructor of a class or structure in which it is defined.
The shared members belong to the whole class rather than to an individual object. It
can be defined by using the keyword shared.
2. Using the Microsoft Visual Studio 2010, create a class to simulate a Circle Shape.
4. Provide 2 constructors: a parameter less than one which takes a center point and a
radius as parameters.
5. Provide a mechanism to find the diameter (twice the radius) and the area (PI
*square of radius).
6. Apply the proper access modifiers and use proper fields, method and properties.
7. You may change also the appearance of the text on the font properties.
8. For the code, you may use some example from the lessons given.
BOOK RESOURCES
• Visual Basic Programming Essentials, 2nd edition, iBook Development Group,
Copyright 2015 by iBook Publishing