Danh mục tài liệu

Class, Structure, and Interface Members

Số trang: 5      Loại file: pdf      Dung lượng: 21.83 KB      Lượt xem: 18      Lượt tải: 0    
Xem trước 2 trang đầu tiên của tài liệu này:

Thông tin tài liệu:

[ 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
Nội dung trích xuất từ tài liệu:
Class, Structure, and Interface Members [ Team LiB ]A.5 Class, Structure, and Interface MembersClasses, structures, and interfaces can contain one or more fields, methods, properties,and events. This section discusses converting the C# syntax for each of these constructsto Visual Basic.Note that .NET supports both static (or shared) members (which apply to the type as awhole, and typically dont require that an object of that type be instantiated) and instancemembers (which apply only to an instance of that type). Shared or static members areindicated 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 toVB, has the following syntax:Public Shared Function ToString(value As Long) As StringA.5.1 FieldsA field is simply a constant or a variable that is exposed as a publicly accessible memberof 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# datatypes 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# readonlykeyword 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 DBNullA.5.2 MethodsIn C#, all methods have a return value, which appears before the name of the function; incontrast, VB differentiates between function and subprocedures. C# functions without anexplicit return value return void. For example, one of the overloads of the DataSet classsAcceptChanges method has the following syntax in C#:public void AcceptChanges( );C# methods that return void are expressed as subprocedures in VB. Heres thecorresponding syntax of the AcceptChanges method:Public Sub AcceptChanges( )All C# methods other than those returning void are functions in VB. The functions returnvalue appears in an As clause at the end of the function declaration. C# data types andtheir VB equivalents are shown in Table A-3. Methods that return arrays are indicated byadding brackets ([ ]), to the return data type in C# and parentheses, ( ), to the return datatype in VB. Table A-3. C# data types and their VB equivalents C# data type VB data typebool Booleanbyte Bytechar Chardecimal Decimaldouble Doublefloat Singleint Integerlong Longobject Objectsbyte System.SByteshort Shortstring StringSystem.Currency CurrencySystem.DateTime Dateuint System.UInt32ulong System.UInt64ushort System.UInt16 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 isan array, its data type is followed by brackets in C#, such as string[] Name, while in VBthe 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 followingsyntax 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)C# also uses a number of object-oriented qualifiers with methods. These, and their VBequivalents, are shown in Table A-4. Table A-4. C# keywords used with methods and their VB equivalents C# keyword VB keywordabstract MustOverrideoverride Overridessealed NotOverridablevirtual OverridableIn both C# and VB, constructors have a special syntax. In C#, constructors have the samename as the classes whose objects they instantiate and they dont 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. Theequivalent call to the SqlCommand class constructor in VB is:Public Sub New( )A.5.3 PropertiesThe SqlComman ...