Sie sind auf Seite 1von 20

-

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.

iTry Write True if the statement is correct, otherwise write False.

_______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.

2 Programming Volume 2 I iBook Publishing, Inc.


iDiscuss Introduction to Visual Basic 2010

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.

4.2 Visual Basic 2010 IDE


To simplify the process of application development, Visual Studio provides an
environment that is common to all languages, known as an Integrated Development
Environment. The purpose of the IDE is to enable the developer to do as much as possible
with visual tools before writing code.

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:

1. Toolbox – It is a palette of developer objects or controls


that are placed on forms or web pages. and Code is then
added to allow the user to interact with the control.

2. Solution Explorer – This section is used to view and modify


the contents of the project. A Visual Studio Windows
Application Project generally has a form object with a
code page, references to system components and
possibly other modules with special code that is used by
the application.

Unit 2 I Visual Basic 2010 Essentials 3


3. Properties Windows – The Properties Windows shows all the control properties to be
changed at design time. Most of these properties can also be changed with code at
run time.

4.3 Writing the Code


Controls are objects that can be inserted into the form of VB2010 IDE for various
purposes. A programmer can write relevant code for them to perform a certain task.

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.

4 Programming Volume 2 I iBook Publishing, Inc.


Example:
Public Class Form1
Private Sub Button1_Click (ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles Button1.Click

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.

Unit 2 I Visual Basic 2010 Essentials 5


LESSON 5
Object Oriented Programming

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.

A. Give the four (4) concepts of Object Oriented Programming

1.
2.
3.
4.

B. Give six (6) inheritance implemented in different combinations in object-oriented


programming

1.
2.
3.
4.
5.
6.

6 Programming Volume 2 I iBook Publishing, Inc.


iDiscuss Object Oriented Programming

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.

In OOP, data is treated as a critical element and is not


allowed to flow freely. It binds data closely to the functions
that operate on it and protects it from accidental modification
from outside functions. OOP allows decomposition of a
problem into a number of entities called objects and then
builds data and functions around these objects. The major
advantage of OOP is the code reusability.

Some features of Object Oriented programming are as follows:

• Emphasis on data rather than procedure


• Programs are divided into Objects
• Data is hidden and cannot be accessed by external functions
• Objects can communicate with each other through functions
• New data and functions can be easily added whenever necessary

5.2 Principles of Object-Oriented Programming


It is often said that there are four main concepts in the area of object-oriented
programming. Below is the core concept of OOP:

• 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

The idea of encapsulation is to contain or encapsulate the properties and methods of


an abstraction, and expose only those portions that are absolutely necessary. Each property

Unit 2 I Visual Basic 2010 Essentials 7


and method of an abstraction is called a member of the abstraction. The set of exposed
members of an abstraction is referred as the public interface (or just interface) of the
abstraction (or of the software component that encapsulates the abstraction).

Encapsulation serves three useful purposes:

• 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

In object-oriented programming, classes are created according to hierarchies.


Inheritance allows the structure and methods in one class to be passed down the hierarchy.

Inheritance is a fundamental feature of an Object-Oriented Programming. It is the


process of creating a new Class, called the Sub Class, from the existing class, called the Base
Class. Inheritance is a very elegant way of reusing and modifying the data and functionality
that has already been defined in the Base Class.

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.

However, inheritance may be implemented in different combinations in Object-


Oriented Programming languages such as:

• 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.

8 Programming Volume 2 I iBook Publishing, Inc.


Polymorphism sounds a great deal like encapsulation, but it is different. Encapsulation
has something to do with hiding the internal implementation of an object while polymorphism
deals with multiple classes having the same interface.

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

The advantage of abstraction is the capability to hide the implementation details,


component reuse, extensibility, and testability. If the implementation details are hidden, a
cleaner, more comprehensible and usable interface is exposed to the users.

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.

Unit 2 I Visual Basic 2010 Essentials 9


LESSON 6
Working with Classes

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.

_____________________1. It is an action recognized by an object, such as clicking the


mouse or pressing a key.
_____________________2. It is used to indicate that the property’s value can be accessed
but it cannot be changed.
_____________________3. It is a method of designing individual software components.
_____________________4. It describes the variables, properties, procedures and events of
an objects.
_____________________5. It is a combination of code and data that can be treated as a
unit.
_____________________6. It is something that an object does.
_____________________7. It allows the programmer to instantiate and initialize an object.
_____________________8. It is used to filter the access to the member of a class.
_____________________9. This keyword indicate that the property can be access by either
the member of its class or the classes derived from it.
_____________________10. It belongs to the whole class rather than to an individual object.

10 Programming Volume 2 I iBook Publishing, Inc.


iDiscuss Working with Classes

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.

6.2 Classes and Objects


An object is a combination of code and data that can be treated as a unit. It can be a
piece of application, like a control or form. An entire application can also be an object. A
programmer can create application using the objects provided by Visual Basic, such as
controls, forms and data access objects.

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.

To create an object from a class:

1. Determine the class where to create an object.


2. Write a Dim Statement to create a variable to which the user can assign a class
instances.

Dim nextCustomer As customer

3. Add the New Operator keyword to initialize the variable to a new instance of the class.

Dim nextCustomer As New customer

4. Programmer can now access the member of the class through the object variable.

nextCustomer.accountNumber = lastAccountNumber + 1

Unit 2 I Visual Basic 2010 Essentials 11


6.3 Methods
A method is something that an object does. For example, Add is one method of the
ComboBox object that adds a new entry to a combo box.

For example, below is a sample code on how to add items in ComboBox without using
Binding DataSource.

Private Sub Button1_Click (ByVal sender


As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click

ComboBox1.Items. Add (TextBox1.Text &


" " & TextBox2.Text)

End Sub

6.3.1 Events

An event is an action recognized by an object, such as clicking the mouse or pressing


a key. Events can occur as a result of a user action or program code, or they can be caused by
the system. Code that signals an event is said to raise the event while the code that responds
to it is said to handle it.

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:

✓ It always has the name “New”.


✓ Being a sub procedure, it does not return any value
✓ It is automatically called when a new instance or object of a class is created.

12 Programming Volume 2 I iBook Publishing, Inc.


Constructors can be quite useful because they allow the programmer to instantiate
an object and initialize some value in it. For example, if the programmer wants to initialize
the name of student then he can declare the property of the object just like this:

Public Sub New (Optional ByVal name As String = "John")

6.4.1 Overloading Constructors

Overloading feature is used most frequently to overload the constructor. The


programmer can overload the constructor by defining more than one Sub New (). The only
condition while overloading the constructor is that the constructor should have a signature
which is different from other constructors.

Do this:

Create a new form and add a button control. Click the button and insert the code
below:

6.5 Properties

A property is a member of a class that acts as an intermediary to a member variable


of the class. A property is used to “filter” access to a member of a class. To create a property,
use the Property keyword.

6.5.1 Read-Only Properties


A property is referred to us as read-only if it allows the programmer to make the value
it represent available. The ReadOnly keyword is used to indicate that the property’s value
can be accessed but it cannot be changed.

Unit 2 I Visual Basic 2010 Essentials 13


To create a read-only property, use the syntax as follows:

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:

Public Class Circle


Private rad As Double
Public Sub New ()
End Sub
'This is a new property
Public ReadOnly Property Radius ()
Get
End Get
End Property
End Class

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.

There are the rules in using a read-only property.

• Declaration Context – the declaration context of a ReadOnly element must be in


a class, structure, or module, and cannot be a source file, namespace, or
procedure.

• Combined Modifiers – user cannot specify ReadOnly together with Static in


the same declaration.

• 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.

14 Programming Volume 2 I iBook Publishing, Inc.


6.5.2 Shared Members

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.

There are rules to be follow when it comes to Shared Properties:

• Declaration Context – to use the Shared element, it must be in a class or structure


and cannot be a source file, namespace, or procedure.

• Combined Modifiers – users can’t specify Shared together with Overrides,


Overridable, NotOverridable, MustOverride or Static in the same
declaration.

• Accessing – a programmer can access a shared element by qualifying it with its


class or structure name, not with the variable name of a specific instance of its
class or structure.

• 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.

Unit 2 I Visual Basic 2010 Essentials 15


UNIT SUMMARY
Visual Basic evolved from event driven to a fully object-oriented VB.NET versions. It
started with Visual Basic through VB 2005 down to the latest version which is VB 2017.

VB 2010 is a full-fledge Object-Oriented Programming Language launched by


Microsoft. It has the same syntax and interface with VB 2008.

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.

Object-Oriented Programming allows the decomposition of a problem into a number


of entities called objects, and then build data and functions around these objects. The major
advantage of OOP is code reusability.
OOP Language has features like polymorphism, inheritance, abstraction and
encapsulation. Each of these concept plays a significant role in VB.NET programming at one
level or another.

Encapsulation and abstraction concepts provide motivation for object-oriented


programming. On the other hand, inheritance and polymorphism concepts are directly
implemented in VB.NET programming.

In object-oriented programming, classes are created according to hierarchies.


Inheritance is a fundamental feature of an OOP.

Polymorphism allows the programmer to create more than one function with the
same function name but with different argument list.

Object-Oriented Programming Language allows the programmer to design


individual software components with associated behavior and data limitation 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, it can be a
piece of application, like control or form, while class describes the variables, properties,
procedures and events of an object.

Method is something that an object does, while an event is an action recognized by


an object, such as clicking the mouse or pressing a key.

16 Programming Volume 2 I iBook Publishing, Inc.


To instantiate or initialize an object, the programmer must use a constructor to
initialize the object properties being declared.

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.

A member of a class that acts as an intermediary to a member variable of the class is


called property. It is used to filter the access to a member of a class.

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.

Unit 2 I Visual Basic 2010 Essentials 17


iProject Read the instructions carefully to achieve the desired output.

1. This is an individual project.

2. Using the Microsoft Visual Studio 2010, create a class to simulate a Circle Shape.

3. Each circle has its radius and center point.

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.

9. See sample design below.

18 Programming Volume 2 I iBook Publishing, Inc.


WEB RESOURCES
• https://docs.microsoft.com
• https://www.codeproject.com
• http://www.dotnetheaven.com
• https://www.safaribooksonline.com
• http://www.c-sharpcorner.com
• http://www.itcsolutions.eu
• http://www.thecodekey.com
• http://www.vbtutor.net
• http://www.sci.sjp.ac.lk/
• https://msdn.microsoft.com
• https://www.vkinfotek.com

BOOK RESOURCES
• Visual Basic Programming Essentials, 2nd edition, iBook Development Group,
Copyright 2015 by iBook Publishing

Unit 2 I Visual Basic 2010 Essentials 19


Notes

20 Programming Volume 2 I iBook Publishing, Inc.

Das könnte Ihnen auch gefallen