Operators - Concatenation Operators (Page 2 of 4 )
Concatenation operators connect two source string expressions together and return a single string joined from the two original strings. Because strings in .NET are immutable, the returned string is always a completely new string instance.
& (String Concatenation)
The string concatenation operator returns a concatenated string from two source string expressions. Any non-string source expression is first converted to a string prior to concatenation (even if OptionStrict is set to On).
result = expression1 & expression2
+ (Addition)
When the addition operator is used with string operands, it concatenates the operands instead of adding their values. However, using this operator for concatenation can make the source code unclear, especially when using the new .NET-recommended variable naming conventions. If you mix string and numeric operands, this operator may also cause compile-time or runtime errors, depending on the content of the operands. For the clearest code, use the & concatenation operator instead.
Logical and Bitwise OperatorsLogical operators evaluate one or more expressions and return a Boolean result (True or False). VB supports six logical operators, many of which can also be used as bitwise operators, along with two bitwise-only operators. Bitwise operations work on integral (numeric integer) operands at the bit level and return numeric results. Other languages, such as C#, include distinct logical and bitwise operators, but for historical reasons, VB mostly uses a common set of operators for both types of operations.
If any of the operands are numeric (that is, non-Boolean), a bitwise operation is done instead of a logical operation. In cases where one operand is Boolean and the other is not, the Boolean operand is converted to a number first, using 0 for False and -1 for True.
In performing some logical operations, the .NET versions of Visual Basic use conditional short-circuiting, where complex conditional expressions are only partially evaluated if the final result of the entire expression can be determined without full evaluation. Individual expressions within a larger compound expression are evaluated only until the expressions overall value is known, unless one of the individual expressions involves a call to another function or subroutine. Short-circuiting can occur in logical AndAlso operations when the first operand evaluates to False, as well as in logical OrElse operations when the first operand evaluates to True. When using the more common And and Or operators, no short-circuiting is done.
Boolean operations always use the two Boolean values of True and False. Although Visual Basic's Boolean data type is based on the underlying .NET System.Boolean data type, its use in Visual Basic differs from that of other .NET languages. For historical reasons, Visual Basic's True value, when converted to a number, equates to -1. Other .NET languages--specifically C#--use a value of 1 for True. Although .NET resolves this difference through the shared data type, it can become an issue if you use a non-.NET data transfer method (such as a plain text file) to share numeric Boolean data between .NET languages.
Next: Logical and Bitwise Operators Continued >>
More Visual Basic.NET Articles
More By O'Reilly Media
|
This article is excerpted from chapter five of Visual Basic 2005 in a Nutshell, Third Edition, written by Tim Patrick, Steven Roman, Ph.D., Ron Petrusha and Paul Lomax (O'Reilly; ISBN: 059610152X). Check it out today at your favorite bookstore. Buy this book now.
|
|