C# Object Based Programming

All tutorials created in C# to be posted in here.
1 post Page 1 of 1
Contributors
User avatar
reececlarke
VIP - Donator
VIP - Donator
Posts: 245
Joined: Tue Dec 08, 2009 6:47 pm

C# Object Based Programming
reececlarke
C# Object Based Programming

Introduction – What is object based programming and how can it help me

Object programming is based around having classes as abstract as you can get them, not only does this help with code reuse as you are then able to simply drag and drop parts of code you have already written into new projects but it also helps with easily implementing new features for example if you were to create guns within a game instead of having it have an switch statement (Or a set of if statements) you simply just interface with a gun manger object which allows you to simplify code.
Inheritance
Inheritance is one of the more simpler of object based programming methods and it allows you to inherit the properties of the class which you assign it to as well as what that class inherits for example if you were to have a class where you had a function called ‘heal’ if you were to have a class that inherited the previous class it could also call the function ‘heal’. However an important thing to note with this is that you can only inherit one class which means if you want the class to interface with more than one class then you will need to use interfaces.
Syntax
In order to use inheritance you will need to add the following code to your project where you declare the class for example
Code: Select all
public class testClass {

}
We need to add ‘: ‘and then the class that we would like our test class to inherit from in this case we will be inheriting from the class called motherClass so it would be written down as displayed below.
Code: Select all
public class testClass : motherClass{

}
What this allows us to do is now call anything which is within the mother class for example let’s say we had a function called dance in order to call this all we would had to do is call it as we would if the function was in the testClass in this case it would be
“dance();”
Management Objects
Management objects are generally designed to provide a layer between an object and an interface for example if we had a collection of weapons we would use a management object as a middle man between the weapon and the player controller generally we try to make sure that management object do not relay on any external code in order to make them as compact as we can and to make sure that they are only responsible for one part of the project and not a ‘God’ object which controls a lot more than it really should. While it might be easier to code every single function into a single object it makes quite a few problems for efficiency for example we don’t need a form of recoil in every single gun e.g. a laser weapon would not have it so why should we clutter it up with the code for recoil if it does not need it. Or if you are creating a combat ship in a RTS would it need to have the code in order to mine as well? I don’t think so. So in essence only have code related to that object is the important note to take here.
Syntax
First one of the important things to get immediately down is to remove any reliance this object has on as many objects as it can this also includes inheritance generally this object should only have to rely on its self and any data it should need should be passed on when required in this case since we are doing a weapon manger class we would pass the gun it will be managing when it is instanced.
Code Reference:
Code: Select all
Public class weaponManger{
weaponInterface WI; // This is the weapon interface we will be calling from this will be mentioned in //much more detail within the weapon section bellow.
Public class weaponManger(weaponInterface newWI)
{
WI = newWI; // what we are doing here is passing on the information provided to us about the //weapon we will be using to the weapon manger in this way we can make sure that we are as //abstract as we can be allowing this to be used in a wide range of situations.
}
Public void fireWeapon(string target)
{
Messagebox.show(“Did :”+ WI.Damage()+” To : “+target);
// what we are effectively doing here is getting a target in this case the target will be 
//a string however this could be easily extended to a target class which could contain all the //information needed about the target for example name, location, friend or foe etc needed.
}
}
Interfaces
Interfaces are some of the more advance objects when it comes to object orientated programing generally this will be used when you require the ability to switch between a range of object for example if you have a lot of ores in your game and you want to be able to mine between them without having to write mining code for each ore you could instead write data for each ore then provide an interface which would use that data for mining the ore for example this could be how hard a ore is, how much the ore holds and the type of pickaxe it would require to mine it.
In the case of this project we will be referring to the weapons interface and we will be returning how much damage the gun will do however this could of course be extended to for example each gun holding the information to the animation it would play along with other features such as the gun speed etc.
However we will need to create two classes for this first we will need to create the weaponInterface class as well as the pistol class. The weponInterface class which contain what all weapons need to provide to the interface in this case all weapons will need to provide an amount of damage which they deal which will be provided in the form of a public void called ‘Damage’.

Syntax
To start off with we will be creating an interface called weaponInterface in order to do this we will need to delete where it says public class and replace it with public interface like shown below.
Code: Select all
Public Interface weaponInterface{
Void Damage(); // We will not be required to put public in the front of the void shown in here //because all items within an interface by default are public. One good thing to note about interfaces //is that you cannot create functions in here there only feature is to point towards them rather than //being the function themselves. 

}
Next we will be creating the pistol class like shown below
Public class Pistol : weaponInterface{
Public void Damage()
{
Return 5;
}

}
A important feature to note here is that just like inheritance you place interfaces behind the class name using ‘:’ however unlike interfaces you can use as many interfaces as you like on a single class which means they could provide information to as many management objects as you would like which could come in handy at certain points.
Hope this has help your understanding of object based programming and why it is so heavily used because of how easily expanded it can be.
Image
1 post Page 1 of 1
Return to “C-Sharp Tutorials”