Chapter 1(Part 2)Introduction to Visual Basic

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

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

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

  • Visual Basic .NET

Visual Basic .NET is an Object-oriented programming language designed by Microsoft. With the word “Basic” being in the name of the language, you can already see that this is a language for  beginners. There are people who criticize VB.NET because of the simplicity of the syntax, but VB.NET has the ability to create very powerful and sophisticated applications.

VB.NET is a great place to start because of how easy and straight forward it is. The syntax is easy and you will not find yourself writing hundreds of lines of code as there are many shortcuts that make coding so much easier in this language.

Let’s take a look at the VB.NET syntax. The purpose of typing code is to instruct the application  what to do. It’s not as easy as typing “Hey application, multiply 5 by 83 but it’s pretty darn close!

If you wanted to tell your application to show a Message Box telling you that is awesome, this would be the code you would use:

MessageBox.Show(“ is awesome”)

Microsoft Visual Basic .NET is a programming environment used to create Graphical User Interface (GUI) applications for the Microsoft Windows family of operating systems. It usually ships in two types, either by itself or as part of Microsoft Visual Studio .NET.

To use the lessons on this site, you must have installed either Microsoft Visual Basic .NET 2003 or Microsoft Visual Studio .NET 2003. After installing Microsoft Visual Studio .NET 2003, to use the programming environment, you must first open it. To do that, you would click Start -> (All) Programs -> Microsoft Visual Studio .NET 2003 -> Microsoft Visual Studio .NET 2003.

In the early days of Microsoft DOS, there was a language called Basic. It provided a simplified and easy way to create small applications using words very close to the English language. Since the language was easy, it became popular with the help of Microsoft operating systems gaining ground. To continue this tendency and provide more support for Basic, Microsoft used that language as the platform to create Graphical User Interface (GUI) applications.

Once again, this move was welcomed and the language became the widely accepted Visual Basic. The Microsoft Visual Basic programming environment became very popular for its ease of use and it was a candidate for serious productive applications.