intTypePromotion=1
zunia.vn Tuyển sinh 2024 dành cho Gen-Z zunia.vn zunia.vn
ADSENSE

Class, Structure, and Interface Members

Chia sẻ: Bui Tuan | Ngày: | Loại File: PDF | Số trang:5

83
lượt xem
6
download
 
  Download Vui lòng tải xuống để xem tài liệu đầy đủ

[ 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

Chủ đề:
Lưu

Nội dung Text: Class, Structure, and Interface Members

  1. [ 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#: public void AcceptChanges( );
  2. 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 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 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
  3. 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 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, _ recordstates:=DataViewRowState.CurrentRows)
  4. 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 abstract MustOverride override Overrides sealed NotOverridable virtual Overridable 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'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( ) 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 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 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'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
  5. 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 ]
ADSENSE

CÓ THỂ BẠN MUỐN DOWNLOAD

 

Đồng bộ tài khoản
2=>2