
- •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#
VIEW INFORMATION ABOUT METHODS
Amethod 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.
VIEW INFORMATION ABOUT METHODS
Index |
Crtl+Alt+F2 |
⁄ Click Start Programs |
■ The Start page appears. |
|
¤ Click Help. |
|
|
‹ Click Index. |
|
|
|||||
Microsoft Visual Studio .NET |
|
|
|
|
|
|
7.0 Microsoft Visual Studio |
|
|
|
|
|
|
.NET 7.0. |
|
|
|
|
|
|

PROGRAMMING METHODS AND EVENTS 6
When you add a new method, you can have several methods with the same name with different signatures in the same class. However, if you try to add a method and another class type such as an interface with the same name, the MDE window would register an error and the compiler would not be able to run the program. If you have the same name for the method and interface but the method and interface were in separate classes, then C# would have no problem.
Though C# looks for the module name and the formal parameter list when determining a module signature, it does not look for the return type or the names of the parameters. So if you receive an error from the MDE window about signatures, check to see that your module names and lists are different for each module.
methods, adding in
adding in C# |
adding in C# |
■ The Index window appears.
› Type methods in the Look for field.
ˇ Click the adding in C# entry in the Index list box.
■ The C# Add Method Wizard appears so you can learn about adding methods.
117

C#
ADD A METHOD
As with a property and an indexer, C# gives you two ways to add a method. If you like the step-by-step functionality provided by a wizard, the C# Add
Method Wizard lets you add a method automatically. You can also add a method in code.
When you add a method in code you start with the method keyword. You can add information that precedes the keyword: whether the method is static or non-static (the default is non-static) and whether the method contains a void type. The void type renders the method invisible where it takes on no parameters and returns no values.
After you enter the method keyword, you can enter the optional method declarations. These declarations include various attributes, method modifiers, the return type, and then the name of the method itself. Then you begin to add the information within your method.
Attributes include names that you can enter in your class and refer to in your method. An attribute is a good way to identify information that you want to include in your class such as your company Web site. The method modifiers help determine the access to your method from your class and other code in your project.
ADD A METHOD
Visual C# Projects
Console
Application
|
■ The Start page appears. |
■ The New Project window |
||
.NET |
|
¤ Click New Project. |
appears. |
|
Studio |
|
|
‹ Click the Console |
|
|
|
|
||
|
|
|
|
|
|
|
|
Application icon in the |
|
|
|
|
Templates pane. |
› Type a name for the file.
ˇ Click OK.

PROGRAMMING METHODS AND EVENTS 6
You use the return keyword in all methods except one: the void type. When you specify the void method type, you do not need to include the return keyword because the return type is automatically void.
|
TYPE THIS: |
|
|
RESULT: |
|
|
|||
|
using System; |
|
|
The diameter is 25 |
|
class VoidTest |
|
|
|
|
{ |
|
|
|
|
public static void Main() |
|
|
|
|
{ |
|
|
|
|
int diameter = 25; |
|
|
|
|
Console.WriteLine("The diameter is {0}", diameter); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
Class View - M. . .
Method
{}Method
Class1
Public |
Void |
int
Add Method . . . |
Add |
Á Click the Class View tab. |
|
· Right-click the class name |
■ The C# Method Wizard |
||||
|
|||||||
|
‡ Click the plus sign ( |
) |
|
to open the pop-up menu. |
window appears. |
||
|
|
|
‚ Click Add Add Method. |
|
|||
|
next to the Method name. |
|
|
|
|||
|
|
|
|
||||
|
° Click the plus sign ( |
) |
|
|
|
|
|
|
|
|
|
|
|||
|
next to the {} Method name. |
|
|
|
|
CONTINUED
119

C#
ADD A METHOD
After you include the attributes and method access modifiers, you can further define your method using several different modifiers.
If your method resides in an inheriting class and you also have a modifier in your base class, you can disregard the method in the base class by adding the new keyword. Using the new keyword in your method effectively hides the base class method so your class only pays attention to the method in its class.
You can determine if the method will have the static, virtual, override, abstract, or extern status. A static method lets all objects in its class access it. You can use a virtual
method in an inheriting class; a virtual method checks to see if any methods in any related class must override that method. An override method tells that method to override any methods in any related classes. The abstract method introduces a new virtual method but acts as a placeholder for a different method in a related class that will override the abstract method. The extern modifier lets you create an external method.
Once you add the modifier you can determine the return type and then enter the name of the method. After you add the method name you can begin work on the body of your method.
ADD A METHOD (CONTINUED)
void
int |
Int Parameter1 |
The Method signature field at bottom reflects the changes to method code as you type
information into the wizard fields.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
± Type the parameter name |
|
■ The added parameter |
|||||
|
|
|||||||
in the Parameter name field. |
name appears in the |
|||||||
|
|
¡ Click Add. |
Parameter list field. |
|||||
|
|
|
|
|

PROGRAMMING METHODS AND EVENTS 6
C# lets you return multiple values from one method by using the out parameter.
|
TYPE THIS: |
|
|
RESULT: |
|
|
|
||||
|
|
|
|
|
|
|
using System; |
|
|
0 |
|
|
public class OutTest |
|
|
25 |
|
|
{ |
|
|
|
|
|
public static int Output(out int a) |
|
|
|
|
|
{ |
|
|
|
|
|
a = 25; |
|
|
|
|
|
return 0; |
|
|
|
|
|
} |
|
|
|
|
|
public static void Main() |
|
|
|
|
|
{ |
|
|
|
|
|
int a; |
|
|
|
|
|
Console.WriteLine(Output(out a)); |
|
|
|
|
|
Console.WriteLine(a); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void
int
int Parameter1
™ Click to select a method modifier from the Method modifiers check box area.
£ Type a comment in the Comment field.
¢ Click Finish.
■ The method code appears in the parent window.
121