- •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
28 |
Part I |
INTRODUCTION TO C# |
|
|
|
Statements and Expressions
Statements in C# are similar to those in C and C++. Statements in C# can be of two types:
Simple |
|
|
|
Embedded |
|
|
Y |
|
|
|
|
Simple statements include all variable declaration statements and labeled state- |
|||
|
|
L |
|
ments. However, all other statements that are embedded and are a part of another |
|||
statement are called embedded statements. All statements in C# are enclosed within |
|||
curly braces {}. |
|
M |
|
|
|
|
|
Types of Statements |
A |
|
|
|
F |
||
E |
|
||
C# supports simple statements and embedded statements, such as selection, itera- |
|||
T |
|
|
tion, and jump statements. Look at these statements in detail.
Simple Statements
Simple statements include all declaration and labeled statements. Simple statements also include statements that are used to call methods.
Declaration Statements
A declaration statement is used to declare a variable or a constant. You can use a single statement to declare more than one variable or constant. The following is an example of an initializing statement.
public int i, y;
i = 45;
y = 37;
Similarly, you use declaration statements to declare constants. Constants are data types whose value cannot be changed after being declared. The const keyword is used to declare a constant. Consider the following code sample:
const char x = a;
The previous code declares a constant x of the type character and assigns a value a to it.The value of the constant x cannot be changed throughout the lifetime of x.
Team-Fly®
C# BASICS |
Chapter 2 |
29 |
|
|
|
Labeled Statements
In addition to declaring variables and constants, you can declare labels in C#. A labeled statement is a simple statement that is used to declare a label.The syntax of a labeled statement is as follows:
<label1> : <statement1>
Here, label1 is the name of a label and statement1 specifies the statements to be executed when the control reaches the label. You use a goto statement to refer to a label.
Method Call Statements
A method call statement is also a type of a simple statement. A method call statement is used to call a method that is already created. The following code sample is an example of a method call statement:
public int x;
x = 100;
MessageBox.Show (x);
The previous code calls the Show method of the class MessageBox.
Selection Statements
C# also provides you with selection statements. In cases where the program has to execute one block of statements out of all the available blocks of statements, selection statements are used. In such a case, the program code needs to select the block of statements to be executed, which are therefore called selection statements. The selection of the statements to be executed is based on the value returned by evaluating an expression that follows the selection statement. The selection statements are of two types:
if-statement
switch-statement
if-statement
The if-statement is a decision-making statement that selects a specific set of statements to execute.The selection of the set of statements is based on a Boolean value
30 |
Part I |
INTRODUCTION TO C# |
|
|
|
that is returned by evaluating a given expression. The syntax of an if-statement is as follows:
if (Boolean-expression) statement1
The Boolean expression returns a value of either true or false. If the Boolean expression evaluates to true, the statement following the Boolean expression is executed. After the execution of statement1, the control passes to the end of the if-statement. If the result of the Boolean expression is false, the statements in the else block are executed.
Look at the following example of an if-else statement:
int x;
if (x >= 0)
{
MessageBox.Show(“x is a positive number.”);
}
else
{
MessageBox.Show(“x is a negative number.”);
}
The previous code tests for the value of x, and if the Boolean expression evaluates to true, the message “x is a positive number.” is displayed.However, if the value of the Boolean expression (x >= 0) is false, the message “x is a negative number.” is displayed.
If an else statement is not provided in the if-statement, the control is transferred to the end of the if-statement when the result of the Boolean expression is false.
switch-statement
Similar to the if-statement, the switch-statement is a type of a selection statement. However, a switch-statement is used when there are multiple block statements from which to choose. The syntax of a switch-statement is as follows:
--------------
switch (expression)
{
case constant-expression:
statement
C# BASICS |
Chapter 2 |
31 |
|
|
|
jump-statement [default:
statement jump-statement]
}
--------------
The switch statement is written with a switch keyword, followed by an expression to be evaluated.The switch statement evaluates an expression, and the set of statements to be executed is selected.The selection is based on the result of the expression. The different sets of statements to be executed are considered as different cases. The case keyword is used to define different cases. The result of the statement is matched to the available cases, and the set of statements to be executed is selected. The following is an example of a switch-statement:
int x; switch (x)
{
case 1:
MessageBox.Show(“x is a positive number.”); break;
case 2:
MessageBox.Show(“x is a negative number.”); break;
default:
MessageBox.Show(“x is equal to 0.”); break;
}
As shown in the preceding code, you can also include a default case. The default case is executed if the result of the expression does not match any of the available cases. A break statement is used to pass the control out of the case.
In C and C++, a fall-through condition can occur. In a fall-through condition, if you omit any of the break statements, the program executes two cases. However, in C#, a fall-through condition is omitted because the compiler throws an error for each case that does not end with a break statement.The following code in C# generates an error:
int x;
switch (x)
32 |
Part I |
INTRODUCTION TO C# |
|
|
|
{
case 1:
MessageBox.Show(“x is a positive number.”);
break;
case 2:
MessageBox.Show(“x is a negative number.”);
default:
MessageBox.Show(“x is equal to 0.”);
break;
}
If you need to execute two cases, you need to provide an explicit goto statement. The syntax for such a code is given as follows:
switch (expression)
{
case 1 : statement goto case2;
case 2 : statement
goto default; [default:
statement jump-statement]
}
In this case, the compiler first executes case1, then case2, and then the default case.
In addition to the selection statements, types of statements also include iteration statements.
Iteration Statements
The iteration statements are used to execute a set of statements repeatedly until a condition is met. The types of iteration statements are as follows:
for loop
foreach loop
C# BASICS |
Chapter 2 |
33 |
|
|
|
while loop
do-while loop
for Loop
The for loop in C# is similar to the for loop in C and C++. The for loop is used to execute a given set of statements until a given expression in the for loop returns true. The syntax of a for loop is as follows:
for (initializer; condition; iterator)
{
-------------------
}
Here, initializer is an expression that is evaluated before the control enters the loop. The condition specifies the condition that is evaluated before every iteration is completed.The iterator is the expression that is evaluated after every iteration. The statements in the for loop are continuously executed until the expression returns false.
If you want to transfer the control of execution to the end of the for loop when the control is within the loop, you can use a break statement explicitly. In such a case, the statements within the for loop are not executed even if the condition evaluated is true. Therefore, the iteration stops.
However, if you need to end a particular iteration, you can use a continue statement.The control of execution passes to the end of the statements within the for loop, which ends only the running iteration.
foreach Loop
The foreach loop is introduced in C#. However, it did not exist in C and C++.The foreach loop in C# iterates the statements in the foreach loop for each element in an array or collection. The following example will help you to understand the
foreach loop.
int [] Integer = {15,89,1000,6} foreach (int x in Integer)
{
Console.WriteLine (x)
}
34 |
Part I |
INTRODUCTION TO C# |
|
|
|
Here, in is a keyword for the foreach loop.
TIP
The value of the variable in the foreach loop cannot change during the execution of the foreach loop.
while Loop
The while loop is similar to a for loop because it evaluates a condition and executes the statements within the while loop until the condition returns false. The while loop in C# is similar to the while loop in C and C++. If the condition that is evaluated results in false the first time, the while loop is not executed.The syntax of a while loop is similar to that of the for loop, except that the while loop takes only one parameter. The code sample that follows is an example of the while loop that is not executed.
int x = 20; while (x < 10)
{
Console.WriteLine (x);
x++;
}
When the code evaluates the condition for the first time, the result is false. Therefore, the control does not enter the loop.
do-while Loop
The do-while loop is another form of iteration statements in which the condition is evaluated for the first time after the statements in the do-while loop are executed. This implies that the do-while loop, in contrast to the while loop, is executed at least once. The following code executes the statements in the do-while loop once before it checks for the value of x and, therefore, displays the value of x once.
int x = 20; do {
Console.WriteLine (x); } while (x < 10)
C# BASICS |
Chapter 2 |
35 |
|
|
|
Jump Statements
Jump statements are also a type of statement in C#.The jump statements are used to pass the control of the execution unconditionally to another line in the program. The line of code to which the control is transferred is called the target of the jump statement. The commonly used jump statements in C# are:
goto statement
return statement
break statement
continue statement
The jump statements in C# are similar to those in C and C++.
goto Statement
The goto statement is used to jump unconditionally to another line in a program. You need to specify the line to which the code jumps using a label. The syntax of the goto statement uses the goto keyword, such as:
goto Label1;
where Label1 specifies the line to which the code passes the control.
TIP
The goto statement cannot be used in the following cases:
•Jumping into a block of code
•Exiting a finally block
•Jumping outside a class
return Statement
A return statement is another jump statement that is used to end a method of a class. After a method ends, the execution control is transferred to the calling method. If the method that includes the return statement has a return type, the