Visual Basic .NET 2010 Express - The .NET Framework and Objects
< Continued from page 3
Traditionally (people sometimes disagree) a language had to have three features to be considered fully object oriented.
VB6 was often considered to fall short because it didn't support inheritance. But VB.NET is fully OOP.
Let's take a very brief look at each of these.
Inheritance is the ability of to define a new object based on an existing object. The new object will share some or all of the first one's methods and properties.
If we wanted to create another Hello World object that could also translate into Chinese, we could use the code already there in the first one and then simply add Chinese.
Polymorphism is the ability of an object to do things differently when different types of data are passed to it. The Hello World object displays a message in a window when the language string of text is passed to it. What if an audio file where someone was saying the name of the language was passed? It might be very useful to have the object create an audio output with someone speaking "Hello World" in that language.
Encapsulation means that everything the object needs is already in it and the only information you need to use the object is a clearly defined interface. This kind of an object is traditionally called a "black box". The idea is "don't tell me how you do it; just do it." Because we wrote the program, we know how HelloModule.vb works. But suppose we didn't know. Suppose HelloModule.vb was a "black box and all we knew about it was that it required a language input and displayed the message.
Would it work just as well? The answer is "yes". But now, suppose we discovered an even better way to do it. We could match the interface exactly and just start using the better version.
Hello World as an Object
In this part, we're going to code the Hello World module as an object. In part 3, it was programmed so it was not an object to make the difference as clear as possible.
Rather than starting over, let's copy the solution we developed in part 3 into a new folder and change it into an object. One way to do this is to simply start with the downloaded zip file for part 3 and unzip it into a new folder. If you need the part 3 solution again, you can download it by clicking here: Click here to download the complete code.. After unzipping the project to it's own folder, open the solution (HelloWorld.sln).
If you follow these instructions, you may end up with a program that isn't exactly the same as the one that you can download later. That's OK. It's more important that you do the code yourself than it is to match mine exactly. Don't be confused by all of the files and folders that are created automatically by VB.NET Express. You're not expected to know what they all are. (Most programmers probably don't know what all of them are.)
Our solution so far consists of a module HelloModule.vb and a form HelloWorld.vb (and a lot of other files and folders that VB.NET Express creates).
HelloModule.vb isn't very "OOP" because it doesn't have any properties or methods; it doesn't encapsulate anything; it can't be inherited and so forth. The way a module is being used here is just as another place to put some code. If you simply included the same code in the form, it would work the same way.
The Class - The Code Structure for Objects
To change this, we're going to add a Visual Basic .NET Class. In Visual Basic .NET - and most other OOP languages - the Class is the programming structure used for creating objects. As part of the change into an object, we'll update the program so that this Class provides the translated message to the Form as a property. The current selected language will also be a property in the Class.
The code is on the next page!
Traditionally (people sometimes disagree) a language had to have three features to be considered fully object oriented.
- Inheritance
- Polymorphism
- Encapsulation
VB6 was often considered to fall short because it didn't support inheritance. But VB.NET is fully OOP.
Let's take a very brief look at each of these.
Inheritance is the ability of to define a new object based on an existing object. The new object will share some or all of the first one's methods and properties.
If we wanted to create another Hello World object that could also translate into Chinese, we could use the code already there in the first one and then simply add Chinese.
Polymorphism is the ability of an object to do things differently when different types of data are passed to it. The Hello World object displays a message in a window when the language string of text is passed to it. What if an audio file where someone was saying the name of the language was passed? It might be very useful to have the object create an audio output with someone speaking "Hello World" in that language.
Encapsulation means that everything the object needs is already in it and the only information you need to use the object is a clearly defined interface. This kind of an object is traditionally called a "black box". The idea is "don't tell me how you do it; just do it." Because we wrote the program, we know how HelloModule.vb works. But suppose we didn't know. Suppose HelloModule.vb was a "black box and all we knew about it was that it required a language input and displayed the message.
Would it work just as well? The answer is "yes". But now, suppose we discovered an even better way to do it. We could match the interface exactly and just start using the better version.
Hello World as an Object
In this part, we're going to code the Hello World module as an object. In part 3, it was programmed so it was not an object to make the difference as clear as possible.
Rather than starting over, let's copy the solution we developed in part 3 into a new folder and change it into an object. One way to do this is to simply start with the downloaded zip file for part 3 and unzip it into a new folder. If you need the part 3 solution again, you can download it by clicking here: Click here to download the complete code.. After unzipping the project to it's own folder, open the solution (HelloWorld.sln).
If you follow these instructions, you may end up with a program that isn't exactly the same as the one that you can download later. That's OK. It's more important that you do the code yourself than it is to match mine exactly. Don't be confused by all of the files and folders that are created automatically by VB.NET Express. You're not expected to know what they all are. (Most programmers probably don't know what all of them are.)
Our solution so far consists of a module HelloModule.vb and a form HelloWorld.vb (and a lot of other files and folders that VB.NET Express creates).
HelloModule.vb isn't very "OOP" because it doesn't have any properties or methods; it doesn't encapsulate anything; it can't be inherited and so forth. The way a module is being used here is just as another place to put some code. If you simply included the same code in the form, it would work the same way.
The Class - The Code Structure for Objects
To change this, we're going to add a Visual Basic .NET Class. In Visual Basic .NET - and most other OOP languages - the Class is the programming structure used for creating objects. As part of the change into an object, we'll update the program so that this Class provides the translated message to the Form as a property. The current selected language will also be a property in the Class.
The code is on the next page!
Source...