- •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 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