- •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#
COMPARE PROPERTIES AND INDEXERS
n the surface it may seem like properties and Oindexers (indeed, properties and many other C#
features) have many similarities, and to some degree that is true. After all, it makes little sense to reinvent the wheel for every C# feature. However, properties and indexers do have some important differences.
Both properties and indexers use the get and set arguments. The get argument reads and returns a value of the property type. The get value can also compute a value
(such as converting Fahrenheit to Celsius) and return the computed value.
The set argument writes the property received from the get argument and appears after the get argument. The
set argument is similar to a method that returns void because it writes the value of the property type and does not return the value.
In the case of the get and set accessors, indexers contain the same formal parameters as the indexer does but properties do not contain any parameters at all. Otherwise, property accessors have some greater flexibility than indexers: A property is identified by its name instead of its signature, you can access a property through a simple name or access through a class, struct, or interface member, and the property is a static or instance member.
COMPARE PROPERTIES AND INDEXERS
Solution Explo... |
ution Explo... |
appears. |
¤ Click Help. |
‹ Click Index. |
ACCESSING PROPERTIES 9
Properties contain some limitations that you should know about so you can avoid confusing property abilities with other features such as indexers and classes.
•Because C# does not classify properties as variables, you cannot pass a property with the ref or out parameters. The ref and out parameters cause a method to refer to the same variable that the method acquired.
•C# automatically classifies the property as an instance property unless you specifically add the static modifier in the argument. Your program cannot reference a static property through an instance but only through the type name.
•If you declare a property as a static property, then you cannot use the this keyword or the virtual, abstract, or override modifiers.
If you want more information about properties and how they work differently from indexers, the MDE window online help contains downloadable examples of using properties in their properties tutorial. After you search for and access the properties tutorial page, you can access the samples by clicking the “Properties Sample” link on the page.
Index
■ The Index window appears.
› Type properties in the Look for field.
ˇ Click to select Visual C# in the Filtered by dropdown list.
Á Click comparison between properties and accessors under properties in the topics list.
■ The Comparison Between Properties and Indexersv help file appears in the parent window.
187
C#
PROGRAM PROPERTY ACCESSORS
The get and set keywords comprise the property accessors. The get keyword reads information into the property, and the set keyword writes property
information into your class, struct, or interface for further processing in your program.
Any property includes the token get and/or set accessors, but if you want to do anything with your property, you must add the accessor body. The get accessor effectively reads the value of a field — either a value that you enter into the program itself or that the user enters during runtime.
After the get accessor retrieves the value, it must return the value with the return or throw statement. All return
statements in the get accessor must specify an expression, such as a string name, that can be converted to the property type. Unless another portion of your class, struct, or interface references a property as part of an expression, the get accessor computes the value of the property.
The set accessor acts much like a method with the void return type. The body of a set accessor provides a new value for the property. A set accessor must include a return type, but unlike the get accessor, that return type must not include an expression.
PROGRAM PROPERTY ACCESSORS
Solution Explo...
page appears. ■ The New Project window
appears.
New Project.
‹ Click the Console Application icon in the Templates pane.
› Type a name for the file.
ˇ Click OK.
ACCESSING PROPERTIES 9
You can override a property accessor in one property with an accessor from another property.
TYPE THIS:
using System; class Test;
{
abstract int Area {get;
set;
}
class Cube: Area
{
public int side; public Cube(int s) {side = x;
}
public override int Area {get
{
return side * side;
}
set
{side = Math.Sqrt(value);
}}}
RESULT:
This code overrides the abstract Area property with the code for returning the cube side value.
Á 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 Class1. |
‚ Click Add. |
— Click Add Property. |
■ The C# Property Wizard window appears.
CONTINUED
189
C#
PROGRAM PROPERTY ACCESSORS
# provides two different approaches for adding Cproperty accessors. First, you can enter the accessors
entirely in code. Second, you can have C# provide you with the skeleton property information by adding the property in the C# Property Wizard.
You can access the C# Property Wizard via the Class View window. The Property Wizard contains all the basic building blocks of a property including the get and set accessors.
When you finish entering the accessors and all other property information in the Property Wizard, the basic get and/or set accessor skeletons appear so that you can add arguments into the skeletons.
After you add the accessor information, you add property values into the accessor bodies. The get accessor must adhere to the same rules as value-returning methods. Because the return type of a method that is not void must specify an expression that has the same type as the method, the get accessor must specify an expression that has the same property type.
The set accessor must also adhere to rules — in this case, the rules for void methods. Both set accessors and void methods require that any return statement does not specify an expression. If the set accessor completes normally, the accessor writes the property value and returns that value to the property caller.
PROGRAM PROPERTY ACCESSORS (CONTINUED)
int
int
¡ Click to select property type from the drop-down list.
ACCESSING PROPERTIES 9
The get accessor must end with a return or throw statement just as a method does. If you do not include a return or throw statement, your program will not compile.
TYPE THIS:
using System; class Return;
private string name; public string Name
{
get
{
}
}
RESULT:
This code will return an error because there is no return or throw type within the get argument.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
™ Type the property name in |
|
|
£ Type a property comment |
■ The property skeleton code |
∞ Save the program as the |
||||||
|
|
|
||||||||||
the Property name field. |
|
in the Comment field. |
appears in the parent |
filename. |
||||||||
|
■ Leave the accessor and |
|
|
|
¢ Click Finish. |
window. |
|
|||||
|
|
|
|
|
|
|||||||
property modifiers radio |
|
|
|
|
|
|
|
|
|
|
||
buttons selected with get/set |
|
|
|
|
|
|
|
|
|
|
||
and None, respectively. |
|
|
|
|
|
|
|
|
|
|
191