Professional Documents
Culture Documents
Concatenation Operators Join Multi Strings Into A Single String
Concatenation Operators Join Multi Strings Into A Single String
Concatenation operators join multi strings into a single string. There are two
types of concatenation operators + and & both carry basic concatenation
operations.
Dim x As String = “mic” & “ro” & “soft”
Dim y As String = “mic”&”ro”&”soft”
‘the preceeding statemnts sets both x and y to Microsoft
these operators can also concatenate string variables as follows
Dim a As String = “abc”
Dim d As String = “def”
Dim z As String = a & d
Dim w As String = “a + d”
The preceeding statements set both z and w to “abcdef”
LENGTH PROPERTY With the Length property on the String type, you can determine this
count fast. Length is useful in many VB.NET programs. Its implementation is also interesting
could be computed once and stored as a field Or it could be calculated in a loop every time
the property is accessed. In VB.NET, the value is stored, which makes getting the length of
any string equally fast. If the Length were to be computed each time, a loop over every
character that tested against the bound Length would be slow. Performance, for-loop. It is
typically faster to use Length directly in the bounds of a for-loop. The JIT compiler can make
Module Module1
End Module
This program shows the Length property in action. The length of the String "dotnet" is found
to be 6 characters. After another 5 characters are appended, the length is now 11 characters.
A String cannot be directly changed. So when "field" is appended to the String, a whole new
String is created. During String creation, the length value is stored. The String will thus
the VB.NET String, which is returned. This console program shows the result of ToUpper on
the input String "abc123". "abc" are the only characters that were changed. The non-
lowercase letters are not changed. Characters that are already uppercase are not changed by
Module Module1
Sub Main()
value1.ToUpper()
Console.WriteLine(upper1)
End Sub
End Module
Output ABC123
We can determine whether a string is already uppercase. We can use ToUpper and then
compare the original String. If they are equal, the string was already uppercased. However
this is not the most efficient way. A faster way uses a For-Each loop and then the
Char.IsLower function.
If a Char is lowercase, the String is not already uppercase. The code returns False early at
that point.
Module Module1
If Char.IsLower(letter) Then
Return False
End If
Next
Return True
End Function
Sub Main()
End Sub
End Module
Output
ISUPPERCASE: True
ISUPPERCASE: False
ISUPPERCASE: True
TO LOWER changes Strings that contain uppercase letters. We convert all the uppercase
letters in a String to lowercase letters. With ToLower this is easy to doFunction notes. We
Module Module1
Sub Main()
value = value.ToLower()
Console.WriteLine(value)
If cat = cat.ToLower()
End If
End Sub
End Module
Output
abc123 Is Lower
This program uses the ToLower function. In the first example, we lowercase the String
"ABC123": notice how the non-uppercase letters "123" are kept the same in the
output.Function. In the second example, we show how to use ToLower to test if a String is
already lowercased. . It is possible to use an argument to the ToLower function. This is of
type System.Globalization.CultureInfo. This influences how non-ASCII characters are
converted.
TRIM Returns a string containing a copy of a specified string with no leading spaces
containing a copy of a specified string with no leading spaces (LTrim), no trailing spaces
strip leading spaces and the RTrim function to strip trailing spaces from a string variable. It
2. An event is a signal that informs an application that something important has occurred. For
example, when a user clicks a control on a form, the form can raise a Click event and call a
procedure that handles the event. Events also allow separate tasks to communicate. Say, for
example, that your application performs a sort task separately from the main application. If a
user cancels the sort, your application can send a cancel event instructing the sort process to
stop. You declare events within classes, structures, modules, and interfaces using
the Event keyword, as in the following example:
Event AnEvent(ByVal EventNumber As Integer)
Raising Events
An event is like a message announcing that something important has occurred. The act of
broadcasting the message is called raising the event. In Visual Basic, you raise events with
the RaiseEvent statement, as in the following example:
RaiseEvent AnEvent(EventNumber)
Events must be raised within the scope of the class, module, or structure where they are
declared. For example, a derived class cannot raise events inherited from a base class.Any
object capable of raising an event is an event sender, also known as an event source. Forms,
controls, and user-defined objects are examples of event senders.
Event handlers are procedures that are called when a corresponding event occurs. You can
use any valid subroutine with a matching signature as an event handler. You cannot use a
function as an event handler, however, because it cannot return a value to the event source.
Visual Basic uses a standard naming convention for event handlers that combines the name of
the event sender, an underscore, and the name of the event. For example, the Click event of a
button named button1 would be named Sub button1_Click.Before an event handler becomes
usable, you must first associate it with an event by using either
the Handles or AddHandler statement. WithEvents and the Handles Clause
The WithEvents statement and Handles clause provide a declarative way of specifying event
handlers. An event raised by an object declared with the WithEvents keyword can be handled
by any procedure with a Handles statement for that event, as shown in the following example:
Class EventClass
Public Event XEvent()
Public Event YEvent()
' RaiseEvents raises both events.
Sub RaiseEvents()
RaiseEvent XEvent()
RaiseEvent YEvent()
End Sub
End Class
The WithEvents statement and the Handles clause are often the best choice for event handlers
because the declarative syntax they use makes event handling easier to code, read and debug.
However, be aware of the following limitations on the use of WithEvents variables:
You cannot use a WithEvents variable as an object variable. That is, you cannot
declare it as Object—you must specify the class name when you declare the variable.
Because shared events are not tied to class instances, you cannot use WithEvents to
declaratively handle shared events. Similarly, you cannot use WithEvents or Handles to
handle events from a Structure. In both cases, you can use the AddHandler statement to
handle those events.
You cannot create arrays of WithEvents variables.
WithEvents variables allow a single event handler to handle one or more kind of event, or
one or more event handlers to handle the same kind of event. Although the Handles clause is
the standard way of associating an event with an event handler, it is limited to associating
events with event handlers at compile time. In some cases, such as with events associated
with forms or controls, Visual Basic automatically stubs out an empty event handler and
associates it with an event. For example, when you double-click a command button on a form
in design mode, Visual Basic creates an empty event handler and a WithEvents variable for
the command button, as in the following code:
Friend WithEvents Button1 As System.Windows.Forms.Button
Protected Sub Button1_Click() Handles Button1.Click
End Sub