Data type errors

The following sentence:

I green the monkey.

Has a grammatical or syntax error. "Green" is an adjective, not a verb, and a person reading or hearing this sentence would not be able to make sense of it. It is nonsense. Similarly, if you enter a VB statement that the compiler cannot understand, you have committed a syntax error.

We saw that assignment statements can be used to change property values. For instance, if the computer executes this statement:

label1.Top = 20
The Top property of label1 will be assigned a value of 20, which will cause the label to move to a position 20 pixels below the top of the window.

But, what if I had written:

label1.Top = "hoho"
This does not make any sense. The Top property has a numeric data type, and "hoho" is a string. The computer would not know what to do with this instruction and would generate an error message when it tried to execute it. This would be a VB syntax error. It is the VB equivalent of "I green the monkey" in English.

If you think a bit about assignment statements, you will realize that the left and right hand sides should always have the same data type. It makes no more sense to assign a number to a string property or a string to a boolean property than to assign a string to a numeric property.

Luckily, the development system can automatically catch syntax errors for us, as you see below:

The development system has placed a squiggly line under "hoho" and when I placed the cursor over it, it displayed a box explaining the error. It is saying (in computer jargon) that it cannot convert a string to an integer. (An integer is a kind of number).

Checking for syntax errors is very handy, but it is not done by default. To turn it on, I placed a statement saying:

Option Strict On
at the very start of the program.

From now on, put an Option Strict On statement at the beginning of every program you write. (You will not get credit for assignments without it).


Disclaimer: The views and opinions expressed on unofficial pages of California State University, Dominguez Hills faculty, staff or students are strictly those of the page authors. The content of these pages has not been reviewed or approved by California State University, Dominguez Hills.