- •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
MORE ABOUT COMPONENTS |
Chapter 4 |
|
71 |
|
|||
|
|
|
|
Array.Sort (Integer);
Consider an example of an array that stores the marks of students. This data is sorted to know the maximum and minimum marks obtained by the students.
int [] Marks = {70,62,53,44,75,68}; int I = Marks.Length;
Array.Sort (Marks);
for (int x = 0; x < I; x++)
{
Console.WriteLine (x);
}
This code initializes an integer array with the values as specified in the program code.The code then calculates the size of the one-dimensional array Integer and stores its value in the variable I. The size of the array is determined using the Length property. The Sort() method is then used to sort the elements of Integer. The sorted elements are displayed in the Console window by using the Line() method of the Console class.
The output of the previous code is:
44,53,62,68,70,75
In this section, you learned about arrays. An array is a special type of collection in C#. The next section will look at collections in C#.
Collections
A collection is defined as a group of objects that you can access using the foreach loop. For example, look at the following code:
foreach (string str1 in collection1)
{
Console.WriteLine (str1):
}
In this code, collection1 is a collection, and the foreach loop is used to access
objects of collection1.
72 |
Part II |
HANDLING DATA |
|
|
|
Creating Collections
All collections in C# are implemented by the System.Collections.IEnumerable interface. You have learned about interfaces in Chapter 3 in the section “Interfaces.” Interfaces are components used to declare a set of methods that are never implemented. There are several predefined interfaces provided by C#. One of these predefined interfaces is the IEnumerable interface that has a GetEnumerator() method. This method returns an object of the type enumerator. Therefore, every collection has one or more enumerator objects associated with it. These objects are used to access data from the associated collection. You can use the enumerator object only to read data from a collection, not to modify the collection.
To access the elements of a collection, you create an object that implements the IEnumerable interface. To initialize this object, the MoveNext() method is called. This method is used to move across the elements of the collection. When the MoveNext() method is called for the first time, it moves the enumerator object to the first element of the collection.
Once the enumerator object is initialized with the first element of the collection, you can then move across the elements of the collection by calling the MoveNext() method. The value referred by the enumerator object can be read by the Current property. This property returns only a reference to the elements of the collection. Therefore, to get the actual value of the element, you can type cast the reference to the type of the element. To find out more about collections, consider the following code sample.
public interface IEnumerable
{
IEnumerator GetEnumerator ();
}
public interface IEnumerator
{
bool MoveNext(); object Current
{
get;
}
void Reset();
}
MORE ABOUT COMPONENTS |
Chapter 4 |
73 |
|
|
|
This code declares the GetEnumerator() method of the IEnumerable interface. Next, the MoveNext() method of the IEnumerator interface, which returns a Boolean type variable, is called.The Current property of the type object is used to read the current element of the collection. You use the get property to read the elements of a collection. You can use the Reset() method to reset the value of the
enumerator object.
Working with Collections
After creating a collection, you can work with it. To do this, you can use the interfaces provided by C#. Figure 4-1 lists some of the interfaces that you can use to work with collections.
FIGURE 4-1 Interfaces used with collections
Each of these interfaces is present in the System.Collections namespace. These interfaces have several classes and methods associated with them. The ArrayList class will be discussed in detail.
ArrayList is an important class present in the System.Collections namespace that you can use to create a dynamically increasing array. The ArrayList class implements the IList interface. When you create an object of the ArrayList class, C#
74 |
Part II |
HANDLING DATA |
|
|
|
allocates memory to this object. You can specify the initial size of the ArrayList object while creating the instance of the ArrayList class by using the new keyword. You can then add elements to this object. However, if you add more elements to the ArrayList than its capacity (the number of elements that an object of ArrayList can hold), C# automatically allocates more memory to the ArrayList object. Consider the following example to learn about the ArrayList class.
using System;
using System.Collections; public class ArrayList1
{
public static void Main()
{
ArrayList list1 = new ArrayList(); list1.Add(“This”); list1.Add(“is”);
list1.Add(“a”); list1.Add(“sample”); list1.Add(“ArrayList.”);
}
}
This code creates an object of the ArrayList class with the name list1 and then adds elements to this object by using the Add() method.
Some of the methods present in the interfaces used with collections are discussed in the following list.
ICollection Interface:
CopyTo(). The CopyTo() method is used to copy the elements of the ICollection interface to a specified array. You can also specify the starting index from which you want to copy the elements.
IDictionary Interface:
Add(). The Add() method is used to add an element to the IDictionary interface. You can specify the key and value of the element that is added.
MORE ABOUT COMPONENTS |
Chapter 4 |
75 |
|
|
|
Remove(). The Remove() method is used to delete an element from the IDictionary interface. You need to specify the key of the element to be deleted.
Clear(). The Clear() method is used to delete all the elements from the IDictionary interface.
GetEnumerator(). The GetEnumerator() method is used to return an IDictionaryEnumerator object for the IDictionary interface.
Contains(). The Contains() method is used to locate a particular element in the IDictionary interface. You need to specify the key of the element to be located.
IList Interface:
Add(). The Add() method of the IList interface is used to add elements to the IList interface.
Remove(). The Remove() method is used to delete the first occurrence of the object from the IList interface.
RemoveAt(). The RemoveAt() method is used to delete the element present at the index value that you specify.
Clear(). The Clear() method is used to delete all the elements from the IList interface.
Insert(). The Insert() method is used to insert an element at the specified index in the IList interface.
IndexOf(). The IndexOf() method is used to find the index value of the specified element.
ICloneable Interface:
Clone(). The Clone() method is used to create clones of an existing instance of a class.
Having learned about arrays and collections, you need to learn about indexers. Indexers are members that allow you to access objects as if they were the elements of an array.