Tài liệu Class, Structure, and Interface Members - Pdf 87

[ Team LiB ]A.5 Class, Structure, and Interface Members
Classes, structures, and interfaces can contain one or more fields, methods, properties,
and events. This section discusses converting the C# syntax for each of these constructs
to Visual Basic.
Note that .NET supports both static (or shared) members (which apply to the type as a
whole, and typically don't 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 string ToString(long value);
The corresponding VB keyword is Shared, so the ToString method, when converted to
VB, has the following syntax:
Public Shared Function ToString(value As Long) As String
A.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 Value field of the System.DBNull class has the syntax:
public static readonly DBNull Value;
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 A-3
.) Also note that fields are frequently 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 Value field in Visual Basic then becomes:
Public Shared ReadOnly Value As DBNull
A.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 DataSet class's
AcceptChanges method has the following syntax in C#:

<delegate_name> <delegate_name>
<interface_name> <interface_name>
<structure_name> <structure_name>
For example, a method that returns an array would look like this in C#:
public int[ ] ReturnsArray( );
The VB equivalent is:
Public Function ReturnsArray( ) as Integer( )
Method parameters in C# take the general form:
<data_type> <parameter_name>
In VB, method parameters take the form:
<parameter_name> As <data_type>
where the <data_type> will be any of the data types listed in Table A-3
. If a parameter is
an array, its data type is followed by brackets in C#, such as string[] Name, while in VB
the parameter name is followed by parentheses in VB, such as Name( ) As String.
For example, one of the versions of the DataTable class's Select method has the following
syntax in C#:
public DataRow[] Select(string filterExpression, string sort,
DataViewRowState recordStates);
The VB equivalent is:
Overloads Public Function Select(ByVal filterExpression As String, _
ByVal sort As String, ByVal recordStates As DataViewRowState _
) As DataRow( )

VB allows methods to be called using either named and positional
parameters. If named parameters are used, the parameter name must
correspond to that shown in the documentation. For instance,
DataTable.Select can be called as follows using named parameters:
dr = DataTable.Select(filterexpression:=flt, _
sort:=sd, _

the Property keyword, while write-only properties have the WriteOnly keyword before
the Property keyword.
Note that properties, like methods, can use the object-oriented modifiers listed in Table
A-4.
A.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 RowUpdated
event of the SqlDataAdapter class has the following syntax:
public event SqlRowUpdatedEventHandler RowUpdated;
The equivalent VB syntax is:
Public Event RowUpdated As SqlRowUpdatedEventHandler
In addition, the C# event keyword and the VB Event keyword can be preceded by the
object modifiers listed in Table A-4
.
[ Team LiB ]


Nhờ tải bản gốc
Music ♫

Copyright: Tài liệu đại học © DMCA.com Protection Status