• No results found

ADD AN INTERFACE INDEX

In document C # (Page 126-130)

ADD AN INTERFACE INDEX

Class View - Indexer

public

° Right-click Class1. · Click Add.

‚ Click Add Indexer.

■ The C# Indexer Wizard window appears.

— Click to select the indexer access type from the drop-down list.

Add Indexer...

Add Class1

Wizard lets you select the access modifier as you do when you create an instance constructor. You have your choice from one of five access modifiers.

• The public modifier is the default setting; this ensures that your entire project, as well as any other program that accesses your project, can access your class.

• The protected modifier limits access to the members of the base class and any inheriting classes.

• The internal modifier limits access to any element in your project.

• The protected internal modifier limits access to elements in your project or to the members of the base class and any inheriting classes.

• The private modifier limits access to members only within the class.

The default accessibility option is public and that is the choice you see when you create a new interface index in the wizard.

CONTINUED

int

int

int

int

± Click to select one of the 15 indexer types from the drop-down list.

¡ Click to select the indexer parameter type from the drop-down list.

A

fter you enter the indexer access level and type information, you can enter parameters that determine what type and name the indexer will have. The type of indexer must be the same as the value or reference type that the interface index accesses. For example, if the reference type in the class is an object, you should give the interface index the object parameter type.

From there you can enter the parameter name from a list of parameters that meet the object type requirements. For example, if your interface index is an object, the list of available parameter names will be those in the class that associate with the object type. You can add and remove as many parameters from the indexer list as you want.

The indexer modifiers let you determine if the indexer will be a regular indexer or will have special instructions. For example, the virtual indexer will check to see if there is an inheriting class that has its own index that will override the index you are currently adding. If the inheriting class has an index with an override modifier, your class will use the override index instead.

You can also add comments to your index if you want information about your index present in your code.

ADD AN INTERFACE INDEX

ADD AN INTERFACE INDEX (CONTINUED)

int int Param1

™ Type the indexer parameter name(s) in the Parameter name field.

£ Add the name(s) to the Parameter list field by clicking the Add button.

¢ Click to select the indexer modifier from the Indexer modifiers area.

∞ Type a comment for your indexer.

§ Click the Finish button.

■ The indexer code skeleton appears in your class code so you can edit it.

Properties and indexers have some similarities — the most obvious is that all of the rules defined for the properties get and set accessors also apply to the indexer get and set accessors.

Although properties and indexers are related, you should be aware of some significant differences:

• Visual C# identifies a property by its name and an indexer by its signature.

• You can access a property with a simple name. You must access an indexer through an element.

• A property can have a static object that does not change. An indexer must contain instance information generated by the class.

• The get accessor of a property has no additional parameters. The get accessor of an indexer has the same parameters as the indexer.

• The set accessor of a property contains the implicit value parameter. The set accessor of an indexer has the value parameter and the additional indexer parameters.

A

method is a piece of code that implements an action that a class or object can perform. Methods appear within a class and provide additional information that classes cannot handle.

C# supports two types of methods: static and non-static. All objects in a class can access the static methods in that class without creating any instance of that class. Instances of a class can only access non-static methods. For more information on adding static and non-static methods, see pages 6 to 13.

You can overload methods, which means that different methods can have the same name as long as each separate

method has a unique signature. C# identifies a signature by looking for specific method features including the method name and the method parameters.

You can only add a method when you are editing a class.

When you program a method you can do so in one of two ways: in code or by using the C# Method Wizard. The C#

Method Wizard contains fields with basic method

information that you can enter and choose from. Once you finish entering information into the wizard, the basic method code information appears in your code so you can edit it.

In document C # (Page 126-130)

Related documents