- •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#
PROGRAM CLASSES
bject-oriented programming languages use classes Othat act as containers for data elements in your
program. Classes let other elements in your program, such as methods, process that data and perform functions such as displaying a calculation result as output.
Object-oriented programming languages make use of classes, which are reference types that act as containers for data elements in your program. Classes include class member characteristics such as the method the member performs, the events the member performs, or the properties of the member.
A class usually includes definition of the object and the implementation of that object. However, a class can have no implementation information, and C# refers to the members of such a class as abstract members, and the class is called
an abstract class. You can use an abstract class when you want its instances to only have basic functionality that you can override or augment with information from other classes.
C# refers to the members of a class as an object. When your program invokes the object, the program refers to the class and receives the object properties as well as any implementation methods, such as whether the program uses the object as an event.
You can determine how your program accesses objects in your class. For example, one object within a class can only access another object within that class. However, any class can access objects in any other class.
PROGRAM CLASSES
Console
|
|
|
|
|
■ The New Project window |
|
|
|
|
|
|
|
¤ Click New Project in the |
||||
NET |
Start Page. |
appears. |
|||
Studio |
|
|
|
‹ Click the Console |
|
|
|
|
|
|
|
|
|
|
|
|
Application icon in the |
|
|
|
|
|
Templates pane. |
› Type a name for the file.
ˇ Click OK.
PROGRAMMING C# BUILDING BLOCKS 4
When you add a class in the Add Class Wizard, one of the first things you must do is identify a namespace. C# automatically adds a new namespace when you create a new class, but if you add a class within a class, you can specify a new namespace.
TYPE THIS:
using System;
namespace NewSpace
{
class NewClass
{
public static void Main(string[] args)
{
Console.WriteLine("This class has the namespace NewSpace.");
}
}
RESULT:
This class has the namespace NewSpace.
■ The Class1.cs code appears in the parent window.
Note: You can make more room for your Start page by clicking and dragging the right edge of the Start page until you reach the maximum size for the Start page.
|
Á Delete the Class1 code |
° Run the program by |
|
from the program. |
pressing the F5 key. |
|
‡ Type the MyClass code. |
|
|
|
■ The output string appears on the screen.
· Save the program as the filename.
67
C#
ADD A CLASS
a new C# project, the MDE window creates a new class so you can save the class code immediately. If you
you can do so in one of two ways: in code or accessing the Add Class
Explorer or Class View windows.
lets you create a class using the Add Class Wizard also lets you determine
creating will inherit information from
another class. If you want your class to inherit data from another, you must determine whether your class inherits from a base class or another class.
A base class is a single class from which all other classes will inherit. For example, if you set up class B to inherit from the base class A, you can set up class C to inherit from class B, and that way, class C will inherit all of the properties from class B as well as the base class A.
ADD A CLASS
Console
Applicatio
⁄ Click Start Programs Microsoft Visual Studio .NET 7.0 Microsoft Visual Studio
.NET 7.0.
¤ Click New Project in the Start page.
■ The New Project window appears.
‹ Click the Console Application icon in the Templates pane.
› Type a name for the file.
ˇ Click OK.
68
PROGRAMMING C# BUILDING BLOCKS 4
If you have programmed in C++ or Java before, you should be aware of changes in C# so you are not surprised. Because C# is closely related to C++ and Java, here are examples of the differences between several class-related keywords.
REFER TO A BASE CLASS
Java |
super |
C++ |
__super |
C# |
base |
DERIVE A CLASS FROM A BASE CLASS
Java |
class A extends B |
C++ |
class A public B |
C# |
class A B |
SPECIFY THAT A CLASS CAN BE INHERITED
Java |
abstract |
C++ |
abstract |
C# |
abstract |
SPECIFY THAT A CLASS CANNOT BE INHERITED
Java |
final |
C# |
sealed |
Class View - Ad...
AddClass
Black Text
Add Class... |
Add |
Á Click the Class View tab at the bottom of the Solution Explorer window.
‡ Right-click AddClass in the Class View window.
° Click Add. |
■ The C# Class Wizard |
· Click Add Class. |
appears. |
|
CONTINUED
69
C#
ADD A CLASS
f you decide to add a class using the Solution Explorer, Ithe procedure is different than adding a class from the Class View window. The most obvious difference is that you do not use the Add Class Wizard. Instead, you tell C# that you want to create a new class object. After you create
the class object, the class appears with the class skeleton already written for you so you can edit the class.
You can add as many classes to your program as you want. The class structure contains the namespace information, sample XML commentary, the class constructor, and comments telling you to add the constructor logic in place
of the comments. The class structure appears no matter what project you have created — even an empty Web project. You can edit the class to your content in the MDE parent window.
You can change the properties of the added class by using the Properties window below the Solution Explorer window. If you want to change the name of your class you can do that in the Solution Explorer as well. When you finish editing your class, it remains as part of your project unless you click the Exclude From Project option when you right-click the class name.
ADD A CLASS (CONTINUED)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Click to select the |
|
± Click to select a class |
|||
|
||||||
|
|
|||||
access level from the |
modifier in the Class |
|||||
|
-down list. |
modifiers area. |
¡ Type a class comment in the Comment field.
™ Click Finish.
PROGRAMMING C# BUILDING BLOCKS 4
You can specify that your class is a base class by adding the abstract keyword to it.
TYPE THIS:
using System;
// This is an abstract class as denoted by the abstract keyword.
abstract class AbClass
{
static void Main(static[] args)
{
int string a = “An abstract class.”;
Console.WriteLine(a);
}
}
RESULT:
An abstract class.
AddClass
■ The NewClass class appears in its own tab within the parent window.
£ Click AddClass in the Class View window.
¢ Click {} AddClass.
■ The NewClass entry appears in the tree.
∞ Click NewClass.
■ The NewClass properties appear in the Properties window.
§ Save the program as the filename.
71