Saturday, July 31, 2010

Programming- Lesson #2 ~ Mixed Variables and Errors

" Measuring programming progress by lines of code is like measuring an aircraft building progress by weight" - Bill Gates

Who would measure an aircraft building pro
gress by weight? this is just same as measuring a program by the code lines...because usually the code is so much longer than the line itself that appears on the screen. It happens alot in programmers lives especially the ones who get clients from other companies. Some clients ( that are not patient ), they tell the developers or the
programmer himself " why this takes along time to write a code fo
r !! it is only one line !!", and they don't know that it takes many hours to write codes in accurate way.

Why would we mix variables in a program while we have each type to it's own variable?
This actually confused me in the beginning, but Visual Basic allows some data types to be mixed to make it easier for the programmer so he can use different types with different variables...BUT there is an expections.. for example, if you want to declare String variable by
saying " message = 1"; although " 1 " is not a String, it is Integer..but it would understand it. But if you declare Integer as a String, it will give you an error and the computer will tell you there is a problem because you can't assign String value to Integer type. It will not be converted.

Let me give you another example of mixed data and an error types:

As we said previously, Boolean is a logical data value and String is a sequence of characters which is the word " Good ". So VB can't convert " Good " to Boolean because it is not a logical
statement, it is value type of String. It will give you bellow error:

" Dim " is a keyword that tells the computer we are about to have a variable. If you are putting more than 1 variable, then it will be easier for you if you put all Dim statements in the beginning of the Sub so you can see all the variables you would use. " Dim " stands for dimension of the space memory in the memory for variable.

While I was trying different Sub names and values, rather than putting " Sub Main " I put " Sub WrongDataType "... I thought it is ok to change the name as I read so wh
en I tried to run the program, it gave me error that the Sub Main cannot be found.
But what's right is you should put at least one Sub Main in the program, and then under it you can put other Subs with different names.

I like to share another mistake I did while learning... I wrote the " End Sub " after " End Module " which also gave me this error " 'End Sub' must be preceded by a matching 'Sub' " which means you can't put the End Sub after the Module.

Finally...when you pass value directly to WriteLine function you should put around it double quotation marks ("") but when you pass a variable you should not put double quotation marks. So now, WriteLine will know that this is a variable or a value due to the way its written.

That's it for this post...I hope I did not make it confusing and made it simple enough to be understandable. The next lesson will be about functions and how you control your program.

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.