Not last week I was speaking to some pretty experienced Visual Basic developers who had still not embraced Classes in Visual Basic. They just inherently didn’t “get” classes in Visual Basic. These were solid developers and intelligent guys so this confused me. They were quite happy to use Subroutines and Functions organised in neat Modules but were literally scared or intimidated by Classes. Another name for a Class in Visual Basic is actually “Class Module”. The name implies that a class or class module is an extension of what they already know so why were they intimidated?
There’s nothing wrong with their approach you know. You can achieve a lot using well organised Modules, Subs and Functions. Programming has been around in some form since the 1800′s, however classes weren’t introduced until the 1960′s. I developed in straight vanilla BASIC (before it went all Visual on us), FORTRAN and Pascal for years without ever developing a class. The languages didn’t just support it. Subroutines and Functions organised in Modules worked perfectly well. Good code is still good code, no matter how it’s represented. However once I got classes I turned into a modern application developer, capable of communicating with other modern developers. As well as enabling me to get a job and carve out a career as a programmer, understanding classes has allowed me to write the most elegant code my abilities allow. As a craftsmen, you should also have the aspiration to write elegant code and to do so in Visual Basic, you will need to “get” classes. This tutorial will help you do just that (and in the process overtake many seasoned programmers).
So far you have used Functions and Subroutines to develop well organised code that obey the DRY Principle of Do Not Repeat Yourself. The DRY Principle, if you remember, states that “Every piece of knowledge must have a single, unambiguous, authoritative representation within a system”. Every piece of code must be well named, have a distinct role to play in your application and must not be repeated. To comply with the DRY Principle we have used well named Functions and Subroutines to avoid ever repeating the same piece of code twice. We then used Modules to organise these Functions and Subroutines to keep our application easy to understand and maintainable.
In addition to this, we have used Variables, Arrays and Collection Classes to store data – values of data, lists of data or complex structures of multi-dimensional jagged data. This was no mean feat.
Think of a Class as a Module on steroids. We can use classes to organise our code, keeping our Functions and Routines in one location, and we can combine this with the ability to store data like we would with Variables, Arrays or Collections Classes.
In our last example we used a Visual Basic class, the Visual Basic Collection Class. By using this class, you are already familiar with the concepts of what we will do here. Microsoft, by providing us with the Collection Class, have provided us with a means to store, retrieve and manipulate data in one well named unit. A Class Module.
Looking at the Collection Class in more detail:
It provided a Property called Count. This returned the number of items in the collection. Think of a Property as a variable, typically allowing us to read or write one item of data.
It also provided Methods, e.g. Contains or Add. The Contains Method checks to see if a particular item exists in the Collection and returns a Boolean to whether an item was found or not. The Add method adds an item to the collection but doesn’t return a value. Think of a Method as a Subroutine or Function, a named unit of code that performs a certain action.
A Class is simply a named collection of variables, functions and subroutines, which also allows you to store data. These Properties and Methods define the Interface for the class, i.e., how you can communicate with the class. This interface can be described in a diagram.
So why didn’t the developers I spoke to last week understand that? They’d already understood and used all the concepts of a class on a daily basis, they just had no idea they did. Typically in software development, the simplest of concepts are made to sound complex. Hopefully tutorials like this explain these concepts in simple English.
Anyhow, in the next tutorial we will create our own Collection class for storing my Visual Basic books.