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.

2 comments: