Friday, June 25, 2010

A better way to understand basic OOP

OOP - Object Oriented Programming. This is what everyone uses. Once you understand the concept you'll see why its extremely useful when programming. This tutorial will cover very basic OOP so that we can move on into the animation tutorial.

Starting Up
Create a project called OOP. Go to the solution explorer(on your right). Right click where it says "OOP"(right above where it says "References", and go to Add | Add New Item. When you add the item select class, and name it HelloWorld(hmm.. I wonder what this program is going to do). You'll see what classes are in a moment.

VB.NET automatically generates this code for you:

Public Class HelloWorld

End Class

Well if you go back to where it says Form1, you'll notice that it says:

Public Class Form1

End Class

A class is basically a file which contains variables, subs, and functions that do something. Classes are useful because they can be reused. For example, let's say we make a Hello World class. If you give this Hello World class to someone else, and you tell them to use it, their program will display hello world too. Obviously no one will distribute a Hello World class, but if you made a whole bunch of subs that checked collision detection for your game, you could put those subs in a class, and use it for every level in your game to check for collision detection; the advantage is, once you make it, you dont have to type anything over again. The collision detection class(which we will, in fact, make in another tutorial) can be used for every level(or every form) without any exceptions. You don't need to make any modifications at all, as long as you program it properly.

Go back to the HelloWorld class. Add this sub:

Public Sub SayHelloWorld()
MessageBox.Show("Hello World")
End Sub

This sub obviously displays "Hello World" in a MessageBox when you call it.
Go back to Form1. Here's how you use
a class, this concept might be a little tricky. Remember, our class name is HelloWorld. In order to use the class, you'd have to instantiate it, which means you have to create a copy of it.
Dim MyHelloWorldClass As New HelloWorld()
We have to Dim something as the class. For example if you delete this line and go to Form1_Load, type in HelloWorld followed by a dot. You wont get any popup boxes. You have to dim it as your class in order to use it. Now try this, take the code above, and type it in without the New. In form1_load, type in MyHelloWorldClass followed by a dot. YOu wont get a popup box that has SayHelloWorld in it. This question will come up every time: why NEW?

Think about it this way. If you're a historian, and you come across an important document, and you (for some strange reason :-p) want to take notes on it.. you wouldnt! Its an important artifact, if you take notes on it, or draw on it or something like that, it might get ruined (this is THE dumbest story.. I know, but I couldnt think of anything else)! So basically, you have to take a photocopy of it, and THEN take notes on it. In the same manner, you don't want to use the original class, you want to create a copy of it by saying New, and then use the copied class. (bad explanation o_O - if anyone can think of anything better, post it at the forums)

---
Note: 'Stipto' emailed me an alternate explanation for this. He got it from his book, "Programming in Visual Basic .NET by Julia Case Bradley and Anita C Millspaugh." The explanation follows:

Many people use a cookie analogy to describe the relationship of a class and an object. The cookie cutter is the class. You can't eat a cookie cutter, but you can use it to make cookies; the cookie is the object.When you make a cookie using a cookie cutter, you instantiate an object of the cookie class. You can use the same cookie cutter to make various kinds of cookies. Although all the cookies will have the same shape, some may be chocolate, others are lemon or vanilla; some may be frosted or have colored sprinkles on top. The characteristics of the cookie, such as flavor and topping, are the properties of the object.

Thanks Stipto!
---

OK, now that you have the "New" there, go to form1_load and type in MyHelloWorldClass followed by a dot, and click the box that says "SayHelloWorld". Easy enough, run your program and obviously you'll get a messagebox that shows HelloWorld.

That's the simplest class you'll ever use in your entire life :-D. Let's get moving on to some advanced stuff, this was just to demonstrate basic OOP.

In form1_load, delete the line MyHelloWorldClass.SayHelloWorld() line. Go to your HelloWorld class. Change the Public Sub SayHelloWorld to a Private sub. Go back to form1_load and type in the line which you just deleted. As you're typing the dot, you'll notice that SayHelloWorld is missing... that's becuase it's private. "Well why would you want it to be private if you can't use it in the form", you might ask. Well, you can use it within the class - it's not that useful in this program but i'll show you uses for it in other programs. For now, create a Public Sub Hi in the class. In the sub, type in SayHelloWorld(). If you go to form1_load and type in MyHelloWorldClass.Hi, it'll display hello world. What's happening is it's going to the sub Hi, and from that Sub Hi, it's going to the sub SayHelloWorld.


Some Adcanced Stuff
The program which we just did was completely useless, and there's no reason to distribute it because anyone can type MessageBox.Show("Hello World") :-). What we're going to do in this 2nd part of the tutorial is learning about Constructors.

What is a constructor? A constructor is something that is called when you instantiate(make NEW) an object. A constructor is in the sub New of your class. If we took that HelloWorld class, and rename sub Hi to sub New, when we Dim MyHelloWorldClass as New HelloWorld, you get the message. Since we dimmed it in the globals (at the top of your code), we get the message when the program loads (becuase global variables are created when the program starts) A constructor is usually used to pass in Arguments, especially a form. We're going to make a class that changes the background color of the form with the color you specify. Delete your HelloWorld class (right click it in the solution explorer and hit delete. Create a new class called ChangeBackground.

Create this sub:
Public Sub New(ByVal target As Form)
target.BackColor = Color.Red
End Sub

(Note: the "ByVal" is automatically typed in; there's 2 things, byVal and byRef. We'll learn about that in another tutorial) We pass in an argument called Target, which is our form. Instead of saying Form1.backcolor = color.red, we specify an argument called Target which can be used for any form. We know what this does, it just changes the background color of the specified form to red. Go to form1's code. Type this in:

Dim Change As New ChangeBackground(
While you're typing in the parenthesees, a little popup box tells you "Target As System.Windows.Forms.Form", meaning that you have to specify a form. In order to refer to form1, we would just say.. Me!
Dim Change As New ChangeBackground(Me)

Even if you create another form, this will change the background color for every form.
Run your project, and your background color will be Red.

How do we make it so that we change the background color to ANY color we want? Simple, pass in a color argument.

Public Sub New(ByVal target As Form, ByVal YourColor As Color)
target.BackColor = YourColor
End Sub Now go back to your Globals area for Form1. Type this in:
Dim Change As New ChangeBackground(Me, Color.Blue)

Run your program and it changes the background color to blue.
This tutorial is a relatively simple one, just to give you an introduction to OOP. The "Advanced OOP" tutorial will teach you Inheritance, Destructors, PolyMorphism.. etc.

Monday, June 14, 2010

Sunday, June 13, 2010