22.5 Class, Structure, and Interface Members
Classes, structures, and interfaces can contain one or more fields,
methods, properties, and events. This section will discuss converting
the C# syntax for each of these constructs to VB.Note that .NET supports both static (or shared) members (which
apply to the type as a whole, and typically do not require that an
object of that type be instantiated) and
instance members (which
apply only to an instance of that type). Shared or static members are
indicated by using the static keyword in C#. For
example:
public static bool IsMnemonic(char charCode, string text);
The corresponding VB keyword is Shared. Hence, the
FromResource method, when converted to VB, has the
following syntax:
Public Shared Function IsMnemonic(charCode As Char, text As String) _
As Boolean
22.5.1 Fields
A field is simply a constant or a
variable that is exposed as a publicly accessible member of a type.
In C#, for example, the Nowhere field of the
DataGrid.HitTestInfo class has the syntax:
public static readonly DataGrid.HitTestInfo Nowhere;
Note that C# indicates the data type of a field before the name of
the field. (For C# data types and their VB equivalents, see Table 22-3.) Also note that fields are most often
read-only. Constant fields, in fact, are always read-only. As a
result, the use of the C# readonly keyword and the
VB ReadOnly keyword with fields is quite common.The syntax for the Nowhere field in Visual Basic
then becomes:
Public Shared ReadOnly Nowhere As DataGrid.HitTestInfo
22.5.2 Methods
In C#, all methods have a return
value, which appears before the name of the function; in contrast, VB
differentiates between function and subprocedures. C# functions
without an explicit return value return void. For
example, one of the overloads of the Bitmap class's
MakeTransparent method has the following syntax in
C#:
public void MakeTransparent( );
C# methods that return void are expressed as
subprocedures in VB. So the corresponding syntax of the
MakeTransparent method is:
Public Sub MakeTransparent( )
All C# methods other than those returning void are
functions in VB. The function's return value follows
appears in an As clause at the end of the function
declaration. C# data types and their VB equivalents are shown in
Table 22-3. Methods that return arrays are
indicated by adding braces ([ ]) to the return
data type in C# and parentheses (( ))to the return
data type in VB.For example, the Focus method of the
Control class has the C# syntax:
public bool Focus( );
The VB equivalent is:
Public Function Focus( ) As Boolean
C# data type | VB data type |
|---|---|
bool | Boolean |
byte | Byte |
char | Char |
decimal | Decimal |
double | Double |
float | Single |
int | Integer |
long | Long |
object | Object |
sbyte | System.SByte |
short | Short |
string | String |
System.Currency | Currency |
System.DateTime | Date |
uint | System.UInt32 |
ulong | System.UInt64 |
ushort | System.UInt16 |
<class_name > | <class_name > |
<delegate_name > | <delegate_name > |
<interface_name > | <interface_name > |
<structure_name > | <structure_name > |
<data_type> <parameter_name>
In VB, method parameters take the form:
<parameter_name> As <data_type>
where <data_type> is any of the data
types listed in Table 22-3. If a parameter is an
array, its data type is followed by braces in C# (e.g.,
string[ ] Name), while the parameter name is
followed by parentheses in VB (e.g., Name( ) As
String).For example, one of the versions of the Color
class's FromArgb method has the
following syntax in C#:
public static Color FromArgb(int red, int green, int blue);
Its VB equivalent is:
Public Shared Function FromArgb(red As Integer, _
green As Integer, _
blue As Integer) As Color
qualifiers with methods. These, and their VB equivalents, are shown
in Table 22-4.
C# keyword | VB keyword |
|---|---|
abstract | MustOverride |
override | Overrides |
sealed | NotOverridable |
virtual | Overridable |
C#, constructors have the same name as the classes whose objects they
instantiate and do not indicate a return value. For example, the
constructor for the Button class is:
public Button( );
In VB, the constructor is represented by a call to a
class's New subprocedure. The
equivalent call to the Button class constructor in
VB is:
Public Sub New( )
22.5.3 Properties
The
FileDialog.Title property provides a more or less
typical example of a property definition using C# syntax:
public string Title {get; set;}Like all C# type definitions, the property's data
type precedes the property name. The get; and
set; property accessors indicate that this is a
read-write property. Read-only properties are indicated with a
get; only, while write-only
properties are indicated with a
set; only.The equivalent VB property definition is:
Public Property Title As String
Note that read-write properties are not decorated with additional
keywords in VB. Read-only properties, on the other hand, are
indicated with the ReadOnly keyword in front of
the Property keyword, while write-only properties
have the WriteOnly keyword before the
Property keyword.The shared ProductName property of the
Application class is read-only. Its C# syntax
appears as follows:
public static string ProductName {get;}The corresponding VB syntax is:
Public Shared ReadOnly Property ProductName As String
Note that properties, like methods, can use the object-oriented
modifiers listed in Table 22-4.
22.5.4 Events
Events are declared in C# using
the event keyword, which is followed by the
delegate type returned by the event and the name of the event. For
example, the Parse event of the
Binding class has the following syntax:
public event ConvertEventHandler Parse;
The equivalent VB syntax is:
Public Event Parse As ConvertEventHandler
In addition, the C# event and the VB
Event keywords can be preceded by the object
modifiers listed in Table 22-4.

