- •About the Authors
- •Contents at a Glance
- •Contents
- •Introduction
- •Goal of the Book
- •How to Use this Book
- •Introduction to the .NET Framework
- •Common Language Runtime (CLR)
- •Class Library
- •Assembly
- •Versioning
- •Exceptions
- •Threads
- •Delegates
- •Summary
- •Introduction to C#
- •Variables
- •Initializing Variables
- •Variable Modifiers
- •Variable Data Types
- •Types of Variables
- •Variable Scope
- •Types of Data Type Casting
- •Arrays
- •Strings
- •Initializing Strings
- •Working with Strings
- •Statements and Expressions
- •Types of Statements
- •Expressions
- •Summary
- •Classes
- •Declaring Classes
- •Inheritance
- •Constructors
- •Destructors
- •Methods
- •Declaring a Method
- •Calling a Method
- •Passing Parameters to Methods
- •Method Modifiers
- •Overloading a Method
- •Namespaces
- •Declaring Namespaces
- •Aliases
- •Structs
- •Enumerations
- •Interfaces
- •Writing, Compiling, and Executing
- •Writing a C# Program
- •Compiling a C# Program
- •Executing a C# Program
- •Summary
- •Arrays
- •Single-Dimensional Arrays
- •Multidimensional Arrays
- •Methods in Arrays
- •Collections
- •Creating Collections
- •Working with Collections
- •Indexers
- •Boxing and Unboxing
- •Preprocessor Directives
- •Summary
- •Attributes
- •Declaring Attributes
- •Attribute Class
- •Attribute Parameters
- •Default Attributes
- •Properties
- •Declaring Properties
- •Accessors
- •Types of Properties
- •Summary
- •Introduction to Threads
- •Creating Threads
- •Aborting Threads
- •Joining Threads
- •Suspending Threads
- •Making Threads Sleep
- •Thread States
- •Thread Priorities
- •Synchronization
- •Summary
- •Case Study
- •Project Life Cycle
- •Analyzing Requirements
- •High-Level Design
- •Primary and Foreign Keys
- •Referential Integrity
- •Normalization
- •Designing a Database
- •Low-Level Design
- •Construction
- •Integration and Testing
- •User Acceptance Testing
- •Implementation
- •Operations and Maintenance
- •Summary
- •Creating a New Project
- •Console Application
- •Windows Applications
- •Creating a Windows Application for the Customer Maintenance Project
- •Creating an Interface for Form1
- •Creating an Interface for WorkerForm
- •Creating an Interface for CustomerForm
- •Creating an Interface for ReportsForm
- •Creating an Interface for JobDetailsForm
- •Summary
- •Performing Validations
- •Identifying the Validation Mechanism
- •Using the ErrorProvider Control
- •Handling Exceptions
- •Using the try and catch Statements
- •Using the Debug and Trace Classes
- •Using the Debugging Features of Visual Studio .NET
- •Using the Task List
- •Summary
- •Creating Form1
- •Connecting WorkerForm to the Workers Table
- •Connecting CustomerForm to the tblCustomer Table
- •Connecting the JobDetails Form
- •to the tblJobDetails Table
- •Summary
- •Introduction to the Crystal Reports Designer Tool
- •Creating the Reports Form
- •Creating Crystal Reports
- •Creating the Windows Forms Viewer Control
- •Creating the Monthly Worker Report
- •Summary
- •Introduction to Deploying a Windows Application
- •Deployment Projects Available in Visual Studio .NET
- •Deployment Project Editors
- •Summary
- •Case Study
- •Project Life Cycle
- •Analyzing Requirements
- •High-Level Design
- •Low-Level Design
- •Summary
- •Populating the TreeView Control
- •Displaying Employee Codes in the TreeView Control
- •Event Handling
- •Displaying Employee Details in the ListView Control
- •Summary
- •Case Study
- •Project Life Cycle
- •Analyzing Requirements
- •High-Level Design
- •Low-Level Design
- •Summary
- •Adding the Programming Logic to the Application
- •Adding Code to the Form Load() Method
- •Adding Code to the OK Button
- •Adding Code to the Exit Button
- •Summary
- •The Created Event
- •Adding Code to the Created Event
- •Overview of XML
- •The XmlReader Class
- •The XmlWriter Class
- •Displaying Data in an XML Document
- •Displaying an Error Message in the Event Log
- •Displaying Event Entries from Event Viewer
- •Displaying Data from the Summary.xml Document in a Message Box
- •Summary
- •Airline Profile
- •Role of a Business Manager
- •Role of a Network Administrator
- •Role of a Line-of-Business Executive
- •Project Requirements
- •Creation and Deletion of User Accounts
- •Addition of Flight Details
- •Reservations
- •Cancellations
- •Query of Status
- •Confirmation of Tickets
- •Creation of Reports
- •Launch of Frequent Flier Programs
- •Summarizing the Tasks
- •Project Design
- •Database Design
- •Web Forms Design
- •Enabling Security with the Directory Structure
- •Summary
- •Getting Started with ASP.NET
- •Prerequisites for ASP.NET Applications
- •New Features in ASP.NET
- •Types of ASP.NET Applications
- •Exploring ASP.NET Web Applications
- •Introducing Web Forms
- •Web Form Server Controls
- •Configuring ASP.NET Applications
- •Configuring Security for ASP.NET Applications
- •Deploying ASP.NET Applications
- •Creating a Sample ASP.NET Application
- •Creating a New Project
- •Adding Controls to the Project
- •Coding the Application
- •Summary
- •Creating the Database Schema
- •Creating Database Tables
- •Managing Primary Keys and Relationships
- •Viewing the Database Schema
- •Designing Application Forms
- •Standardizing the Interface of the Application
- •Common Forms in the Application
- •Forms for Network Administrators
- •Forms for Business Managers
- •Forms for Line-of-Business Executives
- •Summary
- •The Default.aspx Form
- •The Logoff.aspx Form
- •The ManageUsers.aspx Form
- •The ManageDatabases.aspx Form
- •The ChangePassword.aspx Form
- •Restricting Access to Web Forms
- •The AddFl.aspx Form
- •The RequestID.aspx Form
- •The Reports.aspx Form
- •The FreqFl.aspx Form
- •Coding the Forms for LOB Executives
- •The CreateRes.aspx Form
- •The CancelRes.aspx Form
- •The QueryStat.aspx Form
- •The ConfirmRes.aspx Form
- •Summary
- •Designing the Form
- •The View New Flights Option
- •The View Ticket Status Option
- •The View Flight Status Option
- •The Confirm Reservation Option
- •Testing the Application
- •Summary
- •Locating Errors in Programs
- •Watch Window
- •Locals Window
- •Call Stack Window
- •Autos Window
- •Command Window
- •Testing the Application
- •Summary
- •Managing the Databases
- •Backing Up the SkyShark Airlines Databases
- •Exporting Data from Databases
- •Examining Database Logs
- •Scheduling Database Maintenance Tasks
- •Managing Internet Information Server
- •Configuring IIS Error Pages
- •Managing Web Server Log Files
- •Summary
- •Authentication Mechanisms
- •Securing a Web Site with IIS and ASP.NET
- •Configuring IIS Authentication
- •Configuring Authentication in ASP.NET
- •Securing SQL Server
- •Summary
- •Deployment Scenarios
- •Deployment Editors
- •Creating a Deployment Project
- •Adding the Output of SkySharkDeploy to the Deployment Project
- •Deploying the Project to a Web Server on Another Computer
- •Summary
- •Organization Profile
- •Project Requirements
- •Querying for Information about All Books
- •Querying for Information about Books Based on Criteria
- •Ordering a Book on the Web Site
- •Project Design
- •Database Design
- •Database Schema
- •Web Forms Design
- •Flowcharts for the Web Forms Modules
- •Summary
- •Introduction to ASP.NET Web Services
- •Web Service Architecture
- •Working of a Web Service
- •Technologies Used in Web Services
- •XML in a Web Service
- •WSDL in a Web Service
- •SOAP in a Web Service
- •UDDI in a Web Service
- •Web Services in the .NET Framework
- •The Default Code Generated for a Web Service
- •Testing the SampleWebService Web Service
- •Summary
- •Creating the SearchAll() Web Method
- •Creating the SrchISBN() Web Method
- •Creating the AcceptDetails() Web Method
- •Creating the GenerateOrder() Web Method
- •Testing the Web Service
- •Securing a Web Service
- •Summary
- •Creating the Web Forms for the Bookers Paradise Web Site
- •Adding Code to the Web Forms
- •Summary
- •Case Study
- •Project Life Cycle
- •Analyzing Requirements
- •High-Level Design
- •Low-Level Design
- •Summary
- •Overview of Mobile Applications
- •The Microsoft Mobile Internet Toolkit
- •Overview of WAP
- •The WAP Architecture
- •Overview of WML
- •The Mobile Web Form
- •The Design of the MobileTimeRetriever Application
- •Creating the Interface for the Mobile Web Forms
- •Adding Code to the MobileTimeRetriever Application
- •Summary
- •Creating the Forms Required for the MobileCallStatus Application
- •Creating the frmLogon Form
- •Creating the frmSelectOption Form
- •Creating the frmPending Form
- •Creating the frmUnattended Form
- •Adding Code to the Submit Button in the frmLogon Form
- •Adding Code to the Query Button in the frmSelectOption Form
- •Adding Code to the Mark checked as complete Button in the frmPending Form
- •Adding Code to the Back Button in the frmPending Form
- •Adding Code to the Accept checked call(s) Button in the frmUnattended Form
- •Adding Code to the Back Button in the frmUnattended Form
- •Summary
- •What Is COM?
- •Windows DNA
- •Microsoft Transaction Server (MTS)
- •.NET Interoperability
- •COM Interoperability
- •Messaging
- •Benefits of Message Queues
- •Limitations
- •Key Messaging Terms
- •Summary
- •Pointers
- •Declaring Pointers
- •Types of Code
- •Implementing Pointers
- •Using Pointers with Managed Code
- •Working with Pointers
- •Compiling Unsafe Code
- •Summary
- •Introduction to the Languages of Visual Studio .NET
- •Visual C# .NET
- •Visual Basic .NET
- •Visual C++ .NET
- •Overview of Visual Basic .NET
- •Abstraction
- •Encapsulation
- •Inheritance
- •Polymorphism
- •Components of Visual Basic .NET
- •Variables
- •Constants
- •Operators
- •Arrays
- •Collections
- •Procedures
- •Arguments
- •Functions
- •Adding Code to the Submit Button
- •Adding Code to the Exit Button
- •Summary
- •Introduction to Visual Studio .NET IDE
- •Menu Bar
- •Toolbars
- •Visual Studio .NET IDE Windows
- •Toolbox
- •The Task List Window
- •Managing Windows
- •Customizing Visual Studio .NET IDE
- •The Options Dialog Box
- •The Customize Dialog Box
- •Summary
- •Index
76 |
Part II |
HANDLING DATA |
|
|
|
Indexers
There may be instances where you need to access the elements of a class as an array. You can do this by using indexers provided by C#. To be able to use indexers in classes, you first need to declare an indexer. Indexers are declared as follows:
<modifier> <type> this [parameter-list]
Here, modifier is the indexer modifier and type defines the return type of the indexer. The this keyword is used as a name of the indexer. Indexers do not have an explicit name. The parameter-list in the square brackets defines the data type of the object that has the elements to be accessed.
TIP
In the indexer declaration statement, you can specify any data type as the index of the elements to be accessed.
For example, consider the following sample code:
public int this [int x]
This code declares a public indexer with the return type as integer. Here, the data type of the object is of the integer type.
C# allows you to define both read-only and write-only indexers. To read and write data to an indexer, you use the get and set properties, respectively. The get and set properties do not take any parameter. However, the get property returns the elements of the type as specified in the indexer declaration statement. The set property is used to assign values to indexer elements.Consider the following code:
class Class1
{
int variable1, variable2;
public int this [int x]
set
{
switch (x)
{
MORE ABOUT COMPONENTS |
Chapter 4 |
77 |
|
|
|
case 0:
variable1 = 10;
break; case 1:
variable2 = 20;
break;
}
}
get
{
switch (x)
{
case 0:
return variable1;
case 1:
return variable2;
}
}
}
In this code, the switch statements are used to read and write data to the indexer.
In this section, you learned about arrays, collections, and indexers that are used to store and access variables and objects. However, you also need to learn about type casting variables into objects and vice versa. To do this, C# provides you with the techniques of boxing and unboxing.
Boxing and Unboxing
Boxing is a data type conversion technique that is used to implicitly convert a value type to either an object type or a reference type. When you convert a value type to an object type, C# creates an instance of the object type and then copies the value type to that instance.
Consider the following example of an implicit data conversion by using boxing.
class Class1
{
public static void Main ()
78 |
Part II |
HANDLING DATA |
|
|
|
{ |
|
|
|
|
|
|
|
string string1 = “New String”; |
|
|
|
object obj1 = string1; |
|
|
|
Console.WriteLine (obj1); |
|
|
|
} |
|
|
} |
|
|
|
This code initializes a string type variable with the value “New String” and then |
||
|
|
|
Y |
|
creates an instance obj1 of the type object. The value of string1 is now copied to |
||
|
|
L |
|
|
the new instance of object and is displayed in the Console window. |
||
|
|
F |
|
|
In addition to implicit data conversion by using boxing, you can use boxing to |
||
|
|
M |
|
explicitly convert data. Look at the following example of an explicit data conver-
sion by using boxing. A string string1 = “New String”;E
object obj1 = (object) string1;
This code uses theTcast operator to explicitly convert string1 to an object.
Similar to boxing, unboxing is also a data type conversion technique. Unboxing is used to explicitly convert an object type to a value type.The technique of unboxing is opposite to that of boxing. However, to unbox a reference type, it is essential that you first box the value type. Unboxing can be only of the explicit conversion type. Consider the following example to understand unboxing.
string string1 = “New String”;
object obj1 = string1;
string string2 =(string) obj1;
While unboxing from one type to another, you need to take care that the resultant variable has enough space to store the initial type. For example, if you try to unbox as byte variable type from an integer variable type, it may result in an error. In this case, you box a 32-bit integer type value to an 8-bit sbyte type value. Subsequently, you unbox a smaller value to a larger value. Therefore, the following code generates an error in C#.
int x = 100;
object y = (object) x;
sbyte z = (sbyte) y;
Team-Fly®
MORE ABOUT COMPONENTS |
Chapter 4 |
79 |
|
|
|
In addition to data conversion statements, C# provides you with certain commands that influence the compilation of your code. These commands are called preprocessor directives.
Preprocessor Directives
In C#, preprocessor directives are commands that are not executed. However, these commands influence the process of compilation of code. For example, if you do not want the compiler to execute certain part of the code, you can mark the code by using the preprocessor directive. To declare a preprocessor directive, use a # sign, such as:
# preprocessor name
Some of the commonly used preprocessor directives provided by C# are discussed in the following sections.
#region and #endregion
C# provides you with the #region preprocessor directive that you can use to define a set of statements to be executed as a block. The #endregion directive marks the end of such a set of statements. For example:
#region Region1
string EmpName, EmpAddress;
int Empcode, Empphone;
#endregion
Here, Region1 is the name given to the set of statements marked by the #region preprocessor directive.
#define and #undef
The #define and #undef preprocessor directives are used to define and remove the definition of a symbol, respectively. These preprocessor directives are similar to a variable declaration statement. However, the symbols created by these directives
80 |
Part II |
HANDLING DATA |
|
|
|
do not exist. You can use the #define and #undef directives to declare symbols. However, you cannot create symbols by using these declarations. For example:
#define symbol1
and
#undef symbol1
The first line of code defines a symbol with the name symbol1, and the second line of code deletes the definition of symbol1.
#if, #endif, #else, and #elif
As discussed earlier, preprocessor directives can be used to prevent a compiler from executing certain sections of code. Similarly, you can also use certain preprocessor directives to conditionally compile certain sections of code. To do this, C# provides you with the #if, #endif, #else, and #elif preprocessor directives. These directives are commonly called conditional preprocessor directives.
The syntax of an #if-#endif command is as follows:
#if symbol1
-----------------
#endif
Here, symbol1 is a symbol declared by the #define preprocessor directive. The statements in the #if loop are executed if the symbol following the #if keyword has been previously declared using the #define command. If symbol1 has not been previously declared, the compiler reaches the end of #endif statement.
You can also direct the compiler to execute a set of statements if the symbol is not defined.This can be done using the #elif and #else preprocessor directives. Look at the following example.
#define Symbol1
class Class1
{
#if Symbol1
Console.WriteLine (“Symbol1 exists”)
MORE ABOUT COMPONENTS |
Chapter 4 |
81 |
|
|
|
#else
Console.WriteLine (“Symbol1 does not exist”)
#endif
}
TIP
You can also use nested #if-#elif loops.
#error and #warning
The #error and #warning preprocessor directives are used to raise an error and a warning, respectively. If the compiler encounters the #warning preprocessor directive, it issues a warning to the programmer by displaying the text in the #warning statement. The compiler then resumes with compilation of the code. However, if the compiler comes across an #error preprocessor directive, it generates an error and stops executing the code. The #error and #warning preprocessor directives are generally used with the conditional preprocessor directives discussed previously.
Look at the following example to have better understanding of the preprocessor directives used in C#.
#define Symbol1 using System; public class Class1
{
public static void Main()
{
#if Symbol1
Console.WriteLine(“Symbol1 is defined”);
#else
#warning Symbol1 is not defined
#endif
}
}
The output of the previous code is shown in Figure 4-2.