- •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#
INCLUDE NON-STATIC METHODS
The non-static status is the default for all methods. Non-static methods, also called instance methods, rely on an instance of the class — that is, the non-static
method relies on the information it receives from an object generated by the class. Once the non-static method receives that object it provides the object with its implementation instructions and sends the object back out into the class for further processing.
The non-static method is best if you know that the class will generate an object for the method. If you create a method in an inherited class, then the non-static method is the only
choice. A static method belongs to its class, but a non-static method can take objects from inheriting classes. You can also set non-static methods to override or be overridden by other non-static methods in other inherited classes in your class family or from the base class.
Your non-static method does not accept objects automatically. You must tell the method that you want to accept the value by using the this keyword. When you use the keyword this in your method, the referenced object receives a type that matches the object type and a value that acts as a reference to the object.
INCLUDE NON-STATIC METHODS
Visual C# Projects
Console
Applications
Start page appears. |
■ The New Project |
|
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 reference a non-static method with the member-access format so you can point directly to the member you want the method to call.
|
TYPE THIS: |
|
|
RESULT: |
|
|
|
||||
|
|
|
|
|
|
|
using System; |
|
|
First name? John |
|
|
public class Name { |
|
|
First name: John |
|
|
public string first; |
|
|
|
|
|
public string last;public Person () { } |
|
|
|
|
|
public Person (string first, string last) { |
|
|
|
|
|
this.first = first; |
|
|
|
|
|
this.last = last; |
|
|
|
|
|
} |
|
|
|
|
|
class Main: Person { |
|
|
|
|
|
public static void Main() { |
|
|
|
|
|
Console.Write("First name? "); |
|
|
|
|
|
String first = Console.ReadLine(); //accepts input |
|
|
|
|
|
Person a = new Person (name, id); |
|
|
|
|
|
Console.WriteLine("First name: {0}", a.first); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
Class View - N...
Class1
void
int
Add Method |
Add |
Á Click the Class View tab. |
|
· Right-click the |
■ The C# Method |
Note: The Method signature field at |
|||||||
|
|||||||||||
|
|
‡ Click the plus sign ( |
) |
class name. |
Wizard appears. |
the bottom reflects the changes to |
|||||
|
|
|
‚ Click Add Add Method. |
|
— Type the method name in |
the method code as you type |
|||||
next to the Method name. |
|
|
|
||||||||
|
|
information into the wizard fields. |
|||||||||
|
° Click the plus sign ( |
) |
|
|
|
the Method name field. |
|||||
|
|
|
|
|
|
|
|||||
|
|
|
|
|
|
|
CONTINUED |
|
|||
next to the {} Method name. |
|
|
|
|
|
|
|
||||
|
|
|
|
|
|
|
|
127
C#
INCLUDE NON-STATIC METHODS
When the non-static method processes the instance of a class, C# creates a copy of all instance (that is, object) fields for the method to process. This
ensures that a copy of the instance remains in the class while your class is being instructed by the non-static method. Once the object leaves the non-static method, the method-trained copy replaces the original that was in the class.
The earlier discussion in this chapter about static methods included information about simple names and how the declaration of those names can affect processing in a static method. With non-static methods the same rules apply.
If you try to evaluate a static variable in a non-static method, you will receive an error and the MDE window will prohibit you from compiling your program.
If you have a non-static method that another method in another inheriting class can override, be sure that your overriding non-static method can process the variables in your class correctly. If you do not, you may encounter processing errors because the new, overriding method may not be able to process all the variables in your class. The same holds true if you override a non-static method in another inheriting class.
INCLUDE NON-STATIC METHODS (CONTINUED)
|
|
|
£ Type the code for the |
|
■ The method code appears |
||
|
in the parent window. |
One method. |
|
|
™ Type code that establishes |
¢ Type code and an |
|
|
|||
|
the First class and move |
override method in the |
|
|
the method code within the |
Second class. |
|
|
First class. |
|
PROGRAMMING METHODS AND EVENTS 6
You can represent a class object in the memberaccess format as well for precise representation of your object. Though the member-access E.M format is the same as with static methods, the E cannot represent a type. Instead, the E must represent the class instance. Usually the member-access format does not include the identifier signified by M because the instance expression signified by E is all the information needed.
Another reason for using the member-access format is that you can perform a member lookup. A member lookup evaluates simplename or member-access forms in an expression or statement.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
∞ Rename the Class1 class |
|
|
§ Type the Main method |
|||
|
|
|
|||||
as Output. |
|
that outputs the values. |
¶ Press the F5 key. |
• Save the program as the |
■ The Second class |
filename. |
|
|
overrides the First class |
|
and produces two output |
|
lines of Second.One. |
|
129