- •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
COMPONENTS OF C# |
Chapter 3 |
59 |
|
|
|
Here, CalculateSalary() is a method in the Salary class.
You have learned about classes and namespaces. The next section offers some information about structs. Structs are data structures similar to classes and are important components of C#.
Structs
Structs are data structures that contain constructors, constants, variables, methods, indexers, properties, operators, and nested types. However, unlike classes, which are reference types, structs are value types.This implies that structs do not require allocation of heap. In addition, a variable declared of the type struct directly contains data, in contrast to a class variable that contains only a reference to the data.
Similar to declaring a class, you can also declare a struct in C#. Structs are declared using the struct keyword.
<modifier> struct <struct name>
{
------------
}
Modifiers used with structs are similar to those used with classes. Struct modifiers include new, public, private, protected, and internal. However, you cannot use the abstract and sealed modifiers with structs. Structs are implicitly sealed, as they do not support inheritance.
Structs are used to group similar data. This data can then be easily copied from one struct to another. Consider the following example:
public struct Employee
{
public int Empid; public string Empname; public string Empemail; public string Empsalary;
}
60 |
Part II |
HANDLING DATA |
|
|
|
The previous struct includes variables that are used to store employee information. Once a struct is declared, the variables in the struct are initialized to default values. However, to copy these values from one struct to another, you need first to initialize the struct with the new keyword. The new keyword is used to call the default constructor of the struct, resulting in initializing the variables declared in the struct.
Employee emp1;
Employee emp2;
emp1= new Employee (); emp1.EmployeeName = ‘Steve’ emp1.Employeeemail = ‘steve@hotmail.com’ emp1.EmployeeSalary = $1000;
Now, to copy these values from emp1 to emp2, you can use the assignment operator
(=) as follows:
emp2 = emp1;
As you can see, a single statement can copy the entire value of one struct to another. However, if you want to use classes to perform this task, you need to create a method. All data is copied from one struct to another; therefore, it is advisable that you create structs to store small amounts of data.
Structs in C# are different from structs in C and C++. Unlike C and C++, all data members of structs in C# are private by default.In addition, structs in C# are very similar to classes and can perform most of the things that classes do. However, some differences between structs and classes still exist.
As discussed earlier, structs do not require heap allocation. Instead, structs are stored on stacks of memory. This is how structs differ from classes. Figure 3-1 lists the key differences between structs and classes.
Another important component of C# is enumerations. Similar to classes and structs, enumerations are used to store values. The next section looks at enumerations in detail.
COMPONENTS OF C# |
Chapter 3 |
|
61 |
|
FIGURE 3-1 Differences between structs and classes
Enumerations
Enumerations are data structures that store values with user-friendly names. This set of user-friendly named constants is called an enumerator list. The default data type of an enumerator list is an integer. Each enumerator has an integer basetype called the underlying-type. This underlying-type of the enumerator list must contain all the values that might be present in an instance of an enumerator. To use an enumerator in your code, you first need to declare the enumerator. Enumerators are declared using the enum keyword.
<modifier> enum <enumeration name>
{
---------------
}
The modifiers used with enumerations are new, public, private, protected, and internal. However, enumerations cannot be of the type abstract or sealed. Look at the following example to understand enumerations.
public enum months
{January, February, March, April, May, June, July, August, September, October, November, December};
62 |
Part II |
HANDLING DATA |
|
|
|
The previous code creates an enumeration with the name months and declares all the possible values for months. The first element of the enumeration takes a default value of 0 and the successive elements take the previous value plus 1.
You can also specify user-defined values for the elements of an enumeration. For example,
public enum months
{January = 1, February, March, April, May, June, July, August, September, October,
November, December};
In this case, the values of January, February, and March are 1, 2, and 3, respectively. In this chapter, you have learned about inheritance in classes. To implement inheritance, C# supports interfaces.
Interfaces
Interfaces are components used to declare a set of methods. However, the data members of an interface are not implemented. As discussed earlier, C# allows you to group related data by using structs. However, to group related methods, properties, indexers, and events, you use interfaces. Interfaces contain only method declarations; therefore, you cannot create an instance of an interface. However, you need to declare an interface by using the interface keyword.
interface <interface name>
{
-----------------
}
Interface declarations do not include a modifier because all interfaces are public by default. Interfaces cannot be abstract, sealed, virtual, or static. However, you can use the new modifier with nested interfaces. The new modifier is used when you need to hide an inherited namespace by the same name as the base namespace.
In situations where you want the members of a class to exhibit certain features, you can group these members in an interface. The class can then implement the interface. The classes in C# can also implement multiple interfaces. Implementing an interface implies that a class is derived from the interface and the class
COMPONENTS OF C# |
Chapter 3 |
63 |
|
|
|
implements all the members of that interface. Consider the following example of the Employee class that implements two interfaces:
interface Employee
{
------------
}
interface Salary
{
------------
}
class Employee: Employee, Salary
{
------------
}
Therefore, the class Employee implements all the methods declared in the inter-
faces Employee and Salary.
Similar to classes, interfaces can also be inherited. These interfaces are called explicit base interfaces. C# does not support multiple inheritance of classes. However, you can achieve multiple inheritance in C# by using interfaces.
In the previous example, if the interface Salary was inherited from Employee, the interface declaration statement would be:
interface Salary: Employee
{
------------
}
TIP
In C#, if a class implements an interface, the class also implicitly implements its base interfaces.
By now, you have learned about the basic components of C# that you can use to write programs in C#. The next sections look at writing a simple program in C# and then compiling and executing it.