OO Programming Walkthrough
OO Programming Walkthrough
This will show you how to implement each of the key concepts of OO in Visual Basic through simple
implementation of each concept. Please read the paragraphs carefully as you’re going through.
Classes
Remember that a class is a blueprint that we can use to create objects. First thing we are going to do
is to create a new project in VB which will be a Class Library. You need to make sure to give it a
meaningful name, I’ve called mine ‘OOClassLibrary’. I’ve also created a specific folder called ‘OO’ to
save it in, you should do the same as we’re going to be using two files!
You should now have a simple coding window will looks like this:
Now it’s time to create our blueprint. We’re going to rename it and assign some Attributes to it.
Note that attributes are declared as private and I’ve used slightly funny names… Why? We’ll get to
that in a little while.
Inheritance
You were probably expecting me to talk about objects first weren’t you? We aren’t creating any
objects just yet because the Unit Class that I have created is intended to only be used as a Base class.
This means that we won’t actually create any Unit objects, we just want to inherit from it because all
units will share those same attributes. So, time to create our Barbarian Class!
As you can see, we simply declare a new class called ‘Barbarian’ and on the next line we tell it to
inherit Unit. Easy right? Try using one of the variables from the Unit Class in the Barbarian Class…
Encapsulation
We’re already talked about encapsulation. Encapsulation is the process of making attributes in a
class private so that they can’t be used outside the class. So, we can’t use those variables even
though we have inherited the class. That seems a bit pointless doesn’t it? However, our Barbarian
Class does inherit anything from the Unit class that is Public. So, we need a way of using those
variables despite them being encapsulated.
What we are able to do it to create what is known as a Public Property. This allows us, through the
public get and set methods, to modify private attributes from outside of the class. Back to our unit
Class now:
Here I have created a public property called name. Note that ‘name’ here is more what you might
expect your variable to be called compared to my earlier ‘strName’; this is because ‘name’ is what
we will use everywhere else in the program outside of this base class.
Now visual studio has very helpfully created all the skeleton code needed for our Get & Set methods.
The set method is used to change our private attribute, the get method is used to retrieve the value
in that private attribute. So, let’s get these methods programmed:
Simple as that :) The Set method will take whatever value is passed into the public property and save
it into our private attribute. The .Get method will return the value in the private variable.
Now create public properties for the health and the attack in the exact same way. Try it yourself first,
if you can’t quite manage it then the code is below…
So why did we do it this way rather than just making the attributes public? People can just change
the attributes anyway thanks to our getters and setters. Making them private allows us to uphold the
principles of encapsulation, protecting our private attributes from external classes by adding
validation at the moment of object instantiation. For example, I want any unit name to be two or
more characters so I can validate for that in my Public Property for the name.
So now if the name is fewer than two characters a message will be displayed and the property will
exit. This means that the value of the Private attribute will not be Set.
The Constructor
The constructor is a special Method that needs to run whenever we create an instance of a class. It
will set the initial values of the Attributes at the point the object is created. The constructor is always
called ‘New’. In the constructor for the Barbarian Class we want to set the name to a value passed in
by the programmer and the health and attack to be set values to be defined by us.
See here how I have created inputname as a parameter that the Method is expecting an argument
to be passed into. I have also set the health and attack to set values. Note these are the names of
our public properties we declared in the Unit class. We aren’t getting any errors, so our inheritance
worked!
Object instantiation
Finally! Time to create an object. First thing you need to do is build your current program:
Now you’re going to need to create a new visual Basic Console App. Give it a sensible name and save
it into the same folder that we saved our Class Library. I’d open a fresh copy of visual studio to
create this so that your class library is still available to you.
This should look familiar to you. We created a class file earlier as that file can now be used on an
infinite number of different VB programs where we want to use those classes. But first we need to
add a reference to that file. Go to Project > Add reference
Then click Browse in the bottom right. Browse to the Debug folder of your Class library file and select
the .dll file. Then click ok.
Now that we have created the reference, we can Import the Library that we created:
Make sure to type the imports line at the top. This will
now allow us to create our objects
I’ve added the console.writeline to show that the attributes have been declared correctly.
We’ve now created an object :) Run the program to see if it has worked.
Now let’s create another Class called Archer. We have to go back to our class library file and declare
it in the same way we did our Barbarian.
Now we want our Objects to actually be able to do something and for that we need Methods.
Methods are declared just like Subroutines. Polymorphism refers to a class inheriting a method but
then overriding what that method so. So, our different child classes can call the same inherited
method and have it do different things.
I’m going to start by declaring a method in the base class. For now, I’m not going to have it do
anything because the point is to inherit it and use polymorphism.
Now I’m going to declare that same method in both of the Barbarian and Archer Classes but I’m
going to Overload (Override in any other language, thanks VB!) what they do.
For now I’m just making them print a message to demonstrate the point. Rebuild the solution again!