Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
CSharp - Your Visual Blueprint For Building .NET Applications (2002) [eng].pdf
Скачиваний:
32
Добавлен:
16.08.2013
Размер:
9.71 Mб
Скачать

C#

ADD AN INDEXER

An indexer gives your class the ability to behave as an array. If you have a class with many elements, then an indexer lets you sort that information so

your program can get the element it needs from your class.

C# gives you two methods for adding an indexer to a class or an interface. You can add the indexer directly into your program or, if you add a class to your interface, you can add it using the Add C# Interface Indexer Wizard.

Class and interface index accessors come in two forms: get and set. The get accessor returns the type of the indexer.

The set accessor sets the value of the accessor type. The get and set accessors use the same access modifiers as the indexer declaration itself; the access modifiers for get and set must be as accessible as the indexer itself.

You can add an indexer to an interface through the Add C# Interface Indexer Wizard in the Class View window. The Add C# Interface Indexer Wizard contains fields so you can enter the indexer type, the parameter type, the parameter name, and any comments. After you finish entering data into the wizard, C# will create the skeleton of the indexer for you so you can add the indexer accessors.

ADD AN INDEXER

Class View - Ind...

The New Project window

.NET appears.

 

 

Click the Console

 

 

 

 

Application icon in the

.

 

Templates pane.

 

 

Type a name for the file.

 

 

 

 

ˇ Click OK.

 

 

Á Click the Class View tab.

Click the plus sign () next to the method name.

° Click the plus sign () next to the {} method name.

· Right-click the class name to open the pop-up menu.

Click Add.

Click Add Indexer.

If you declare more than one indexer in the same class or interface, then the signature for each index must be unique.

TYPE THIS:

using System; class Indexer

{

private int [] Array1 = new int[20]; public int this [int Index]

{

get

{

if (index < 0 | | index >= 20) return 0;

}

set

{

if (!(index < 0 | | index >= 20)) Array1[index] = amount;

}

}

public int [] Array2 = new int[50]; public int this [int Index]

USING ARRAYS 7

RESULT:

You will get an error and your program will not run because you cannot have the same index signature (Index).

int

int Parameter1

The C# Indexer Wizard window appears.

± Type the indexer parameter name in the Parameter name field.

¡ Click Add.

The parameter appears in the parameter list field.

Add an indexer comment in the Comment field.

£ Click Finish.

The indexer skeleton code appears in the parent window.

¢ Save the program as the filename.

155

C#

INCLUDE ENUMERATIONS

Enumerations are value types that assign numerical values to elements in an array. By assigning numerical values to elements, enumerations let you acquire those

elements quickly for further processing.

C# assigns the first element in the array the number zero (0) and each successive element in the array receives a successive number. For example, if you enumerate an array with the 12 months of the year, January will receive the number 0 and C# will continue until the end of the array when December gets the number 11.

An enumeration is a special type of array that you declare using the enum keyword. Like an array, you can set

accessibility attributes and access modifiers. The enum elements appear within curly brackets ({}) separated by commas just as array elements do. The key difference between an enumeration and an array is that an enumeration can only be of an integral type, and the default integral type is int. Because enumerations only assign integers to their elements, the only integral type that you cannot include is the char type.

You can change the enumeration value by assigning a number to the first value in the element list, and all successive elements in the list will receive successive numbers. For example, if you give January the number 1, then C# assigns December the number 12.

INCLUDE ENUMERATIONS

Properties

The Start page appears.

The New Project window

 

¤ Click New Project.

appears.

 

 

Click the Console

 

 

 

 

 

 

 

 

Application icon in the

 

 

Templates pane.

Type a name for the file.

ˇ Click OK.

USING ARRAYS 7

You can convert the enumeration type to an integral type — for example, to equate a string in the enumeration with an integer for tracking purposes.

 

TYPE THIS:

 

 

RESULT:

 

 

 

 

 

 

 

 

 

 

using System;

 

 

March = 1

 

 

public class Convert;

 

 

 

 

 

{

 

 

 

 

 

enum SpringMonths {Mar=1, Apr, May, Jun};

 

 

 

 

 

public static void Main()

 

 

 

 

 

{

 

 

 

 

 

int a = (int) SpringMonths.Mar //converts the Mar

 

 

 

 

 

value (1) to an integer

 

 

 

 

 

Console.WriteLine(“March = {0}”, a);

 

 

 

 

 

}

 

 

 

 

 

}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Á Type the code that establishes the enumeration, sets the value, and outputs the value to the screen.

Run the program by

° Save the program as the

pressing the F5 key.

filename.

The enumeration number

 

appears with its proper

 

season.

 

157

Соседние файлы в предмете Программирование на C++