- •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 STATIC METHODS
Astatic method maintains its information regardless of how many class instances are created; you can use static methods for maintaining a value such as the
boiling temperature of water. Like classes, methods are either static or instance members of the class. A static method contains information that will remain constant so the class can use it repeatedly. This is useful when you want to make calculations in your class with a value that is always constant.
You must explicitly include the static option before typing in the method keyword in your code. If you do not, then C# will automatically consider the method to be non-static. This chapter discusses non-static methods in greater detail later on.
If you declare a static modifier with your method, then you cannot also include a virtual, abstract, or override modifier. If you try to, the MDE window will point out the error and your project will not compile. The static modifier remains with that class and only with that class — it does not rely on any methods in any other inheriting or base class. Because virtual, abstract, and override modifiers deal with inheriting classes, they do not apply to static modifiers.
You cannot access static members through object instances that occur when you run your project. That is what nonstatic methods are for. You can access static methods through both value and reference types.
ADD STATIC METHODS
Visual C# Projects
Console
Application
■ 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
If you need to return more than one variable from your static method, you can do so using the params keyword.
|
TYPE THIS: |
|
|
RESULT: |
|
|
|
||||
|
using System; |
|
|
10 |
|
|
public class Params |
|
|
15 |
|
|
{ |
|
|
20 |
|
|
public static void Parameter(params int[] list) |
|
|
|
|
|
{ |
|
|
|
|
|
for ( int x = 0 ; x < list.Length ; x++ |
|
|
|
|
|
) |
|
|
|
|
|
Console.WriteLine(list[x]); |
|
|
|
|
|
Console.WriteLine(); |
|
|
|
|
|
} |
|
|
|
|
|
public static void Main() |
|
|
|
|
|
{ |
|
|
|
|
|
Parameter(10, 15, 20); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
Class1
void
int
Add Method |
Add |
Á Click the Class View tab. |
|
· Right-click the class |
■ The C# Method Wizard |
|||||
|
||||||||
|
‡ Click the plus sign ( |
) |
name. |
appears. |
||||
|
|
|
‚ Click Add Add Method. |
|
— Type the method name in |
|||
next to the Method name. |
|
|
|
|||||
|
|
|
||||||
|
|
° Click the plus sign ( |
) |
|
|
|
the Method name field. |
|
|
|
|
|
|
|
|
next to the {} Method name.
StaticMethod
Note: The Method signature field at the bottom reflects the changes to the method code as you type information into the wizard fields.
CONTINUED
123
C#
ADD STATIC METHODS
C# uses simple names for accessing many different elements in a C# project, and methods are no different. However, if you have a static method then
how you program static methods and other static information in your method determines if you can use simple names or not.
Simple names for a variable can be just one letter, such as x.
When you declare variables and associate them with value types, the methods you include those declarations in determine whether your program can process those variables. For example, you can declare two variables of integers with the simple names a and b, with a declared as a non-static member and b declared as a static member.
If you place the two variables in a non-static method and evaluate them later in your class, you will have no trouble with your evaluation. However, if you put those two variables in a static method you will only be able to evaluate the static variable b because a static method cannot access a non-static variable.
If you decide to plunge ahead anyway and try to evaluate a non-static variable in a static method, you will find that the MDE window will protest that action and your program will not compile until you fix the problem.
ADD STATIC METHODS (CONTINUED)
Class1.cs
select a method |
■ The static method code |
|
the Method |
appears in the parent |
|
check box area. |
window. |
|
. |
|
¢ Move the static |
|
||
|
method code above the Main |
|
|
method code. |
∞ Type the Record class code that establishes variables and static methods for adding to the number of records.
PROGRAMMING METHODS AND EVENTS 6
You can reference a static method in what Visual Studio .NET refers to as a member-access format. The member-access format contains the full version of the type and its associated identifier. The member-access format comes in the form of the type, a period, and then the identifier. For example, you can have the member access type int.number.
C# and the Visual Studio .NET suite do not force you to use the member-access format because many of the access types have aliases that C# refers to. If you reference your static method (or any other static member) in member-access form you must do so in the form E.M. The E must stand for the type that your method uses, not the object. The M stands for the type identifier. For example, if your method is of the type integer with the name NumberOne, then the member access form is int.NumberOne.
§ Type the Main method that lets the user input values and outputs the results.
¶ Press the F5 key. |
• Save the program as the |
■ Type information at the |
filename. |
|
|
prompts and the output |
|
appears. |
|
125