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 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 classs AcceptChanges method has the following syntax in C#:

public void AcceptChanges( );

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

Public Sub AcceptChanges( )

All C# methods other than those returning void are functions in VB. The functions 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 A-3. Methods that return arrays are indicated by adding brackets ( [ ] ), to the return data type in C# and parentheses, ( ) , to the return data type in VB.

Table A-3. C# data types and their VB equivalents

C# data type

VB data type



































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:



> <



In VB, method parameters take the form:



> As <



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 classs 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, _

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

Table A-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 they don 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 classs New subprocedure. The equivalent call to the SqlCommand class constructor in VB is:

Public Sub New( )

A.5.3 Properties

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

public string CommandText {get; set;}

Like all C# type definitions, the propertys 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 only a get; while write-only properties are indicated with only a set .

The equivalent VB property definition is:

Public Property CommandText As String

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

Connecting to Data

Retrieving and Managing Data

Searching and Analyzing Data

Adding and Modifying Data

Copying and Transferring Data

Maintaining Database Integrity

Binding Data to .NET User Interfaces

Working with XML

Optimizing .NET Data Access

Enumerating and Maintaining Database Objects

Appendix A. Converting from C# to VB Syntax

ADO. NET Cookbook
ADO.NET 3.5 Cookbook (Cookbooks (OReilly))
ISBN: 0596101406
EAN: 2147483647
Year: 2002
Pages: 222
Authors: Bill Hamilton

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