# The Official OOP guide for Rednecks ™



## Kreij (Dec 19, 2010)

*The Official OOP Guide for Rednecks ™ * ** ****
(* I'm a northern redneck. If you take offense at the term KMA)
(** this guide assumes you know at least a little about programming, like what a variable is)
(*** TPU, all it's affiliates and especially Kreij take no responsibility for anything)

Object Oriented Programming (OOP) has been around for quite some time and this guide is meant to try to make it as easy as possible to understand the terms and ideas associated with it. No more, no less. So grab yourself a beer and we'll get started.

*What is OOP?*
OOP is a paradigm or programming methodology. WUT?
It's just a way of looking at programming. OOP programming looks at the program from the standpoint of … you guessed it … objects. 
That beer in your hand? That's an object.
The fact that's it's almost empty now? That's not an object. Go get another.
We'll get to all that shortly.

*What is the history of OOP?*
Google it, this is a guide, not a history lesson.
_They say_ that those that forget history are destined to repeat it. _I say_ grab another beer. You're call. 

*When should I use OOP?*
When your program requires the use of objects. Duh.

*What's an object?*
Anything that has a state (one or more properties) AND a need for some way to manage a change of those states. WUT?
As I said, that beer is an object. One of it's properties might be “Amount left”, which will change over time. The program may have a function (or method as they are usually called) named “Get Another Beer”, based on the “Amount Left” property.

*How do I make an object?*
It's really easy. You just create a class that defines the object, it's properties and state management methods. WUT?
Okay, let's look at some code (all examples are in C# 'cause it's easy to understand what you are looking at).


Spoiler: ”Code Example”





```
[color=Blue]public class[/color] Beer
{
    [color=Blue]private[/color] [color=Teal]Int32[/color] _AmountLeft = 100; 
    [color=Green]// This makes the beer full when you create it.[/color]
    [color=Blue]private[/color] [color=Teal]Boolean[/color] _GetAnotherBeer = [color=Blue]false[/color]; 
    [color=Green]// This is true or false, depending on how much beer is left.
    // We set it to false originally because the beer is full when you open it[/color]

    [color=Blue]public[/color] Beer() {};

    [color=Blue]public[/color] [color=Teal]Boolean[/color] GetAnotherBeer { [color=Blue]get[/color] { [color=Blue]return[/color] _GetAnotherBeer; } }

    [color=Blue]public void[/color] TakeAnotherGulp()
    {
        _AmountLeft = _AmountLeft – 20; [color=Green]// Subtract 20% of the beer each gulp.[/color]
        if (_AmountLeft < 40) _GetAnotherBeer = [color=Blue]true[/color]; 
        [color=Green]// If your down to your last gulp, set GetAnotherBeer to true[/color]
    }
}
```




Coming Soon … What to do with the class you just made and an explanation of everything in it. I'll append it to this post.
(Give me a break, these guides take a lot of time to write, proofread and get right. )
Comments always appreciated.


----------



## char[] rager (Dec 19, 2010)

Thanks. I was recently wondering how games keep track of things, and I tried to simulate enemies using classes. I made some pretty obfuscated code, but it clearly demonstrates the power of object oriented programming.

I await what information somebody with your programming experience will be able to shed on OOP

*Enemy Class*

```
namespace EnemySoldier
{
    class Enemy
    {
        public int health;

        public Enemy()
        {
            health = 100;
        }

        public Enemy(int newHealth)
        {
            health = newHealth;
        }

        public void shootThisEnemySoldier(int damageDone)
        {
            health -= damageDone;
        }

        public bool isThisEnemySoldierDead()
        {
            return health <= 0;
        }
    }
}
```

*Implementation*

```
using System;
using System.Collections.Generic;
using EnemySoldier;

class Game
{
    static void PrintEnemySoldierHealth(List<Enemy> enemyList)
    {
        foreach (Enemy enemyObject in enemyList)
        {
            Console.WriteLine(enemyObject.health);
        }
    }
    
    static int IsEnemySoldierDead(List<Enemy> enemyList)
    {
        int numberOfEnemySoldiersDead = 0;
        
        foreach (Enemy enemyObject in enemyList)
        {
            Console.WriteLine(enemyObject.isThisEnemySoldierDead());

            if (enemyObject.isThisEnemySoldierDead() == true)
            {
                numberOfEnemySoldiersDead++;
            }
        }

        return numberOfEnemySoldiersDead;
    }
    
    static void Main()
    {
        int numberOfEnemySoldiersDead;
        
        List<Enemy> enemyList = new List<Enemy>();
        Random randomHealth = new Random();
        
        Console.Write("How Many Enemies Do You Want?  ");
        int numberOfEnemies = int.Parse(Console.ReadLine());
        Console.WriteLine();

        for (int counter = 0; counter < numberOfEnemies; counter++)
        {            
            enemyList.Add(new Enemy(randomHealth.Next(1, 500)));
        }

        PrintEnemySoldierHealth(enemyList);

        do
        {
            Console.WriteLine();

            Console.Write("How Much Damage Do You Want To Inflict On The Enemy?  ");
            int damageDelivered = int.Parse(Console.ReadLine());

            foreach (Enemy enemyObject in enemyList)
            {
                enemyObject.shootThisEnemySoldier(damageDelivered);
            }

            Console.WriteLine();

            PrintEnemySoldierHealth(enemyList);

            Console.WriteLine();

            numberOfEnemySoldiersDead = IsEnemySoldierDead(enemyList);

            Console.WriteLine();
        } while (numberOfEnemySoldiersDead != numberOfEnemies);
    }
}
```


----------



## Ross211 (Dec 20, 2010)

I like creating arrays in which each index in the array is an object I created by creating my own custom class and creating an instance of it....  *Have I confused you yet ?  Yeah, this is OOP!  * 

I like playing around with arrays of objects.  It is very elegant way of programming because each index in your array can have multiple "instance variables".  This makes it easier to store information in an array more efficiently and accomplish more using less memory (RAM).  Don't forget to destroy objects when they are not needed, you expert programmers!

Example (This is VB.NET) -
Here is my "serials" class.  

```
Public Class Serials
    Public systemSerial As String
    Public boughtFrom As String
    Public tradeDate As String
    Public employee As String

    Public Sub New(ByVal system As String, ByVal customer As String, ByVal tDate As String, ByVal emp As String)
        systemSerial = system
        boughtFrom = customer
        tradeDate = tDate
        employee = emp
    End Sub
End Class
```

Now I create a big fat array of the type serials in my main form's code.

```
Public systemSerials(1000000) As Serials
```

[edit]
Almost forgot, I need to create an instance of this class.

```
For k = 0 To systemSerials.Length - 1
            systemSerials(count) = New Serials("", "", "", "")
        Next
'^^^^^ count is being incremented elsewhere
```


Then I play around :~)

```
Public Sub WriteSerials()
        Using Writer As New StreamWriter(selectedSystem + ".txt", False)
            For k = 0 To lsbDisplay.Items.Count - 1
                Writer.Write(systemSerials(k).systemSerial + ",")
                Writer.Write(systemSerials(k).boughtFrom + ",")
                Writer.Write(systemSerials(k).tradeDate + ",")
                Writer.Write(systemSerials(k).employee + ",")
                Writer.WriteLine()
            Next
            Writer.Write(systemSerial + ",")
            Writer.Write(boughtFrom + ",")
            Writer.Write(Now.Date + ",")
            Writer.Write(employee + ",")
        End Using
    End Sub
```

I really like this thread Kreij, it helps noob programmers like myself that are still in college.  I learned OOP first in a Java programming course, I'm very glad I understand it at the bare level I do.

I hope maybe this example can help some VB noobies like myself


----------



## olithereal (Dec 20, 2010)

Great thread Kreij! I'm sure this will help a TON of new programmers! It probably will clarify and teach me more than a couple things as well! I'm still in college, so I try to read as much as I can on the subject!

Keep it up, and a big thanks!


----------



## streetfighter 2 (Dec 20, 2010)

This would probably be a good time to mention that programmers should be careful about passing an entire object instance to a function unless a good chunk of the object is going to be modified by the function.  This relates to a program I wrote some years ago where the instances were rather large (~50MB of memory) and one of my functions was creating a huge slow down when all I needed to do was assert a single property of an object instance.


----------

