- •maranGraphics
- •CREDITS
- •ACKNOWLEDGMENTS
- •ABOUT THE AUTHORS
- •AUTHORS’ ACKNOWLEDGMENTS
- •TABLE OF CONTENTS
- •HOW TO USE THIS BOOK
- •INTRODUCTION TO C#
- •START VISUAL STUDIO .NET
- •OPEN A NEW C# PROJECT
- •OPEN A C# WEB PROJECT
- •SET JSCRIPT .NET AS THE DEFAULT SCRIPT LANGUAGE
- •EXPLORE THE CLASS VIEW WINDOW
- •VIEW THE CONTENTS WINDOW
- •GET HELP USING THE INDEX WINDOW
- •SEARCH FOR HELP
- •ADD COMPONENTS FROM THE TOOLBOX
- •ADD A TASK TO THE TASK LIST
- •CHANGE FORM PROPERTIES IN THE PROPERTIES WINDOW
- •ADD A CUSTOM TOOLBAR
- •DELETE A TOOLBAR
- •CHANGE THE VISUAL STUDIO ENVIRONMENT
- •MANAGE OPEN WINDOWS
- •OPEN A PROJECT
- •VIEW THE MAIN METHOD
- •COMBINE PROGRAM TYPES
- •ADD REFERENCE TYPES
- •ADD OPERATORS
- •INSERT ATTRIBUTES
- •ENTER CLASSES
- •ADD COMMENTS TO CODE
- •WRITE YOUR FIRST PROGRAM
- •ENTER XML DOCUMENTATION
- •ACCESS DOCUMENTATION
- •LOG A BUG REPORT
- •VIEW INFORMATION ABOUT C# BUILDING BLOCKS
- •PROGRAM CLASSES
- •ADD A CLASS
- •EMPLOY CLASS INHERITANCE
- •PROGRAM INSTANCE CONSTRUCTORS
- •INSERT DESTRUCTORS
- •PROGRAM STRUCTS
- •DISPLAY HEAP AND STACK INFORMATION
- •FIND TYPE INFORMATION
- •PROGRAM CONSTANT EXPRESSIONS
- •SPECIFY VALUE TYPES
- •PROGRAM NUMERIC TYPES
- •PROGRAM THE BOOLEAN TYPE
- •DECLARE REFERENCE TYPES
- •ENTER REFERENCE TYPE DECLARATIONS
- •CONVERT VALUE TYPES TO REFERENCE TYPES
- •PROGRAM POINTER TYPES
- •INSERT THE VOID TYPE
- •ADD INTERFACE PROPERTIES
- •ADD AN INTERFACE INDEX
- •VIEW INFORMATION ABOUT METHODS
- •ADD A METHOD
- •ADD STATIC METHODS
- •INCLUDE NON-STATIC METHODS
- •ENTER DELEGATES
- •PROGRAM EVENTS
- •ADD AN EVENT-HANDLING METHOD
- •VIEW INFORMATION ABOUT ARRAYS
- •ENTER SINGLE-DIMENSIONAL ARRAYS
- •ADD MULTIDIMENSIONAL ARRAYS
- •PROGRAM ARRAY-OF-ARRAYS
- •ITERATE THROUGH ARRAY ELEMENTS
- •SORT ARRAYS
- •SEARCH ARRAYS
- •IMPLEMENT A COLLECTIONS CLASS
- •PROGRAM STRUCTS
- •ADD AN INDEXER
- •INCLUDE ENUMERATIONS
- •CREATE STRING LITERALS AND VARIABLES
- •ASSIGN VALUES TO STRINGS
- •CONCATENATE STRINGS
- •COMPARE STRINGS
- •SEARCH FOR SUBSTRINGS
- •REPLACE CHARACTERS
- •EXTRACT SUBSTRINGS
- •CHANGE THE CHARACTER CASE
- •TRIM SPACES
- •REMOVE CHARACTERS
- •SPLIT A STRING
- •JOIN STRINGS
- •PAD STRINGS
- •VIEW INFORMATION ABOUT PROPERTIES
- •COMPARE PROPERTIES AND INDEXERS
- •PROGRAM PROPERTY ACCESSORS
- •DECLARE ABSTRACT PROPERTIES
- •INCLUDE PROPERTIES ON INTERFACES
- •VIEW INFORMATION ABOUT WINDOWS FORMS
- •ADD A WINDOWS FORM IN THE WINDOWS FORM DESIGNER
- •SET THE FORM TYPE
- •CHOOSE THE STARTUP WINDOWS FORM
- •CREATE A MODAL FORM
- •LAYOUT A FORM
- •SET A FORM LOCATION
- •CHANGE FORM PROPERTIES
- •CREATE A TRANSPARENT FORM
- •AN INTRODUCTION TO WEB FORMS AND CONTROLS
- •CREATE AN ASP.NET WEB SITE
- •CREATE A WEB FORM
- •ADD SERVER CONTROLS TO A WEB FORM
- •READ AND CHANGE PROPERTIES FROM OBJECTS ON A WEB FORM
- •USING SERVER-SIDE COMPONENTS ON WEB FORMS
- •INTRODUCING DATA ACCESS WITH ADO.NET
- •DISPLAY DATA WITH THE DATAGRID CONTROL
- •CONFIGURE THE DATAGRID CONTROL
- •INSERT DATA INTO A SQL DATABASE
- •UPDATE DATA FROM A SQL DATABASE
- •DELETE DATA FROM A SQL DATABASE
- •EXECUTE A STORED PROCEDURE IN A SQL DATABASE
- •READ XML FROM A FILE
- •SAVE XML TO A FILE
- •QUERY XML WITH XPATH
- •APPLY XSL TO XML
- •INTRODUCTION TO DISTRIBUTED APPLICATIONS
- •CREATE AN APPLICATION WITH PRIVATE ASSEMBLIES
- •CREATE AN APPLICATION WITH SHARED ASSEMBLIES
- •VERSION A SHARED ASSEMBLY
- •CONFIGURE A CLIENT FOR A VERSIONED ASSEMBLY
- •CREATE A WEB SERVICE
- •USING A WEB SERVICE
- •INTRODUCTION TO EXCEPTION HANDLING
- •THROWING AN EXCEPTION
- •HANDLING EXCEPTIONS WITH THE CATCH BLOCK
- •USING THE FINALLY BLOCK
- •WRITE ERRORS TO THE APPLICATION LOG
- •BASIC EXAMPLES
- •WHAT’S ON THE CD-ROM
- •USING THE E-VERSION OF THIS BOOK
- •INDEX
- •Symbols & Numbers
C#
ADD AN INTERFACE INDEX
ike interfaces, you can add an interface index in a class. LThe MDE window lets you create an interface index in
your class. After you finish with the wizard, the index code will appear in the MDE window so you can edit it to your satisfaction.
An indexer works very much like an array, but the difference is that an array stores values and an indexer stores implementation information contained in one or more interfaces in your class. The interface index helps your program categorize and obtain interface information more quickly. This means that indexers are your friends because they increase the performance of your program.
The C# Indexer Wizard is a window containing several fields in which you can enter interface indexer information. This includes drop-down lists of default information that you can choose from and adding index parameters (such as the index name) to your index.
Visual C# bases the interface indexer type on the value or reference type that appears in your class. This lets you tie into the type of value that you want the index to affect. For example, if you have a variable with a byte value that you want the interface index to add its information to, be sure the indexer type is byte.
ADD AN INTERFACE INDEX
Console |
Applicatio |
Click the Console
Application icon in the
Templates pane.
Type a name for the file.
Click OK.
Á Click the Class View tab in the Solution Explorer window.
■ The Class View window appears.
Class View - Indexer
Class1
‡ Click the plus sign (+) to expand the tree until you reach the Class1 entry.
Note: You can also view the Class View window by pressing Ctrl+Shift+C on your keyboard.
WORKING WITH TYPES AND INTERFACES 5
The public indexer access option in the C# Indexer 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.
Class View - Indexer
Class1
|
Add |
|
Add Indexer... |
° Right-click Class1. |
· Click Add. |
|
‚ Click Add Indexer. |
public
■ The C# Indexer Wizard window appears.
— Click to select the indexer access type from the drop-down list.
CONTINUED
113
C#
ADD AN INTERFACE INDEX
fter you enter the indexer access level and type Ainformation, 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 (CONTINUED)
int
int
¡ Click to select the indexer parameter type from the drop-down list.
WORKING WITH TYPES AND INTERFACES 5
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.
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.
115