Friday, November 26, 2010

Programming- Lesson #6 ~ Creating Your Own Classes

“Most software today is very much like an Egyptian pyramid with millions of bricks piled on top of each other, with no structural integrity, but just done by brute force and thousands of slaves.”- Alan Kay

I did not write any post about programming since long time ago and I keep delaying because it requires time because I read, learn, then write about what I've understood. Here is one post about Lesson #6, and there should be one more post to finish up the basics.

So what is Classes? and are we able to build them our own? Yes, you can. Classes are the structure of your program. The Hello World program is simple and small, you would find your way around even if it wasn't organized, but when you start doing more complex programs, you probably want your codes organized in classes so you find them easily. So it is important to understand how to create those classes. There is no right or wrong in making your classes.. Imagine you are in the library, and there are so many want to find a specific novel... the librarian may organized the books collection based on the alphabetic names, or the most recent books. So you can organize your program in any way you like.

In our small Hello World program, we will create a greeting class (just like how we greeted Diana, Hannah and Alex here).

Here is how you create a class in your program:

My application called " MyFirstApplication " , so whatever you names your file name, go toSolution Explorer window, right-click on the project you have then point at Add, then clickClass. Name it anything you like, I named it " Greeting.vb " then after that, you would see a window opened with the a class called " Public Greeting Class. "

For now, copy below to your new class and then we will explain it later on and why it looks this way.

Also, Modify your SubMain as follows:

When you run th program, you will see that nothing it changed, it is just this boring Hello Eric greeting, but the way we does this greeting is completely different as you noticed. We wrote the code in a very different way. This is how it will looks like:

As you saw in the code above, there is Dim theGreeting As Greeting. This is our new Main Sub. So now you are wondering " whats the difference between this type of greeting and the ones we did before?"

In this line of our class Dim theGreeting As Greeting the difference is the type of variable. Remmeber in the previous lessons here and here when we talked about Strings and Integers? Our variable here is Greeting not string or integer, there is no numbers or anything, right? It can't hold anything except the Greeting value, therefore; the 'slot' is empty and we have to fill it with something after the ( = ) sign like we did above. Here is what we added: Dim theGreeting As Greeting. This is an object that identify the greeting and what type it is. It is a class of an object. Our new object called instance of the greeting class. So when you want to use this term in VB, you call it instantiation. So we are instating a class here. See the module below.

This module shows the class we created and in what we will fill it. The property of the greeting is Recipient. So in order to do that we have to set he recipient to greet Eric like this: theGreeting.Display()

There should be one more post and we will finish the first part ( and basics ) for Visual Basic.