- •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
INTRODUCTION TO VISUAL BASIC .NET Appendix B 861
Collections
A collection can be considered as a group of related objects. Generally, a collection is used to work with related objects. However, collections can be made to work with any data type.
Standard Collections Provided by Visual Basic .NET
Visual Basic .NET provides you with several collections that are used to organize and manipulate objects in an efficient way. Consider an example. All the controls in a form are stored in the Controls collection. Similarly, all the forms in a Visual Basic .NET project are stored in the Forms collection. An efficient way to keep track of the objects that an application needs to create and destroy during run time is provided by a collection.
Consider an example: In an application that you have created, you need to take inputs for five text boxes from the user and then validate the data entered by the user for all of them. One way in which the code can be written is to check for each of the text boxes separately. Another way, which is relatively easy, is to check using the Controls collection. Every form has a Controls collection that represents all the controls, such as command buttons, labels, text boxes, and so on that are present in the form. You can easily perform the input validation check by using the Controls collection.
NOTE
Controls is the base class for all the controls. It is included in the
System.Windows.Forms namespace and is provided by Visual Basic .NET.
You have learned about the collections in a brief overview. I will now discuss how you can create your own collections.
Creating Collections
In addition to the various standard collections that are present in Visual Basic
.NET, you can create your own collections. For collections, Visual Basic .NET
862 |
Part X |
APPENDIXES |
|
|
|
provides the Collection class. The syntax for creating a collection is discussed as follows:
Dim CollectionName As New Collection()
In the preceding syntax, the name of the collection that you want to create is specified by CollectionName. An instance of the Collection class is created, which is declared by the New keyword in the declaration statement
After the creation of the collection, you can manipulate the creation in the same way as you would manipulate the standard collections that are provided by Visual Basic .NET. However, there are some differences between the two. Consider the following example:
Dim collection1 as New Collection()
collection1 = Controls
The preceding code creates and initializes a Collection object, collection1, with the Controls collection. However, this statement displays an error message. Why is it so? The answer is that the Controls collection and the Collection class object are not interchangeable and are of different types with different usage. In addition, they do not have the same methods and also do not use the same kinds of index values.
Procedures
Consider a scenario where you need to perform a particular task repeatedly, for instance, calculating the average of marks obtained by students in a particular subject. In a situation such as this, you can group them in a procedure instead of writing the statements repeated ly. A set of statements grouped together to perform a specific task is called procedure. You can organize your applications by using procedures that allow you to chunk and group the program code logically.
After grouping the statements in a procedure, you can call the procedure from anywhere in the application. To call a procedure means to execute a statement that further instructs the compiler to execute the procedure. After executing the code in the procedure, the statement following the statement that called the procedure is executed.The statement that is called by a procedure is called a calling statement, and it includes the name of the procedure.The calling statement also includes the
INTRODUCTION TO VISUAL BASIC .NET Appendix B 863
data values that are needed by the procedure for performing the tasks that are specified. The data values are also referred to as arguments or parameters.
Consider the example of calculating average mentioned previously. In this case, you can create a procedure that accepts the maximum and minimum marks obtained by students as data values and calculate the average. To call this procedure, the statement to be called must provide the minimum and maximum marks obtained by students as parameters.
Now consider some of the advantages that are offered by procedures. The first advantage is the reusability of code. In other words, a procedure can be created and used when it is required and if any statement has to be changed, you simply need to make the changes in a single location.This is useful mainly in the case of large and complex applications.The applications that use procedures are easier to debug. Additionally, you can easily trace the errors in a procedure without debugging the entire application code.
Now consider the scope or accessibility of procedures in an application. Similar to classes and variables, procedures have a scope. A procedure is generally declared in a class or a module. Therefore, you can call a procedure from the same class or module in which it is created. The scope of the procedure depends on the access modifiers that you use while the procedures are declared. The access modifiers supported by Visual Basic .NET are listed in Table B-3.
Table B-3 Access Modifiers for Procedures
Access Modifier |
Scope |
Public |
A procedure with a Public access modifier can be called from any class |
|
or module in the application. |
Private |
A procedure with a Private access modifier can be called from the |
|
same class or module in which it is declared. |
Protected |
A procedure with a Protected access modifier can be called from the |
|
same class or module in which it is declared. In addition, it can be |
|
called from the derived classes of the class in which it is declared. |
Friend |
A procedure with a Friend access modifier can be called from any class |
|
or module that contains its declaration. |
|
|
864 |
Part X |
APPENDIXES |
|
|
|
Based on the functionality of procedures, they can be classified as:
Sub procedures. A sub procedure is used to perform a specific task.
Function procedures. A function procedure is used to perform the specific tasks and returns a value to the calling statement.
Property procedures. A property procedure is used to assign or access a value from an object.
Event-handling procedures. An event-handling procedure is used to perform a specific task when a particular event occurs.
Arguments
As stated earlier, variables, constants, or expressions are accepted as arguments by procedures. As a result, each time a procedure that accepts arguments is called, arguments need to be passed to the procedure. Based on the data values that are passed as arguments, the result can differ for each call to a procedure. Arguments can be passed to procedures by either value or reference.
Functions
Visual Basic .NET provides various built-in functions that can be used in applications. Some of the built-in functions are MsgBox, InputBox, CStr, DateDiff, and StrComp. The Microsoft.VisualBasic namespace contains a declaration for these built-in functions. These functions can be classified based on the tasks performed by the various built-in functions. The functions can be classified as follows:
Functions to enhance your programs are performed by the Application enhancement functions. Examples of Application enhancement func-
tions are MsgBox and InputBox.
Functions to manipulate strings are performed by String functions. Examples of String functions are StrComp, Len, and Trim.
Functions to manipulate date and time values are performed by the Date function. Examples of Date functions are DateDiff, Now, and Month.
Functions to convert one data type to another are performed by the Conversion function. Examples of Conversion functions are CStr, CDate, and Val.
INTRODUCTION TO VISUAL BASIC .NET Appendix B 865
Having discussed the components of Visual Basic .NET, you can use this knowledge to create a simple application in Visual C# .NET.
Creating a Simple Visual C# .NET
Windows Application
In this section, you will create a simple Visual C# .NET Windows application. Then, I will discuss how to create the same application in Visual Basic .NET. Doing this will help you to appreciate how easy it is to convert an application created in one of the languages of the .NET Framework to another. In addition, you will be able to realize how closely the two languages of the .NET Framework, Visual Basic .NET and Visual C# .NET, are related.
Now, I will proceed with creating a simple Windows application in Visual C#
.NET. Name this application SampleWindowsApplication. This application accepts a username and password from the user. After specifying the required information, the user clicks on the Submit button. A message box showing the
text The user name and password that you have specified is accepted. is dis-
played. In addition, the Windows form consists of an Exit button that is used to exit the Windows application. To create SampleWindowsApplication, include two label controls, two text box controls, and two command controls to the Windows form. Next, change the following properties of the controls:
Form1
Name: frmAcceptUserInput
Text: Accept User Input
Label1
Name: lblUserName
Text: User Name
Label2
Name: lblPassword
Text: Password
866 |
Part X |
APPENDIXES |
|
|
|
Textbox1
Name: txtUserName
Textbox2
Name: txtPassword
PasswordChar: [*]
Button1
Name: btnSubmit
Text: submit
Button2
Name: btnExit
Text: Exit
After dragging the controls to the form, your SampleWindowsApplication looks as shown in Figure B-1.
FIGURE B-1 SampleWindowsApplication with the cont rols added
INTRODUCTION TO VISUAL BASIC .NET Appendix B 867
Now, to add the functionality to the application, you need to write code for the button controls. After adding code to the button controls, the code for the application is as shown:
using System.Drawing; using System.Collections;
using System.ComponentModel; using System.Windows.Forms; using System.Data;
namespace SampleWindowsApplication
{
public class frmAcceptUserInput : System.Windows.Forms.Form
{
private System.Windows.Forms.Label lblUserName; private System.Windows.Forms.Label lblPassword; private System.Windows.Forms.Button btnSubmit; private System.Windows.Forms.Button btnExit; private System.Windows.Forms.TextBox txtUserName; private System.Windows.Forms.TextBox txtPassword;
private System.ComponentModel.Container components = null;
public frmAcceptUserInput()
{
InitializeComponent();
}
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
868 |
Part X |
|
APPENDIXES |
|
|
|
|
|
|
|
|
|
[STAThread] |
|
|
||
|
|
static void Main() |
|
|
|
|
|
{ |
|
|
|
|
|
|
Application.Run(new frmAcceptUserInput()); |
||
|
|
} |
|
|
|
|
|
private void btnSubmit_Click(object sender, System.EventArgs e) |
|||
|
|
{ |
|
|
Y |
|
|
|
|
L |
|
|
|
|
MessageBox.Show(“The user name and password that you have specified is |
||
|
|
|
accepted.”); |
F |
|
|
|
|
|
|
|
|
|
} |
|
M |
|
|
} |
|
|
|
|
|
|
|
A |
|
|
|
|
private void btnExit_Click(object sender, System.EventArgs e) |
|||
|
|
{ |
E |
|
Application. xit();
} } T
After creating an application in Visual C# .NET, you can create this application in Visual Basic .NET.
Creating a Simple Application
in Visual Basic .NET
The steps for creating an application in Visual Basic .NET are similar to the steps for creating an application in Visual C# .NET. Similar to Visual C# .NET, Visual Studio .NET also provides you with a template to create an application in Visual Basic .NET. To create the SampleWindowsApplication by using Visual Basic
.NET, perform the following steps:
1.On the File menu, point to the New option.
2.In the displayed list, select the Project option. The New Project dialog box is displayed.
3.In the Project Types: pane of the New Project dialog box, select the Visual Basic Projects option.
Team-Fly®
INTRODUCTION TO VISUAL BASIC .NET Appendix B 869
4.In the Templates: pane, select the Windows Application option.
5.In the Name: text box, type the name of the Windows application as
SampleWindowsApplication1.
6.Accept the default location as specified in the Location: text box. You may also choose to browse for the location where you want to save the application by clicking on the Browse button.
7.Click on the OK button to close the New Project dialog box.
Figure B-2 shows the New Project dialog box for the SampleWindowsApplication1 project in Visual Basic .NET.
FIGURE B-2 The New P roject dialog box for SampleWindowsApplication1
When you click on the OK button, Visual Studio .NET automatically creates the default files and a blank Windows form for you. Click on the Show All Files button in the Solution Explorer window to view a list of all the files created by Visual Studio .NET. Figure B-3 shows the default files and the blank form created by Visual Studio .NET.
870 |
Part X |
APPENDIXES |
|
|
|
FIGURE B-3 The default files and the blank form created by Visual Studio .NET
As you can see in Figure B-3, the blank form in Visual Basic .NET is created with an extension .vb. In addition, Visual Studio .NET creates some reference files for the SampleWindowsApplication1 project. Similar to Visual C# .NET, Visual Studio .NET creates a solution with the same name as that of the Windows application. Inside the solution, the project with the name SampleWindowsApplication1 is created.
Now, proceed with the creation of the SampleWindowsApplication1 application. To create the application, you need to add controls to the Windows form. Because Visual Basic .NET and Visual C# .NET are languages based on the .NET Framework, the IDE for the Visual Basic .NET applications is the same as that of the Visual C# .NET applications. The IDE for the Visual Basic .NET applications contains a toolbox that contains controls that you can use to create the Windows application. Figure B-4 shows the toolbox that contains standard controls for creating a Windows application in Visual Basic .NET.
INTRODUCTION TO VISUAL BASIC .NET Appendix B 871
FIGURE B-4 The toolbox for creating Windows application
From the toolbox, drag two label controls, two text box controls, and two button controls and place them on the form. Now in the Properties window of the controls, change the properties of the controls.
TIP
If the Properties window is not displayed, select the control and press the F4 key. Alternatively, you can select the Properties Window option on the View menu.
Change the following properties of the controls:
Form1
Name: Form1
Text: Accept User Input