An Introduction To Classes in Visual Basic

 

Not last week I was speaking to some pretty experienced Visual Basic developers who were still not using Classes in Visual Basic.   They just inherently didn’t “get” classes in Visual Basic.  These were solid developers, had been developers for a number of years and they’re 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”.  Even the name implies that a Class is also a Module – 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 and classes weren’t introduced until the 1960′s.   I developed in straight vanilla BASIC (before it went all Visual on us), C, FORTRAN and Pascal for years without ever developing a Class.  The languages didn’t even support classes.  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 carve out a career as a programmer, understanding classes has allowed me to write the most elegant code my abilities allow.   We developers are craftsmen.  As a craftsman you should have aspire to write the most elegant code that your abilities allow.  To write the most elegant code in Visual Basic requires that you “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.  In other words, every piece of code must be well named, have a distinct role to play in your application and must not be repeated.  Do Not Repeat Your Code!

 

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 VariablesArrays 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 as a Module on steroids – a Module that we can use to store data. That’s right, as well as using a Class to organise our code and keep our Functions and Routines in one well named location, we use Classes to store data.  A Class takes the best bit of Modules, Functions/Subs, Variables and Arrays/Collections Classes in one programming concept.

 

In our last example we used a Visual Basic Class called the Visual Basic Collection Class.   By using this Class you are already familiar with the concepts of what we will introduce here.   Microsoft, with the Visual Basic Collection Class, has provided us with a means to store, retrieve and manipulate data in one well named unit.  Microsoft has provided us with their own Class Module.

 

This Visual Basic Collection Class provides:

 

  • 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.
  • Methods.  The VB Collection Class has a number of Methods e.g. a Contains method and an Add method.  The Contains Method checks to see if a particular item exists in the Collection and returns a Boolean to whether the item exists.  Much like a Function.  The Add method adds an item to the Collection but doesn’t return a value.  Much like a Subroutine.  Think of a Method as a Subroutine or Function, a named unit of code that performs a certain action which may return a value.

 

A Class in its simplest form is a Collection of Variables, Functions and Subroutines represented as Properties (variables) and Methods (Subroutines and Functions) in one well named Module of code. These Properties and Methods define the Interface for the Class, i.e., how a developer will interact with the Class.  This interface can be described diagrammatically.  On the Microsoft web site, the Visual Basic Collection Class is described using the following diagram:

what is a visual basic collection class

It’s not the simplest diagram but you can clearly see how you would interact with the Class, even if you don’t understand what each Method or Property is for.

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 these Visual Basic books.