Showing posts with label Class. Show all posts
Showing posts with label Class. Show all posts

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 books...you 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.

Friday, July 30, 2010

Programming- Lesson #1 ~ Creating Your First Program

"The function of good software is to make the complex appear to be simple" - Grady Booch

It is interesting how everything appear to us in the screen simpler than what it really is. Look now at the header of my blog, this has a long code. Look at the title of the blog, it has a code to be appeared this way. Everything on the computer, windows, and internet is edited by Computer Language, written as codes, tested and then appear to us in this way. We can use it easily by just clicking the link. I am sure almost anyone who use the computer knows this. But I like to share what I am learning now; how to program. It is not a very hard thing once you understand it; it is just like a chain. Things are linked and connected to each others...if you didn't understand the first step then you should not move on. I did not program anything yet, I am still learning and sharing my readings with you.

I first started reading about
Visual Basic Programming Language, because this language uses symantics that somehow more readable language than other programming languages which make it easier to grab and apply. So obviously, I will understand everything from the roots in a fun, easy way. You can download it from here to your computer so you can start programming. It's free! let's get started.

Open a new project to start with, but before that you need to make sure that the button "Start without Debugging" is one of the options of "Debug" in the tool bar.

This is a screenshot of the bar itself:




" Debug" is the process that trace the bugs (errors) and fix them so you use the debugger to fix them or find where they are. For example, if you have a minor mistake in the code, this debugger will fix it and tell you why the program didn't run as you expected. If you " Start Without Debugging " then the compiler will start the program without enabling the debugger to trace the errors. The next step is creating the simple program which beginner programmers always call it "Hello World!"... I found it funny, because it is really a new world (programming) to say "Hi" to.


As you insert the "Hello , world!" code between the
Sub Main , you can run the program by clicking " Start Without Debugging" in the tool bar. It is the button we added previously. And as you can see, whenever a code end or starts there should be an End Sub or Sub Main (starting the code). The "Sub" is actually a “subroutine" which what do the actual work in the program, everything in between is part of the subroutine.

Are you wondering what contains the subroutine? Well, there are two statements, they called "
Module" or "Class"…and what they do is having all the information and codes under them. And of course, there is "End Module" that ends the code. Yes, I told you everything is like a chain; everything is part of something and contains different information.


Now, you need to define the variable ( where it stores the data on the computer's memory like text, numbers, integer..etc) and specify the type of it. For example, I want to store this text value " Hello, world!". So what I do is define this variable and name it whatever I like, I would name it " World " or " Message " or any subject you want; in order to save this text in the variable, I will define it as String so I can store them on the computer. " String " is a type to define a variable, and there are many other types we will learn later on.


"Console.WriteLine" is a function in this VB Program that print the codes on the screen while running the program. So when you write Console.WriteLine(" Hello, world!") it will print " Hello , world! "on the screen. It basically print the value of the variable.


Those are the types of Variables:
1) Integer - values between -2,147,483,648 and 2,147,483,647.
2) Byte - values from 0 to 225.
3) Decimal - values with decimal places between ±1.0×10-28 and ±7.9×1028.
4) String - sequence of character.
5) Char - value of one character.
6) Boolean - logical value, True or False.

Let me give another example of different variable type.
I want to calculate the sum of 1 and 3 . Logically, they are integer, so I will use variable of type Integer. So I will write " Dim Total as Integer " - this tells the computer to define the variable as Integer type. Then I would use it as " Total = 4 ". The computer will finally calculate it and save the results as a Total variable.

And by the way, you can't use as a "Integer" value when you put quotes ("") around your variable. VB Program will receive it as a "String".

Now as you put the codes in the right places and define the variables correctly, this what you should see before running the program:



Press any key to dismiss the window.


That was our first lesson..I hope I will get to more complex programs after a while. My next lesson will be about how to use mixed Variables and deal with errors in your program.