Introduction to Visual Basic


After studying this unit, you will be able to:

 Discuss visual basic elements

 Explain object-oriented programming in VB.NET

 Understand visual framework

 Discuss common type system

 Elaborate VB.NET features

 Explain .NET framework

 Understand VB.NET as a language in .NET framework


Computer programming, often shortened to programming, scripting, or coding is the process

of designing, writing, testing, debugging, and maintaining the source code of computer programs.

This source code is written in one or more programming languages such as C++, C#, Java,

Python, Smalltalk, etc. The purpose of programming is to create a set of instructions that

computers use to perform specific operations or to exhibit desired behaviors. The process of

writing source code often requires expertise in many different subjects, including knowledge of

the application domain, specialized algorithms and formal logic. Within software engineering,

programming is regarded as one phase in a software development process.

Visual basic is a high level programming language developed from the earlier DOS version

called BASIC. Visual Basic .NET is the latest technology introduced by Microsoft with tons of

new features including the .NET framework. Educational institutes, Universities and Software

Development companies have migrated to VB.NET now but Visual Basic 6 is still widely learned

and taught. Learning Visual Basic 6 is quite easier than other programming languages such as

C++, C#, Java etc. This is because Visual Basic enables you to work in a graphical user interface

where you can just drag and drop controls that you want to work with where you have to write

bunches of code to create in C++ or C# or even in Java. If you are new to programming and want

to start it in the smoothest and easiest way, then you should start it with Visual Basic.

Sometime in the July 2000, Microsoft announced a whole new software development framework

for Windows called .NET in the Professional Developer Conference (PDC). Microsoft also released

PDC version of the software for the developers to test. After initial testing and feedback Beta 1

of .NET was announced. Beta 1 of the .NET itself got lot of attention from the developer

community. When Microsoft announced Beta 2, it incorporated many changes suggested by the

community and internals into the software. The overall ‘Beta’ phase lasted for more than 1 ½

years. Finally, in March 2002 Microsoft released final version of the .NET framework. One thing

to be noted here is the change in approach of Microsoft while releasing this new platform.

Unlike other software where generally only a handful people are involved in beta testing, .NET Notes

was thrown open to community for testing in its every pre-release version. This is one of the

reasons why it created so many waves of excitement within the community and industry as

well. Microsoft has put in great efforts in this new platform. In fact Microsoft says that its future

depends on success of .NET. The development of .NET is such an important event that Microsoft

considers it equivalent to transition from DOS to Windows. All the future development –

including new and version upgrades of existing products – will revolve around .NET. So, if you

want to be at the forefront of Microsoft Technologies, you should be knowing .NET.

1.1 Elements of Visual Basic

The two basic elements of Visual Basic are the GUI and the code associated with the application

that makes it respond to events occurred as a result of a user action.

1.1.1 Visual Element

IDE is a term commonly used in the programming world to describe the interface and

environment that we use to create our applications. It is called integrated because we can access

virtually all of the development tools that we need from one screen called an interface. The IDE

is also commonly referred to as the design environment, or the program.

The Visual Basic IDE is made up of a number of components:

 Menu Bar

 Tool Bar

 Project Explorer

 Properties Window

 Form Layout Window


 Form Designer

 Object Browser

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()


End Sub

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 object as if it were a black box, which leads to increased maintainability. Along the same lines, Notes

polymorphism allows you to write less code because each individual object does not have to be

dealt with separately. Finally, polymorphism lends itself to writing code that can be reused

because it will not be specific to a particular object.

1.2.3 Inheritance

The final OO concept is inheritance. Inheritance allows objects to share their interfaces (the

definition of their members) and/or implementation in a hierarchy. For example, Tyrannosaurus

and Velociraptor objects might be derived or inherited from a more generic Theropod object.

All three objects share a basic set of members and, possibly, behaviors, such as carnivorousness,

although the descendant objects might also include additional members or override members

of Theropod. Inheritance allows objects to become more specific further down the hierarchy by

adding additional members. In a nutshell, inheritance allows objects to reuse features (either

their definition or their code) of other objects to which they are naturally related. The primary

benefit of inheritance is, thus, reuse.

Obviously, inheritance and polymorphism are closely related, and, in fact, inheritance is what

makes polymorphism possible in OO designs. It is always the case that objects that are in an

inheritance relationship can be treated polymorphically. For example, if the Velociraptor object

is inherited from the Theropod object, any consumer that is designed to work with Theropod

objects will also work with Velociraptor objects.

VB.NET developers can benefit from inheritance in two ways: through interface inheritance and

implementation inheritance. Interface inheritance allows only the definition of the object to be

reused, whereas implementation inheritance allows the actual code written for the ancestor

object (and its ancestors all the way down the line) to be reused.