- •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#
ENTER DELEGATES
Delegates act like pointers that you find in other languages such as C++, but delegates go several steps further in C#. Delegates provide object-
oriented pointers to methods from other points in your project. This approach makes it easy for methods throughout your program to retrieve information from one source without having to enter that information repeatedly.
Delegates provide two key benefits. Delegates act as a central point where all pieces of your code that need objects refer to a specific method. It is quite inconvenient to have to write static methods for many different classes.
It is also inconvenient to refer to the same class for the same method, and that approach can also slow your project down when it runs. It is much more efficient to have one or a few delegates that can handle method operations for your entire project.
The second benefit of delegates is anonymity. The delegate does not care about what the method includes — whether it be static or non-static, what accessibility the method has, or any other information. The only thing the delegate cares about is if the method that it is looking for has the same signature as the delegate.
ENTER DELEGATES
Visual C# Projects
Console
Applications
|
■ The Start page appears. |
■ The New Project |
||
|
|
¤ Click New Project. |
window appears. |
|
|
|
|
‹ Click the Console |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Application icon in the |
|
|
|
|
Templates pane. |
› Type a name for the file.
ˇ Click OK.
PROGRAMMING METHODS AND EVENTS 6
You can combine existing delegates into a multi-cast delegate.
TYPE THIS:
using System;
delegate void Del1(string x) class Class1
{
public static void On(string x)
{
Console.WriteLine("Switch goes on.", x);
}
public static void Off(string x)
{
Console.WriteLine("Switch goes off.", x);
}
public static void Main();
{
Del1 a, b, c;
a = new Del1(On); b = new Del1(Off); c = a + b;
Console.WriteLine("The two switch states:"); c;
}
}
RESULT:
The two switch states:
Switch goes on.
Switch goes off.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Á Type the Main method |
|
|
|
|
|
|
‡ Type the code for the |
|||||
|
||||||
|
code that outputs the values. |
methods that specify and |
||||
|
|
|
output the delegate value. |
° Press the F5 key. |
· Save the program as the |
■ The delegate output appears. |
filename. |
|
131
C#
PROGRAM EVENTS
In object-oriented programming, an event lets clients of that class — clients can include delegates, other classes, methods, and indexers — know about something that
happens to an object in a class. That something that happens is of great interest to the clients in the class and so the event lets those clients know about it and act accordingly.
By acting accordingly, the clients give the class delegates so the delegates can retrain those objects using the modules called by those delegates. Once the appropriate module retrains the changed object to behave properly, the object goes back to the class for further processing.
When you declare an event inside of a class you must declare the delegate inside the event. The class that the event resides in is the only class that calls the event. When the class calls the event, the class checks to see if a client has hooked up a delegate to the event, and if that is true then the class processes the event.
The previous task mentioned that C# declares events using delegates. If you have come to this task wanting to learn about events but you have not learned about delegates yet, skip back four pages and read about delegates before you continue on.
PROGRAM EVENTS
Console
Applications
■ The New Project
NET |
|
window appears. |
||
|
|
‹ Click the Console |
||
|
|
|||
|
|
Application icon in the |
||
. |
|
Templates pane. |
||
|
|
|
› Type a name for the file. |
|
|
|
|
||
|
|
|
|
ˇ Click OK. |
|
|
|
|
Á Type the code that establishes your event delegate, interface, and class.
PROGRAMMING METHODS AND EVENTS 6
The facts that events can only be called from the classes they reside in and that classes can be inherited poses an interesting problem. C# does not let you invoke events in a base class from an inheriting class. This seems to defeat the purpose of having a class inherit all information from your base class. However, C# does offer a workaround to this problem.
C# can have an inheriting class call a base class event by creating a protected invoking method within the event. This method invokes the base class event and the project passes along the information from that base class event to the rest of the event. If you would rather not have the base class send its events, you can have this protected invoking method as a virtual method. An overriding method in an inheriting class can then take over from the virtual method and shut down the base class events.
‡ Type the code for the Output class that outputs the string when the event fires.
° Press the F5 key. |
· Save the program as the |
■ The string appears on the |
filename. |
|
|
screen when the event fires. |
|
133