Page 122 - Open Soource Technologies 304.indd
P. 122
Event Driven Programming
Do not use this code snippet for more than an example of how to compare
times! The eternal looping while waiting for something to happen is a bad
idea in Windows, because your program monopolizes a lot of resources that
way. Instead, set up a Visual Basic Timer object and have a procedure called,
say, every second.
5.3 Concatenation Operators
Concatenation operators join multiple strings into a single string. There are two concatenation
operators, + and &. Both carry out the basic concatenation operation, as the following example
shows.
Dim x As String = “Con” & “caten” & “ation”
Dim y As String = “Con” + “caten” + “ation”
’The preceding statements set both x and y to “Concatenation”.
These operators can also concatenate String variables, as the following example shows.
Dim a As String = “abc”
Dim d As String = “def”
Dim z As String = a & d
Dim w As String = a + d
’ The preceding statements set both z and w to “abcdef”.
5.3.1 Differences Between the Two Concatenation Operators
The + Operator (Visual Basic) has the primary purpose of adding two numbers. However, it can
also concatenate numeric operands with string operands. The + operator has a complex set of
rules that determine whether to add, concatenate, signal a compiler error, or throw a run-time
Invalid Cast Exception exception.
The & Operator (Visual Basic) is defined only for String operands, and it always widens its
operands to String, regardless of the setting of Option Strict. The & operator is recommended
for string concatenation because it is defined exclusively for strings and reduces your chances of
generating an unintended conversion.
5.4 Logical Operator
The logical operators compare Boolean expressions and return a Boolean result. The And, Or,
AndAlso, OrElse, and Xor operators take two operands, and the Not operator takes a single
operand.
The Not operator performs logical negation on a Boolean expression. Put simply, it yields the
opposite of the expression it evaluates. If the expression evaluates to True, Not yields False; if
the expression evaluates to False, Not yields True. An example is given on next page.
116 LOVELY PROFESSIONAL UNIVERSITY