Object-Oriented Programming (or OOP) is a program design method that's come to the forefront of programming new in recent years. The basic idea is to think about programming from a somewhat different perspective.
Usually, when we think of a program, we think of it in terms of what it does. OOP tells you to look at a program in terms of what data it has. You can then model that data into objects, and your code should flow pretty easily from your objects.
So, what is an object? Basically, objects have three major properties:
An object's "state" refers to what it contains or stores at any one time. Think of an e-mail system. Each mailbox may be in an empty state, or a full state. This state also affects the behavior of the mailbox (if it's full, it won't accept new messages).
Each object has operations which change its state. A mailbox, for example, can add new messages or list its saved messages, though it can't diagram airplane schematics or calculate the cosine of Pi.
Two objects may have the same state and support the same operations, but not be the same object. Two mailboxes, for example, may have the same messages in them, but they still aren't the same mailbox. Each mailbox's identity is different.
Classes
OK, so now we have an idea about what an object is. Does Visual Basic have objects? Sure it does! A variable is an object (it has a value or state, it allows certain operations such as addition, and two equal variables are not the same variable).
However, it would be nice to be able to have a collection of objects, wouldn't it? We already have that in Types, but types are rather restrictive. Instead, we'll use Classes.
A class (in Visual Basic it's called a Class Module) is really just a new file that contains code. Conceptually, a class is both an object, and a container for objects. If you click on the Project drop-down menu and select "Add Class Module", a new class module will be displayed in the Project Explorer, and the Code Window will pop up for that class. A class looks a lot like the code behind a form; in fact, the Code Window will be sitting in the (General) (Declarations) section of the class.
What can you do with a class? You can declare and use variables, and you can add procedures, just like you could with the code for a Form. Your class will act as a container for these variables and methods.
Some of the real power behind classes, and OOP in general, comes from the concept of encapsulation. Encapsulation simply means that some parts of a process are hidden from the programmer. This is achieved by putting the word Private in front of a variable or procedure delcaration; a private variable or procedure can only be used within its class. Let's see an example.
Let's say we have a library-tracking program. It will let you add books to a list of books, remove books from that list, etc. We'll create a class module, named "Library", with code like this:
(General) (Declarations) Dim Private Title(0 To 0) As String Dim Private Author(0 To 0) As String Dim Private NumBooks As Integer Private Sub resetBookList () ' Re-dimensions the arrays Redim Preserve Title(0 To NumBooks) As String Redim Preserve Author(0 To NumBooks) As String End Sub Public Sub addBook (t As String, a As String) ' Adds a book with title _t_ and description _d_ to the list NumBooks = NumBooks + 1 resetBookList Title(NumBooks) = t Author(NumBooks) = a End Sub Public Sub removeLastBook () ' Removes the last book in the list NumBooks = NumBooks - 1 resetBookList End Sub Public Sub clear () ' Clears the entire list NumBooks = 0 resetBookList End Sub Public Function getTitle (n As Integer) As String ' Returns the title of the _n_th book in the list getTitle = Title(n) End Function Public Function getAuthor (n As Integer) As String ' Returns the author of the _n_th book in the list getAuthor = Author(n) End Function |
Now all we have to do to work with the library is code like this:
Dim MyLib As Library MyLib = new Library MyLib.addBook("The Politics", "Aristotle") MyLib.addBook("Dandelion Wine", "Ray Bradbury") MyLib.addBook("Hard Times", "Charles Dickens") txtTitle.Text = MyLib.getTitle(2) txtAuthor.Text = MyLib.getAuthor(2)
This may seem like an awful lot of work just to work with a list of books, but now imagine we want to write a program to track books as they move through a warehouse. All you have to do is pull this Class Module into a new Project, and write the interface! You've just saved yourself a huge amount of work!
You can also see how useful it would be to be able to put classes within other classes, and use them there.