
110
|
C# & VB.NET Conversion Pocket Reference
to declare the event and pass to the constructor the name of
the function you wish to use to catch the event notification.
In VB.NET you use a slightly different approach:
Class MailClient
Dim WithEvents ms As MailServer
Public Sub PrintToScreen( ByVal msg As String) _
Handles ms.OnReportProgress
System.Console.WriteLine(msg)
End Sub
Sub Compose( )
ms = New MailServer( )
End Sub
End Class
The first step in requesting event notifications is to declare a
member variable of the class that provides the event.
MailServer has an OnReportProgress event; therefore, you
declare a variable of type MailServer. However, you must use
the
WithEvents keyword in the declaration to receive event
notifications. The second step is to declare a method that has
the same signature as the event. In the declaration of the
method, you add a
Handles clause plus the variable name you
declared with
WithEvents, a dot, and the name of the event.
In the example, the clause reads:
Handles ms.OnReportProgress
The last step is to register for notifications. This is done by
setting the
WithEvents variable to an instance of the class that
fires the event. To unregister from notifications, you set the
WithEvents variable equal to Nothing, as follows:
Sub Silent
ms = Nothing
End Sub
Comparing Classes
Classes fall under two main categories: value types and refer-
ence types. Value types are classes that are derived from
VB
VB

Object-Oriented Features
|
111
System.ValueType. They include primitive types like integer,
long, double, decimal, bool or Boolean, etc., structures, and
enumerated types. Reference types are all other classes. They
include classes that are derived directly or indirectly from
System.Object. The String class is actually a reference type
that in some ways behaves like a value type.
One difference between value and reference types is the way
in which their memory is allocated and deallocated. Another
difference is the way in which the compiler interprets com-
parisons of type members.
To test two objects for equality in C#, you can use the
==
operator (two equals signs). For example:
class RefPerson
{
int Age;
string Name;
public RefPerson(string Name, int Age)
{
this.Name = Name;
this.Age = Age;
}
}
class Class1
{
static void Main(string[] args)
{
RefPerson pr1 = new RefPerson("Jose",12);
RefPerson pr2 = new RefPerson("Jose",12);
if (pr1 == pr2)
System.Console.WriteLine("Equal");
}
}
This code example defines the RefPerson class. RefPerson is
a reference type—it inherits from System.Object directly.
The class has two fields, Age and Name. It also has a single
constructor that requires a name and an age to be passed in
as arguments. The class Class1 has a Main procedure that
C#

112
|
C# & VB.NET Conversion Pocket Reference
creates two instances of the RefPerson class, both of which
have the same values for name (Jose) and age (12). The cru-
cial part of the example is the comparison using the
== opera-
tor. It turns out that with the above definition of the class,
the C# compiler translates
== to an identity check rather
than an equality check. An identity check only takes into con-
sideration where the objects live in memory. In other words,
“Is pr1 pointing to the same memory location as pr2?” This
would be true only if pr1 and pr2 point to the same object. In
this case, because the code creates two instances of the Ref-
Person class, that can never be true.
The equivalent of
== in VB is the = operator. However, in VB
there is no way to compare two reference objects using the
=
operator. To compare for identity (two variables pointing to
the same object), you must use the
Is operator. Thus, this
C# code can be translated to VB as follows:
Class RefPerson
Dim Age As Integer
Dim Name As String
Sub New(ByVal Name As String, ByVal Age As Integer)
Me.Name = Name
Me.Age = Age
End Sub
End Class
Class Class1
Shared Sub Main( )
Dim pr1 As New RefPerson("Jose",12)
Dim pr2 As New RefPerson("Jose",12)
If pr1 Is pr2 Then
System.Console.WriteLine("Equal")
End If
End Sub
End Class
The Is operator in VB is used to test identity. Just like in the
C# example above, the code will not report that
pr1 Is pr2
is true, since both variables point to unique objects.
VB
Get C# & VB.NET Conversion Pocket Reference now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.