33.5 Class, Structure, and Interface Members

33.5 Class, Structure, and Interface Members


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 FromResource method, when converted to VB, has the following syntax:

 Public Shared Function ToString(value As Long) As String 

33.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 33-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 

33.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#:

 public void AcceptChanges(  ); 

C# methods that return void are expressed as subprocedures in VB. Here's the corresponding syntax of the AcceptChanges method:

 Public Sub AcceptChanges(  ) 

All C# methods other than those returning void are functions in VB. The function's return value appears in an As clause at the end of the function declaration. C# data types and their VB equivalents are shown in Table 33-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 Copy method of the DataSet class has the C# syntax:

 public bool Copy(  ); 

The VB equivalent is:

 Public Function Copy(  ) As Boolean 
Table 33-3. C# data types and their VB equivalents

C# data type

VB data type











































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 < data_type > is any of the data types listed in Table 33-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 DataTable class's Select method has the following syntax in C#:

 public DataRow[] Select(string filterExpression, string sort,    DataViewRowState recordStates); 

Its 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, _      recordstates:=DataViewRowState.CurrentRows) 

C# also uses a number of object-oriented qualifiers with methods. These, and their VB equivalents, are shown in Table 33-4.

Table 33-4. C# keywords used with methods and their VB equivalents

C# keyword

VB keyword









In both C# and VB, constructors have a special syntax. In C#, constructors have the same name as the classes whose objects they instantiate and don't indicate a return value. For example, the default constructor for the SqlCommand class is:

 public SqlCommand(  ); 

In VB, the constructor is represented by a call to a class's New subprocedure. The equivalent call to the SqlCommand class constructor in VB is:

 Public Sub New(  ) 

33.5.3 Properties

The SqlCommand.CommandText property provides a more or less typical example of a property definition using C# syntax:

 public string CommandText {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 CommandText As String 

Note that read-write properties aren't 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.

Note that properties, like methods, can use the object-oriented modifiers listed in Table 33-4.

33.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 and the VB Event keywords can be preceded by the object modifiers listed in Table 33-4.

ADO. NET in a Nutshell
ADO.NET in a Nutshell
ISBN: 0596003617
EAN: 2147483647
Year: 2005
Pages: 415

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net