- •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#
INSERT THE VOID TYPE
The void type is a new type introduced with Visual C# and the last of the four types available. Visual C# uses the void type with methods as well as functions that
require methods including classes, events, delegates, and indexers.
The void type indicates that the method does not return a value and/or take any parameters. Many statements will use the void type that precedes the method so the program will understand that it will take the implementation information from your method and the method will not accept any parameters or return any value.
If you want a method to accept parameters from the code that accesses the method (such as a class) but not return any value, you can enter void as the return type. The void type cannot be used as a parameter in the method statement; void applies only to the method return type and as a precedent to the method statement.
The void type gets a lot of exposure because so many different components in Visual C# use methods. These can include indexers and events as well as other reference types including classes and delegates. See page 130 to learn more about using the void type with delegates.
INSERT THE VOID TYPE
Console
Applicatio
Properties
¤ Click New Project. |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
■ The New Project window |
|
|
|
|
|
||||
|
|
|
|
|
|||||
› Type a name for the file. |
|||||||||
|
appears. |
|
ˇ Click OK. |
||||||
|
|
‹ Click the Console |
|
||||||
|
|
|
|
|
|
|
|||
|
|
|
|
|
|
|
|||
|
Application icon in the |
|
|
|
|
|
|||
|
Templates pane. |
|
|
|
|
|
WORKING WITH TYPES AND INTERFACES 5
When you create a new class, you can use the new modifier for hiding an inherited member in the base class. You can do this by entering the name of the method preceded by the void type.
TYPE THIS:
using System;
public class Inherited : Base
{
new public void Main ();
}
RESULT:
This code hides the Main method in the base class so only the objects in the inherited class will receive the implementation instructions from the Main method. Because the void type precedes the Main method in the code, the method will not return any values or accept any variables.
You use the void type when the method has no return statement, but if you do not include a void type or a return statement within your method, the Visual Studio.NET compiler will return an error. The MDE window alerts you if a void type or return statement does not exist, so that you can fix the problem before you compile your program.
When you create a new class, you can use the new modifier for hiding an inherited member in the
■ The appears window
Á Delete within
C#
ADD INTERFACE PROPERTIES
he Visual C# Add Properties Wizard lets you enter Tproperties information from the Class View window
without entering any code. After you finish with the wizard, the properties information appears in your code in the proper location.
Properties provide basic information about how to read, write, and compute values of fields. Interface properties use the get and set accessors, statements that access information, for reading and writing information from a field, respectively.
When a user enters information into a text field in your program, you can use the get accessor to add that
information into your program and you can use the set accessor for assigning that user input to a value. The get accessor is similar to a method in that it must return a value of the property type. For example, if the property for the get accessor is character based, the value must be a string.
The set accessor is similar to a method that returns the void type. The set accessor is not designed to write information for output but to provide information acquired through the get accessor for use in the rest of the program. For example, a name acquired through the get accessor can be assigned to a value by using the set accessor.
ADD INTERFACE PROPERTIES
Console |
Applicatio |
|
Click the Console |
Á Click the Class View tab |
|
||
Application icon in the |
in the Solution Explorer |
|
Templates pane. |
window. |
|
|
Type a name for the file. |
■ The Class View window |
|
||
|
Click OK. |
appears. |
|
Note: You can also view the Class |
|
|
|
|
|
|
View window by pressing Ctrl+Shift+C |
|
|
on your keyboard. |
Class View - IntProp
Add
Add Property...
‡ Click the plus sign (+) to expand the tree until you reach the Class1 entry.
° Right-click Class1.
· Click Add.
‚ Click Add Property.
WORKING WITH TYPES AND INTERFACES 5
You can change the state of your object as the program runs within the get accessor, such as adding two plus (+) operators to an integer variable to change the variable value.
TYPE THIS:
using System; class ChangeState
{
public int Number get
{
return Number++;
}
}
RESULT:
The state of the object changes every time your project accesses the Number field.
Visual C# classifies the get and set accessors as read-only and write-only properties, respectively. Read-only properties cannot have any values written to them. Writeonly properties have restricted reference access — only properties that can use the write-only property to perform a task can reference that write-only property.
■ The C# appears.
— Type in the access, type,