Are the shorthand assignment operators used often in practice?


Visual Basic contains a number of shorthand assignment operators, such as +=, -=, *=, and so on. These operators first perform a mathematical computation (such as addition in the case of +=) and then an assignment. They are used in the following form:

variable += expression

They have the effect of adding the value of expression to the current value of variable and then storing this summation as the new value of variable.

These shorthand assignment operators were first introduced in Visual Basic 7, which was the version of Visual Basic released when Microsoft's .NET platform first came out. (The Visual Web Developer uses Visual Basic version 8.) Prior to .NET, the shorthand assignment operators did not exist. Therefore, if a developer wanted to increment a variable by one, she'd have to use the more verbose code:

variable = variable + 1

as opposed to the more succinct option that is available in VB.NET:

variable += 1

Shorthand assignment operators are used quite often in practice due to this succinctness. In fact, in a number of examples throughout this book, we'll see these shorthand assignment operators in use.


Are there any advantages to setting Strict as true in an ASP.NET page?


Recall that setting the Strict option to true in the Properties window configures Visual Basic so that implicit casting is not allowed. Setting it to False (or simply leaving it unspecified) allows implicit castings that are widening.

Personally, I prefer to enable implicit widening casts because I find that it leaves the code less cluttered. However, implicit casting is more error prone because a variable may be automatically cast from one type to another without your knowledge. For example, the / operator returns a nonintegral result. The following code, however, will not produce an error message unless Strict is set to true:

Dim x as Integer
x = 10/3

Here, the value of 10/3 will be 3.3333333. However, because the result is assigned to x, an Integer, the value is implicitly cast to an Integer, meaning that the decimal portion is truncated.

To see why implicit casting can lead to potential errors, imagine that at some point later in your code, you display a particular message that x is greater than 3. This conditional code will never execute because x is equal to 3, not greater than 3. However, this may baffle you, since, in examining the code, you might mistakenly think that x has the value 3.3333333 and therefore the conditional code should execute.

 Python   SQL   Java   php   Perl 
 game development   web development   internet   *nix   graphics   hardware 
 telecommunications   C++ 
 Flash   Active Directory   Windows