<?xml version="1.0" encoding="UTF-8" standalone="no"?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><rss xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd" version="2.0"><channel><title>Web Solution</title><description></description><managingEditor>noreply@blogger.com (PS Info Soft)</managingEditor><pubDate>Thu, 5 Sep 2024 19:44:33 -0700</pubDate><generator>Blogger http://www.blogger.com</generator><openSearch:totalResults xmlns:openSearch="http://a9.com/-/spec/opensearchrss/1.0/">19</openSearch:totalResults><openSearch:startIndex xmlns:openSearch="http://a9.com/-/spec/opensearchrss/1.0/">1</openSearch:startIndex><openSearch:itemsPerPage xmlns:openSearch="http://a9.com/-/spec/opensearchrss/1.0/">25</openSearch:itemsPerPage><link>http://swebsolution.blogspot.com/</link><language>en-us</language><itunes:explicit>no</itunes:explicit><itunes:subtitle/><itunes:owner><itunes:email>noreply@blogger.com</itunes:email></itunes:owner><item><title>Best Hotels of India</title><link>http://swebsolution.blogspot.com/2009/10/best-hotels-of-india.html</link><author>noreply@blogger.com (Unknown)</author><pubDate>Sat, 24 Oct 2009 09:20:00 -0700</pubDate><guid isPermaLink="false">tag:blogger.com,1999:blog-7227425366482465228.post-8755015270629259990</guid><description>&lt;div xmlns='http://www.w3.org/1999/xhtml'&gt;&lt;p&gt;Hotels in India,Hotel India,Hotels India,India Hotel, India Hotels,  Hotel in India,India Hotels,Hotels of India,Hotel of India,Indian Hotels, Luxury Hotels in India, Hotels in Goa, Hotels in Kerala, Hotels in Rajasthan, Hotels in Uttaranchal, Hotels in Agra, Hotels in Bangalore, Hotels in Bombay, Hotels in Chennai, Hotels in Delhi, Hotels in Hyderabad, Deluxe Hotels in India, Hotels in Jaipur, Hotels in Mumbai,Hotels in New Delhi, Hotels in Udaipur, Indian Hotels and Resorts, Indian hotels and resorts, Indian Odyssey&lt;/p&gt;in reference to: &lt;a href='http://www.besthotelsofindia.in/'&gt;Best India Hotels, Best India Hotels, Best Indian Hotels, Cheap Hotels in India, Deluxe Hotels in India, Best Hotels in India, Hotels Packages of India, India Hotels Packages, India online Hotels Booking, Hotels Booking in Indian, Hotels in Cities of India, Hotels in States of India, Luxury Hotels in India&lt;/a&gt; (&lt;a href='http://www.google.com/sidewiki/entry/prakashsherawat/id/7O1vD0XeN1rvSEtNsRrZXmJATNA'&gt;view on Google Sidewiki&lt;/a&gt;)&lt;/div&gt;</description><thr:total xmlns:thr="http://purl.org/syndication/thread/1.0">0</thr:total></item><item><title>Java Help, Java Tutorials, Java Programming, Java Tricks</title><link>http://swebsolution.blogspot.com/2008/01/java-help-java-tutorials-java_6645.html</link><author>noreply@blogger.com (Unknown)</author><pubDate>Tue, 8 Jan 2008 00:54:00 -0800</pubDate><guid isPermaLink="false">tag:blogger.com,1999:blog-7227425366482465228.post-827900750575038361</guid><description>&lt;p&gt;&lt;b&gt;&lt;br /&gt;&lt;a href="http://www.sbinfosystem.com"&gt;Java Help, Java Tutorials, Java Programming, Java Tricks&lt;/a&gt;&lt;/p&gt;&lt;br&gt;lesson four&lt;br /&gt;&lt;p&gt;Working with Objects&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;CONTENTS&lt;br&gt;&lt;br /&gt;&lt;a href="http://www.sbinfosystem.com/"&gt;Creating New Objects&lt;/a&gt;&lt;br&gt;&lt;br /&gt;Using new&lt;br&gt;&lt;br /&gt;What new Does&lt;br&gt;&lt;br /&gt;A Note on Memory Management&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;Accessing and Setting Class and Instance Variables&lt;br&gt;&lt;br /&gt;Getting Values&lt;br&gt;&lt;br /&gt;Changing Values&lt;br&gt;&lt;br /&gt;Class Variables&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;Calling Methods&lt;br&gt;&lt;br /&gt;Class Methods&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;References to Objects&lt;br&gt;&lt;br /&gt;Casting and Converting Objects and Primitive Types&lt;br&gt;&lt;br /&gt;Casting Primitive Types&lt;br&gt;&lt;br /&gt;Casting Objects&lt;br&gt;&lt;br /&gt;Converting Primitive Types to Objects and Vice Versa&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;Odds and Ends&lt;br&gt;&lt;br /&gt;Comparing Objects&lt;br&gt;&lt;br /&gt;Determining the Class of an Object&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;Class and Object Reflection (Java 1.1)&lt;br&gt;&lt;br /&gt;&lt;a href="http://www.sbinfosystem.com/"&gt;The Java Class Library&lt;/a&gt;&lt;br&gt;&lt;br /&gt;Summary&lt;br&gt;&lt;br /&gt;Q&amp;amp;A&lt;/p&gt;&lt;br /&gt;&lt;p&gt;&lt;/b&gt;&lt;br&gt;&lt;br /&gt;Let's start today's lesson with an obvious statement: Because Java is an &lt;br /&gt;object-oriented language, you're&lt;br&gt;&lt;br /&gt;going to be dealing with a lot of objects. You'll create them, modify them, move &lt;br /&gt;them around, change&lt;br&gt;&lt;br /&gt;their variables, call their methods, combine them with other objects-and, of &lt;br /&gt;course, develop classes and&lt;br&gt;&lt;br /&gt;use your own objects in the mix.&lt;br&gt;&lt;br /&gt;Today, therefore, you'll learn all about the Java object in its natural habitat. &lt;br /&gt;Today's topics include&lt;br&gt;&lt;br /&gt;Creating instances of classes&lt;br&gt;&lt;br /&gt;Testing and modifying class and instance variables in your new instance&lt;br&gt;&lt;br /&gt;Calling methods in that object&lt;br&gt;&lt;br /&gt;Casting (converting) objects and other data types from one class to another&lt;br&gt;&lt;br /&gt;Other odds and ends about working with objects&lt;br&gt;&lt;br /&gt;An overview of the Java class libraries&lt;/p&gt;&lt;br /&gt;&lt;p&gt;&lt;br&gt;&lt;br /&gt;Creating New Objects&lt;br&gt;&lt;br /&gt;When you write a Java program, you define a set of classes. As you learned on &lt;br /&gt;Day 2, &amp;quot;Object-Oriented&lt;br&gt;&lt;br /&gt;Programming and Java,&amp;quot; classes are templates for objects; for the most part, you &lt;br /&gt;merely use the class to&lt;br&gt;&lt;br /&gt;create instances and then work with those instances. In this section, therefore, &lt;br /&gt;you'll learn how to create a&lt;br&gt;&lt;br /&gt;new object from any given class.&lt;br&gt;&lt;br /&gt;Remember strings from yesterday? You learned that using a string literal-a &lt;br /&gt;series of characters enclosed in&lt;br&gt;&lt;br /&gt;double-quotes-creates a new instance of the class String with the value of that &lt;br /&gt;string.&lt;br&gt;&lt;br /&gt;The String class is unusual in that respect-although it's a class, there's an &lt;br /&gt;easy way to create instances of&lt;br&gt;&lt;br /&gt;that class using a literal. The other classes don't have that shortcut; to &lt;br /&gt;create instances of those classes you&lt;br&gt;&lt;br /&gt;have to do so explicitly by using the new operator.&lt;br&gt;&lt;br /&gt;Note&lt;br&gt;&lt;br /&gt;What about the literals for numbers and characters? Don't they create&lt;br&gt;&lt;br /&gt;objects, too? Actually, they don't. The primitive data types for&lt;br&gt;&lt;br /&gt;numbers and characters create numbers and characters, but for&lt;br&gt;&lt;br /&gt;efficiency, they aren't actually objects. You can put object wrappers&lt;br&gt;&lt;br /&gt;around them if you need to treat them like objects (you'll learn how to&lt;br&gt;&lt;br /&gt;do this in &amp;quot;Casting and `Converting Objects and Primitive Types&amp;quot;).&lt;br&gt;&lt;br /&gt;Using new&lt;br&gt;&lt;br /&gt;To create a new object, you use the new operator with the name of the class you &lt;br /&gt;want to create an instance&lt;br&gt;&lt;br /&gt;of, then parentheses after that. The following examples create new instances of &lt;br /&gt;the classes String,&lt;br&gt;&lt;br /&gt;Random, and Motorcycle, and store those new instances in variables of the &lt;br /&gt;appropriate types:&lt;br&gt;&lt;br /&gt;String str = new String();&lt;br&gt;&lt;br /&gt;Random r = new Random();&lt;br&gt;&lt;br /&gt;Motorcycle m2 = new Motorcycle();&lt;br&gt;&lt;br /&gt;The parentheses are important; don't leave them off. The parentheses can be &lt;br /&gt;empty (as in these examples),&lt;br&gt;&lt;br /&gt;in which case the most simple, basic object is created; or the parentheses can &lt;br /&gt;contain arguments that&lt;br&gt;&lt;br /&gt;determine the initial values of instance variables or other initial qualities of &lt;br /&gt;that object:&lt;br&gt;&lt;br /&gt;Date dt = new Date(90, 4, 1, 4, 30);&lt;br&gt;&lt;br /&gt;Point pt = new Point(0,0);&lt;br&gt;&lt;br /&gt;The number and type of arguments you can use inside the parentheses with new are &lt;br /&gt;defined by the class&lt;br&gt;&lt;br /&gt;itself using a special method called a constructor (you'll learn more about &lt;br /&gt;constructors later today). If you&lt;br&gt;&lt;br /&gt;try and create a new instance of a class with the wrong number or type of &lt;br /&gt;arguments (or if you give it no&lt;br&gt;&lt;br /&gt;arguments and it needs some), then you'll get an error when you try to compile &lt;br /&gt;your Java program.&lt;br&gt;&lt;br /&gt;Here's an example of creating several different types of objects using different &lt;br /&gt;numbers and types of&lt;br&gt;&lt;br /&gt;arguments. The Date class, part of the java.util package, creates objects that &lt;br /&gt;represent the current&lt;br&gt;&lt;br /&gt;date. Listing 4.1 is a Java program that shows three different ways of creating &lt;br /&gt;a Date object using new.&lt;br&gt;&lt;br /&gt;Listing 4.1. Laura's Date program.&lt;br&gt;&lt;br /&gt;1: &lt;a href="http://www.sbinfosystem.com/"&gt;import java.util.Date&lt;/a&gt;;&lt;br&gt;&lt;br /&gt;2:&lt;br&gt;&lt;br /&gt;3: &lt;a href="http://www.sbinfosystem.com/"&gt;class CreateDates&lt;/a&gt; {&lt;br&gt;&lt;br /&gt;4:&lt;br&gt;&lt;br /&gt;5: public static void main(String args[]) {&lt;br&gt;&lt;br /&gt;6: Date d1, d2, d3;&lt;br&gt;&lt;br /&gt;7:&lt;br&gt;&lt;br /&gt;8: d1 = new Date();&lt;br&gt;&lt;br /&gt;9: System.out.println(&amp;quot;Date 1: &amp;quot; + d1);&lt;br&gt;&lt;br /&gt;10:&lt;br&gt;&lt;br /&gt;11: d2 = new Date(71, 7, 1, 7, 30);&lt;br&gt;&lt;br /&gt;12: System.out.println(&amp;quot;Date 2: &amp;quot; + d2);&lt;br&gt;&lt;br /&gt;13:&lt;br&gt;&lt;br /&gt;14: d3 = new Date(&amp;quot;April 3 1993 3:24 PM&amp;quot;);&lt;br&gt;&lt;br /&gt;15: System.out.println(&amp;quot;Date 3: &amp;quot; + d3);&lt;br&gt;&lt;br /&gt;16: }&lt;br&gt;&lt;br /&gt;17: }&lt;br&gt;&lt;br /&gt;Date 1: Tue Feb 13 09:36:56 PST 1996&lt;br&gt;&lt;br /&gt;Date 2: Sun Aug 01 07:30:00 PDT 1971&lt;br&gt;&lt;br /&gt;Date 3: Sat Apr 03 15:24:00 PST 1993&lt;br&gt;&lt;br /&gt;Analysis&lt;br&gt;&lt;br /&gt;In this example, three different date objects are created using different&lt;br&gt;&lt;br /&gt;arguments to the class listed after new. The first instance (line 8) uses&lt;br&gt;&lt;br /&gt;new Date() with no arguments, which creates a Date object for&lt;br&gt;&lt;br /&gt;today's date (the first line of the output shows a sample; your output&lt;br&gt;&lt;br /&gt;will, of course, read the current date and time for you).&lt;br&gt;&lt;br /&gt;The second Date object you create in this example has five integer arguments. &lt;br /&gt;The arguments represent a&lt;br&gt;&lt;br /&gt;date: year, month, day, hours, and minutes. And, as the output shows, this &lt;br /&gt;creates a Date object for that&lt;br&gt;&lt;br /&gt;particular date: Sunday, August 1, 1971, at 7:30 a.m.&lt;br&gt;&lt;br /&gt;Note&lt;br&gt;&lt;br /&gt;Java numbers months starting from 0. So although you might expect&lt;br&gt;&lt;br /&gt;the seventh month to be July, month 7 in Java is indeed August.&lt;br&gt;&lt;br /&gt;The third version of Date takes one argument, a string, representing the date as &lt;br /&gt;a text string. When the&lt;br&gt;&lt;br /&gt;Date object is created, that string is parsed, and a Date object with that date &lt;br /&gt;and time is created (see the&lt;br&gt;&lt;br /&gt;third line of output). The date string can take many different formats; see the &lt;br /&gt;API documentation for the&lt;br&gt;&lt;br /&gt;Date class (part of the java.util package) for information about what strings &lt;br /&gt;you can use.&lt;br&gt;&lt;br /&gt;What new Does&lt;br&gt;&lt;br /&gt;When you use the new operator, the new instance of the given class is created, &lt;br /&gt;and memory is allocated&lt;br&gt;&lt;br /&gt;for it. In addition (and most importantly), a special method defined in the &lt;br /&gt;given class is called to initialize&lt;br&gt;&lt;br /&gt;the object and set up any initial values it needs. This special method is called &lt;br /&gt;a constructor. Constructors&lt;br&gt;&lt;br /&gt;are special methods, defined in classes, that create and initialize new &lt;br /&gt;instances of classes.&lt;br&gt;&lt;br /&gt;New Term&lt;br&gt;&lt;br /&gt;Constructors are special methods that initialize a new object, set its&lt;br&gt;&lt;br /&gt;variables, create any other objects that object needs, and generally&lt;br&gt;&lt;br /&gt;perform any other operations the object needs to initialize itself.&lt;br&gt;&lt;br /&gt;Multiple constructor definitions in a class can each have a different number or &lt;br /&gt;type of arguments-then,&lt;br&gt;&lt;br /&gt;when you use new, you can specify different arguments in the argument list, and &lt;br /&gt;the right constructor for&lt;br&gt;&lt;br /&gt;those arguments will be called. That's how each of those different versions of &lt;br /&gt;new that you used in the&lt;br&gt;&lt;br /&gt;CreateDates class can create different Date objects.&lt;br&gt;&lt;br /&gt;When you create your own classes, you can define as many constructors as you &lt;br /&gt;need to implement that&lt;br&gt;&lt;br /&gt;class's behavior. You'll learn how to create constructors on Day 7, &amp;quot;More About &lt;br /&gt;Methods.&amp;quot;&lt;br&gt;&lt;br /&gt;A Note on Memory Management&lt;br&gt;&lt;br /&gt;Memory management in Java is dynamic and automatic. When you create a new object &lt;br /&gt;in Java, Java&lt;br&gt;&lt;br /&gt;automatically allocates the right amount of memory for that object in the heap. &lt;br /&gt;You don't have to allocate&lt;br&gt;&lt;br /&gt;any memory for any objects explicitly; Java does it for you.&lt;br&gt;&lt;br /&gt;What happens when you're finished with that object? How do you de-allocate the &lt;br /&gt;memory that object&lt;br&gt;&lt;br /&gt;uses? The answer, again, is that memory management is automatic. Once you're &lt;br /&gt;done with an object, you&lt;br&gt;&lt;br /&gt;reassign all the variables that might hold that object and remove it from any &lt;br /&gt;arrays, thereby making the&lt;br&gt;&lt;br /&gt;object unusable. Java has a &amp;quot;garbage collector&amp;quot; that looks for unused objects &lt;br /&gt;and reclaims the memory that&lt;br&gt;&lt;br /&gt;those objects are using. You don't have to do any explicit freeing of memory; &lt;br /&gt;you just have to make sure&lt;br&gt;&lt;br /&gt;you're not still holding onto an object you want to get rid of. You'll learn &lt;br /&gt;more specific details about the&lt;br&gt;&lt;br /&gt;Java garbage collector and how it works on Day 21, &amp;quot;Under the Hood.&amp;quot;&lt;br&gt;&lt;br /&gt;New Term&lt;br&gt;&lt;br /&gt;A garbage collector is a special thing built into the Java environment&lt;br&gt;&lt;br /&gt;that looks for unused objects. If it finds any, it automatically removes&lt;br&gt;&lt;br /&gt;those objects and frees the memory those objects were using.&lt;br&gt;&lt;br /&gt;Accessing and Setting Class and Instance Variables&lt;br&gt;&lt;br /&gt;Now you have your very own object, and that object may have class or instance &lt;br /&gt;variables defined in it.&lt;br&gt;&lt;br /&gt;How do you work with those variables? Easy! Class and instance variables behave &lt;br /&gt;in exactly the same&lt;br&gt;&lt;br /&gt;ways as the local variables you learned about yesterday; you just refer to them &lt;br /&gt;slightly differently than you&lt;br&gt;&lt;br /&gt;do regular variables in your code.&lt;br&gt;&lt;br /&gt;Getting Values&lt;br&gt;&lt;br /&gt;To get to the value of an instance variable, you use an expression in what's &lt;br /&gt;called dot notation. With dot&lt;br&gt;&lt;br /&gt;notation, the reference to an instance or class variable has two parts: the &lt;br /&gt;object on the left side of the dot&lt;br&gt;&lt;br /&gt;and the variable on the right side of the dot.&lt;br&gt;&lt;br /&gt;New Term&lt;br&gt;&lt;br /&gt;Dot notation is an expression used to get at instance variables and&lt;br&gt;&lt;br /&gt;methods inside a given object.&lt;br&gt;&lt;br /&gt;For example, if you have an object assigned to the variable myObject, and that &lt;br /&gt;object has a variable&lt;br&gt;&lt;br /&gt;called var, you refer to that variable's value like this:&lt;br&gt;&lt;br /&gt;myObject.var;&lt;br&gt;&lt;br /&gt;This form for accessing variables is an expression (it returns a value), and &lt;br /&gt;both sides of the dot can also be&lt;br&gt;&lt;br /&gt;expressions. This means that you can nest instance variable access. If that var &lt;br /&gt;instance variable itself&lt;br&gt;&lt;br /&gt;holds an object and that object has its own instance variable called state, you &lt;br /&gt;could refer to it like this:&lt;br&gt;&lt;br /&gt;myObject.var.state;&lt;br&gt;&lt;br /&gt;Dot expressions are evaluated left to right, so you start with myObject's &lt;br /&gt;variable var, which points to&lt;br&gt;&lt;br /&gt;another object with the variable state. You end up with the value of that state &lt;br /&gt;variable after the entire&lt;br&gt;&lt;br /&gt;expression is done evaluating.&lt;br&gt;&lt;br /&gt;Changing Values&lt;br&gt;&lt;br /&gt;Assigning a value to that variable is equally easy-just tack an assignment &lt;br /&gt;operator on the right side of the&lt;br&gt;&lt;br /&gt;expression:&lt;br&gt;&lt;br /&gt;myObject.var.state = true;&lt;br&gt;&lt;br /&gt;Listing 4.2 is an example of a program that tests and modifies the instance &lt;br /&gt;variables in a Point object.&lt;br&gt;&lt;br /&gt;Point is part of the java.awt package and refers to a coordinate point with an x &lt;br /&gt;and a y value.&lt;br&gt;&lt;br /&gt;Listing 4.2. The TestPoint Class.&lt;br&gt;&lt;br /&gt;1: import java.awt.Point;&lt;br&gt;&lt;br /&gt;2:&lt;br&gt;&lt;br /&gt;3: class TestPoint {&lt;br&gt;&lt;br /&gt;4: public static void main(String args[]) {&lt;br&gt;&lt;br /&gt;5: Point thePoint = new Point(10,10);&lt;br&gt;&lt;br /&gt;6:&lt;br&gt;&lt;br /&gt;7: System.out.println(&amp;quot;X is &amp;quot; + thePoint.x);&lt;br&gt;&lt;br /&gt;8: System.out.println(&amp;quot;Y is &amp;quot; + thePoint.y);&lt;br&gt;&lt;br /&gt;9:&lt;br&gt;&lt;br /&gt;10: System.out.println(&amp;quot;Setting X to 5.&amp;quot;);&lt;br&gt;&lt;br /&gt;11: thePoint.x = 5;&lt;br&gt;&lt;br /&gt;12: System.out.println(&amp;quot;Setting Y to 15.&amp;quot;);&lt;br&gt;&lt;br /&gt;13: thePoint.y = 15;&lt;br&gt;&lt;br /&gt;14:&lt;br&gt;&lt;br /&gt;15: System.out.println(&amp;quot;X is &amp;quot; + thePoint.x);&lt;br&gt;&lt;br /&gt;16: System.out.println(&amp;quot;Y is &amp;quot; + thePoint.y);&lt;br&gt;&lt;br /&gt;17:&lt;br&gt;&lt;br /&gt;18: }&lt;br&gt;&lt;br /&gt;19:}&lt;br&gt;&lt;br /&gt;X is 10&lt;br&gt;&lt;br /&gt;Y is 10&lt;br&gt;&lt;br /&gt;Setting X to 5.&lt;br&gt;&lt;br /&gt;Setting Y to 15.&lt;br&gt;&lt;br /&gt;X is 5&lt;br&gt;&lt;br /&gt;Y is 15&lt;br&gt;&lt;br /&gt;Analysis&lt;br&gt;&lt;br /&gt;In this example, you first create an instance of Point where X and Y&lt;br&gt;&lt;br /&gt;are both 10 (line 6). Lines 8 and 9 print out those individual values,&lt;br&gt;&lt;br /&gt;and you can see dot notation at work there. Lines 11 through 14&lt;br&gt;&lt;br /&gt;change the values of those variables to 5 and 15, respectively.&lt;br&gt;&lt;br /&gt;Finally, lines 16 and 17 print out the values of X and Y again to show&lt;br&gt;&lt;br /&gt;how they've changed.&lt;/p&gt;&lt;br /&gt;&lt;p&gt;&lt;br&gt;&lt;br /&gt;Class Variables&lt;br&gt;&lt;br /&gt;Class variables, as you've already learned, are variables that are defined and &lt;br /&gt;stored in the class itself. Their&lt;br&gt;&lt;br /&gt;values, therefore, apply to the class and to all its instances.&lt;br&gt;&lt;br /&gt;With instance variables, each new instance of the class gets a new copy of the &lt;br /&gt;instance variables that class&lt;br&gt;&lt;br /&gt;defines. Each instance can then change the values of those instance variables &lt;br /&gt;without affecting any other&lt;br&gt;&lt;br /&gt;instances. With class variables, there is only one copy of that variable. Every &lt;br /&gt;instance of the class has&lt;br&gt;&lt;br /&gt;access to that variable, but there is only one value. Changing the value of that &lt;br /&gt;variable changes it for all&lt;br&gt;&lt;br /&gt;the instances of that class.&lt;br&gt;&lt;br /&gt;You define class variables by including the static keyword before the variable &lt;br /&gt;itself. You'll learn more&lt;br&gt;&lt;br /&gt;about this on Day 6, &amp;quot;Creating Classes and Applications in Java.&amp;quot; For example, &lt;br /&gt;take the following partial&lt;br&gt;&lt;br /&gt;class definition:&lt;br&gt;&lt;br /&gt;class FamilyMember {&lt;br&gt;&lt;br /&gt;static String surname = &amp;quot;Johnson&amp;quot;;&lt;br&gt;&lt;br /&gt;String name;&lt;br&gt;&lt;br /&gt;int age;&lt;br&gt;&lt;br /&gt;...&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;Instances of the class FamilyMember each have their own values for name and age. &lt;br /&gt;But the class&lt;br&gt;&lt;br /&gt;variable surname has only one value for all family members. Change surname, and &lt;br /&gt;all the instances of&lt;br&gt;&lt;br /&gt;FamilyMember are affected.&lt;br&gt;&lt;br /&gt;To access class variables, you use the same dot notation as you do with instance &lt;br /&gt;variables. To get or&lt;br&gt;&lt;br /&gt;change the value of the class variable, you can use either the instance or the &lt;br /&gt;name of the class on the left&lt;br&gt;&lt;br /&gt;side of the dot. Both of the lines of output in this example print the same &lt;br /&gt;value:&lt;br&gt;&lt;br /&gt;FamilyMember dad = new FamilyMember();&lt;br&gt;&lt;br /&gt;System.out.println(&amp;quot;Family's surname is: &amp;quot; + dad.surname);&lt;br&gt;&lt;br /&gt;System.out.println(&amp;quot;Family's surname is: &amp;quot; + FamilyMember.surname);&lt;br&gt;&lt;br /&gt;Because you can use an instance to change the value of a class variable, it's &lt;br /&gt;easy to become confused&lt;br&gt;&lt;br /&gt;about class variables and where their values are coming from (remember that the &lt;br /&gt;value of a class variable&lt;br&gt;&lt;br /&gt;affects all the instances). For this reason, it's a good idea to use the name of &lt;br /&gt;the class when you refer to a&lt;br&gt;&lt;br /&gt;class variable-it makes your code easier to read and strange results easier to &lt;br /&gt;debug.&lt;br&gt;&lt;br /&gt;Calling Methods&lt;br&gt;&lt;br /&gt;Calling a method is similar to referring to an object's instance variables: &lt;br /&gt;Method calls to objects also use&lt;br&gt;&lt;br /&gt;dot notation. The object itself whose method you're calling is on the left side &lt;br /&gt;of the dot; the name of the&lt;br&gt;&lt;br /&gt;method and its arguments are on the right side of the dot:&lt;br&gt;&lt;br /&gt;myObject.methodOne(arg1, arg2, arg3);&lt;br&gt;&lt;br /&gt;Note that all calls to methods must have parentheses after them, even if that &lt;br /&gt;method takes no arguments:&lt;br&gt;&lt;br /&gt;myObject.methodNoArgs();&lt;br&gt;&lt;br /&gt;If the method you've called returns an object that itself has methods, you can &lt;br /&gt;nest methods as you would&lt;br&gt;&lt;br /&gt;variables. This next example calls the getName() method, which is defined in the &lt;br /&gt;object returned by the&lt;br&gt;&lt;br /&gt;getClass() method, which was defined in myObject. Got it?&lt;br&gt;&lt;br /&gt;myObject.getClass().getName();&lt;br&gt;&lt;br /&gt;You can combine nested method calls and instance variable references as well (in &lt;br /&gt;this case you're calling&lt;br&gt;&lt;br /&gt;the methodTwo() method, which is defined in the object stored by the var &lt;br /&gt;instance variable, which in&lt;br&gt;&lt;br /&gt;turn is part of the myObject object):&lt;br&gt;&lt;br /&gt;myObject.var.methodTwo(arg1, arg2);&lt;br&gt;&lt;br /&gt;System.out.println(), the method you've been using through the book this far to &lt;br /&gt;print out bits of&lt;br&gt;&lt;br /&gt;text, is a great example of nesting variables and methods. The System class &lt;br /&gt;(part of the java.lang&lt;br&gt;&lt;br /&gt;package) describes system-specific behavior. System.out is a class variable that &lt;br /&gt;contains an instance of&lt;br&gt;&lt;br /&gt;the class PrintStream that points to the standard output of the system. &lt;br /&gt;PrintStream instances have&lt;br&gt;&lt;br /&gt;a println() method that prints a string to that output stream.&lt;br&gt;&lt;br /&gt;Listing 4.3 shows an example of calling some methods defined in the String &lt;br /&gt;class. Strings include&lt;br&gt;&lt;br /&gt;methods for string tests and modification, similar to what you would expect in a &lt;br /&gt;string library in other&lt;br&gt;&lt;br /&gt;languages.&lt;br&gt;&lt;br /&gt;Listing 4.3. Several uses of String methods.&lt;br&gt;&lt;br /&gt;1: class TestString {&lt;br&gt;&lt;br /&gt;2:&lt;br&gt;&lt;br /&gt;3: public static void main(String args[]) {&lt;br&gt;&lt;br /&gt;4: String str = &amp;quot;Now is the winter of our discontent&amp;quot;;&lt;br&gt;&lt;br /&gt;5:&lt;br&gt;&lt;br /&gt;6: System.out.println(&amp;quot;The string is: &amp;quot; + str);&lt;br&gt;&lt;br /&gt;7: System.out.println(&amp;quot;Length of this string: &amp;quot;&lt;br&gt;&lt;br /&gt;8: + str.length());&lt;br&gt;&lt;br /&gt;9: System.out.println(&amp;quot;The character at position 5: &amp;quot;&lt;br&gt;&lt;br /&gt;10: + str.charAt(5));&lt;br&gt;&lt;br /&gt;11: System.out.println(&amp;quot;The substring from 11 to 17: &amp;quot;&lt;br&gt;&lt;br /&gt;12: + str.substring(11, 17));&lt;br&gt;&lt;br /&gt;13: System.out.println(&amp;quot;The index of the character d: &amp;quot;&lt;br&gt;&lt;br /&gt;14: + str.indexOf('d'));&lt;br&gt;&lt;br /&gt;15: System.out.print(&amp;quot;The index of the beginning of the &amp;quot;);&lt;br&gt;&lt;br /&gt;16: System.out.println(&amp;quot;substring \&amp;quot;winter\&amp;quot;: &amp;quot;&lt;br&gt;&lt;br /&gt;17: + str.indexOf(&amp;quot;winter&amp;quot;));&lt;br&gt;&lt;br /&gt;18: System.out.println(&amp;quot;The string in upper case: &amp;quot;&lt;br&gt;&lt;br /&gt;19: + str.toUpperCase());&lt;br&gt;&lt;br /&gt;20: }&lt;br&gt;&lt;br /&gt;21: }&lt;br&gt;&lt;br /&gt;The string is: Now is the winter of our discontent&lt;br&gt;&lt;br /&gt;Length of this string: 35&lt;br&gt;&lt;br /&gt;The character at position 5: s&lt;br&gt;&lt;br /&gt;The substring from positions 11 to 17: winter&lt;br&gt;&lt;br /&gt;The index of the character d: 25&lt;br&gt;&lt;br /&gt;The index of the beginning of the substring &amp;quot;winter&amp;quot;: 11&lt;br&gt;&lt;br /&gt;The string in upper case: NOW IS THE WINTER OF OUR DISCONTENT&lt;br&gt;&lt;br /&gt;Analysis&lt;br&gt;&lt;br /&gt;In line 4, you create a new instance of String by using a string&lt;br&gt;&lt;br /&gt;literal (it's easier that way than using new and then putting the&lt;br&gt;&lt;br /&gt;characters in individually). The remainder of the program simply calls&lt;br&gt;&lt;br /&gt;different string methods to do different operations on that string:&lt;br&gt;&lt;br /&gt;Line 6 prints the value of the string we created in line 4: &amp;quot;Now is the winter &lt;br /&gt;of our&lt;br&gt;&lt;br /&gt;discontent&amp;quot;.&lt;br&gt;&lt;br /&gt;l &lt;br&gt;&lt;br /&gt;Line 7 calls the length() l method in the new String object. This string has 35 &lt;br /&gt;characters.&lt;br&gt;&lt;br /&gt;Line 9 calls the charAt() method, which returns the character at the given &lt;br /&gt;position in the string.&lt;br&gt;&lt;br /&gt;Note that string positions start at 0, so the character at position 5 is s.&lt;br&gt;&lt;br /&gt;l &lt;br&gt;&lt;br /&gt;Line 11 calls the substring() method, which takes two integers indicating a &lt;br /&gt;range and returns&lt;br&gt;&lt;br /&gt;the substring at those starting and ending points. The substring() method can &lt;br /&gt;also be called&lt;br&gt;&lt;br /&gt;with only one argument, which returns the substring from that position to the &lt;br /&gt;end of the string.&lt;br&gt;&lt;br /&gt;l &lt;br&gt;&lt;br /&gt;Line 13 calls the indexOf() method, which returns the position of the first &lt;br /&gt;instance of the given&lt;br&gt;&lt;br /&gt;character (here, 'd').&lt;br&gt;&lt;br /&gt;l &lt;br&gt;&lt;br /&gt;Line 15 shows a different use of the indexOf() method, which takes a string &lt;br /&gt;argument and&lt;br&gt;&lt;br /&gt;returns the index of the beginning of that string.&lt;br&gt;&lt;br /&gt;l &lt;br&gt;&lt;br /&gt;l Finally, line 19 uses the toUpperCase() method to return a copy of the string &lt;br /&gt;in all uppercase.&lt;br&gt;&lt;br /&gt;Class Methods&lt;br&gt;&lt;br /&gt;Class methods, like class variables, apply to the class as a whole and not to &lt;br /&gt;its instances. Class methods&lt;br&gt;&lt;br /&gt;are commonly used for general utility methods that may not operate directly on &lt;br /&gt;an instance of that class,&lt;br&gt;&lt;br /&gt;but fit with that class conceptually. For example, the String class contains a &lt;br /&gt;class method called&lt;br&gt;&lt;br /&gt;valueOf(), which can take one of many different types of arguments (integers, &lt;br /&gt;booleans, other objects,&lt;br&gt;&lt;br /&gt;and so on). The valueOf() method then returns a new instance of String &lt;br /&gt;containing the string value&lt;br&gt;&lt;br /&gt;of the argument it was given. This method doesn't operate directly on an &lt;br /&gt;existing instance of String, but&lt;br&gt;&lt;br /&gt;getting a string from another object or data type is definitely a String-like &lt;br /&gt;operation, and it makes sense&lt;br&gt;&lt;br /&gt;to define it in the String class.&lt;br&gt;&lt;br /&gt;Class methods can also be useful for gathering general methods together in one &lt;br /&gt;place (the class). For&lt;br&gt;&lt;br /&gt;example, the Math class, defined in the java.lang package, contains a large set &lt;br /&gt;of mathematical&lt;br&gt;&lt;br /&gt;operations as class methods-there are no instances of the class Math, but you &lt;br /&gt;can still use its methods&lt;br&gt;&lt;br /&gt;with numeric or boolean arguments. For example, the class method Math.max() &lt;br /&gt;takes two arguments&lt;br&gt;&lt;br /&gt;and returns the larger of the two. You don't need to create a new instance of &lt;br /&gt;Math; just call the method&lt;br&gt;&lt;br /&gt;anywhere you need it, like this:&lt;br&gt;&lt;br /&gt;in biggerOne = Math.max(x, y);&lt;br&gt;&lt;br /&gt;To call a class method, you use dot notation as you do with instance methods. As &lt;br /&gt;with class variables, you&lt;br&gt;&lt;br /&gt;can use either an instance of the class or the class itself on the left site of &lt;br /&gt;the dot. However, for the same&lt;br&gt;&lt;br /&gt;reasons noted in the discussion on class variables, using the name of the class &lt;br /&gt;for class methods makes&lt;br&gt;&lt;br /&gt;your code easier to read. The last two lines in this example produce the same &lt;br /&gt;result (the string &amp;quot;5&amp;quot;):&lt;br&gt;&lt;br /&gt;String s, s2;&lt;br&gt;&lt;br /&gt;s = &amp;quot;foo&amp;quot;;&lt;br&gt;&lt;br /&gt;s2 = s.valueOf(5);&lt;br&gt;&lt;br /&gt;s2 = String.valueOf(5);&lt;br&gt;&lt;br /&gt;References to Objects&lt;br&gt;&lt;br /&gt;As you work with objects, one important thing going on behind the scenes is the &lt;br /&gt;use of references to those&lt;br&gt;&lt;br /&gt;objects. When you assign objects to variables, or pass objects as arguments to &lt;br /&gt;methods, you are passing&lt;br&gt;&lt;br /&gt;references to those objects, not the objects themselves or copies of those &lt;br /&gt;objects.&lt;br&gt;&lt;br /&gt;An example should make this clearer. Examine Listing 4.4, which shows a simple &lt;br /&gt;example of how&lt;br&gt;&lt;br /&gt;references work.&lt;br&gt;&lt;br /&gt;Listing 4.4. A references example.&lt;br&gt;&lt;br /&gt;1: import java.awt.Point;&lt;br&gt;&lt;br /&gt;2:&lt;br&gt;&lt;br /&gt;3: class ReferencesTest {&lt;br&gt;&lt;br /&gt;4: public static void main (String args[]) {&lt;br&gt;&lt;br /&gt;5: Point pt1, pt2;&lt;br&gt;&lt;br /&gt;6: pt1 = new Point(100, 100);&lt;br&gt;&lt;br /&gt;7: pt2 = pt1;&lt;br&gt;&lt;br /&gt;8:&lt;br&gt;&lt;br /&gt;9: pt1.x = 200;&lt;br&gt;&lt;br /&gt;10: pt1.y = 200;&lt;br&gt;&lt;br /&gt;11: System.out.println(&amp;quot;Point1: &amp;quot; + pt1.x + &amp;quot;, &amp;quot; + pt1.y);&lt;br&gt;&lt;br /&gt;12: System.out.println(&amp;quot;Point2: &amp;quot; + pt2.x + &amp;quot;, &amp;quot; + pt2.y);&lt;br&gt;&lt;br /&gt;13: }&lt;br&gt;&lt;br /&gt;14: }&lt;br&gt;&lt;br /&gt;Point1: 200, 200&lt;br&gt;&lt;br /&gt;Point2: 200, 200&lt;br&gt;&lt;br /&gt;Analysis&lt;br&gt;&lt;br /&gt;In the first part of this program, you declare two variables of type&lt;br&gt;&lt;br /&gt;Point (line 5), create a new Point object to pt1 (line 6), and&lt;br&gt;&lt;br /&gt;finally, assign the value of pt1 to pt2 (line 7).&lt;br&gt;&lt;br /&gt;Now, here's the challenge. After changing pt1's x and y instance variables in &lt;br /&gt;lines 9 and 10, what will&lt;br&gt;&lt;br /&gt;pt2 look like?&lt;br&gt;&lt;br /&gt;As you can see, pt2's x and y instance variables were also changed, even though &lt;br /&gt;you never explicitly&lt;br&gt;&lt;br /&gt;changed them. When you assign the value of pt1 to pt2, you actually create a &lt;br /&gt;reference from pt2 to the&lt;br&gt;&lt;br /&gt;same object to which pt1 refers (see Figure 4.1). Change the object that pt2 &lt;br /&gt;refers to, and you also&lt;br&gt;&lt;br /&gt;change the object that pt1 points to, because both are references to the same &lt;br /&gt;object.&lt;br&gt;&lt;br /&gt;Figure 4.1 : References to objects.&lt;br&gt;&lt;br /&gt;Note&lt;br&gt;&lt;br /&gt;If you actually do want pt1 and pt2 to point to separate objects, you&lt;br&gt;&lt;br /&gt;should use new Point() for both lines to create separate objects.&lt;br&gt;&lt;br /&gt;The fact that Java uses references becomes particularly important when you pass &lt;br /&gt;arguments to methods.&lt;br&gt;&lt;br /&gt;You'll learn more about this later today, but keep these references in mind.&lt;br&gt;&lt;br /&gt;Technical Note&lt;br&gt;&lt;br /&gt;There are no explicit pointers or pointer arithmetic in Java as there are&lt;br&gt;&lt;br /&gt;in C-like languages-just references. However, with these references,&lt;br&gt;&lt;br /&gt;and with Java arrays, you have most of the capabilities that you have&lt;br&gt;&lt;br /&gt;with pointers without the confusion and lurking bugs that explicit&lt;br&gt;&lt;br /&gt;pointers can create.&lt;br&gt;&lt;br /&gt;Casting and Converting Objects and Primitive&lt;br&gt;&lt;br /&gt;Types&lt;br&gt;&lt;br /&gt;Sometimes in your Java programs you may have a value stored somewhere that is &lt;br /&gt;the wrong type for what&lt;br&gt;&lt;br /&gt;you want to do with it. Maybe it's an instance of the wrong class, or perhaps &lt;br /&gt;it's a float and you want it&lt;br&gt;&lt;br /&gt;to be an int. To convert the value of one type to another, you use casting. &lt;br /&gt;Casting is a programming term&lt;br&gt;&lt;br /&gt;that means, effectively, converting a value or an object from one type to &lt;br /&gt;another. The result of a cast is a&lt;br&gt;&lt;br /&gt;new value or object; casting does not change the original object or value.&lt;br&gt;&lt;br /&gt;New Time&lt;br&gt;&lt;br /&gt;Casting converts the value of an object or primitive type into another&lt;br&gt;&lt;br /&gt;type.&lt;br&gt;&lt;br /&gt;Although the concept of casting is a simple one, the rules for what types in &lt;br /&gt;Java can be converted to what&lt;br&gt;&lt;br /&gt;other types are complicated by the fact that Java has both primitive types (int, &lt;br /&gt;float, boolean), and&lt;br&gt;&lt;br /&gt;object types (String, Point, Window, and so on). There are three forms of casts &lt;br /&gt;and conversions to&lt;br&gt;&lt;br /&gt;talk about in this section:&lt;br&gt;&lt;br /&gt;Casting between primitive types: int to float or float l to double&lt;br&gt;&lt;br /&gt;l Casting between object types: an instance of a class to an instance of another &lt;br /&gt;class&lt;br&gt;&lt;br /&gt;l Converting primitive types to objects and then extracting primitive values &lt;br /&gt;back out of those objects&lt;br&gt;&lt;br /&gt;Casting Primitive Types&lt;br&gt;&lt;br /&gt;Casting between primitive types allows you to &amp;quot;convert&amp;quot; the value of one type to &lt;br /&gt;another primitive&lt;br&gt;&lt;br /&gt;type-for example, to assign a number of one type to a variable of another type. &lt;br /&gt;Casting between primitive&lt;br&gt;&lt;br /&gt;types most commonly occurs with the numeric types; boolean values cannot be cast &lt;br /&gt;to any other primitive&lt;br&gt;&lt;br /&gt;type.&lt;br&gt;&lt;br /&gt;Often, if the type you are casting to is &amp;quot;larger&amp;quot; than the type of the value &lt;br /&gt;you're converting, you may not&lt;br&gt;&lt;br /&gt;have to use an explicit cast. You can often automatically treat a byte or a &lt;br /&gt;character as an int, for&lt;br&gt;&lt;br /&gt;example, or an int as a long, an int as a float, or anything as a double &lt;br /&gt;automatically. In most&lt;br&gt;&lt;br /&gt;cases, because the larger type provides more precision than the smaller, no loss &lt;br /&gt;of information occurs&lt;br&gt;&lt;br /&gt;when the value is cast. The exception is casting integers to floating-point &lt;br /&gt;values; casting an int or a&lt;br&gt;&lt;br /&gt;long to a float or a long to a double may cause some loss of precision.&lt;br&gt;&lt;br /&gt;To convert a large value to smaller type, you must use an explicit cast, because &lt;br /&gt;converting that value may&lt;br&gt;&lt;br /&gt;result in a loss of precision. Explicit casts look like this:&lt;br&gt;&lt;br /&gt;(typename)value&lt;br&gt;&lt;br /&gt;In this form, typename is the name of the type you're converting to (for &lt;br /&gt;example: short, int, float,&lt;br&gt;&lt;br /&gt;boolean), and value is an expression that results in the value you want to &lt;br /&gt;convert. So, for example, in&lt;br&gt;&lt;br /&gt;this expression the value of x is divided by the value of y and the result is &lt;br /&gt;cast to an int:&lt;br&gt;&lt;br /&gt;(int) (x / y);&lt;br&gt;&lt;br /&gt;Note that because the precedence of casting is higher than that of arithmetic, &lt;br /&gt;you have to use parentheses&lt;br&gt;&lt;br /&gt;here; otherwise, the value of x would be cast first and then divided by y (which &lt;br /&gt;might very well be a very&lt;br&gt;&lt;br /&gt;different result).&lt;br&gt;&lt;br /&gt;Casting Objects&lt;br&gt;&lt;br /&gt;Instances of classes can also be cast to instances of other classes, with one &lt;br /&gt;restriction: The class of the&lt;br&gt;&lt;br /&gt;object you're casting and the class you're casting it to must be related by &lt;br /&gt;inheritance; that is, you can cast&lt;br&gt;&lt;br /&gt;an object only to an instance of its class's sub- or superclass-not to any &lt;br /&gt;random class.&lt;br&gt;&lt;br /&gt;Analogous to converting a primitive value to a larger type, some objects may not &lt;br /&gt;need to be cast&lt;br&gt;&lt;br /&gt;explicitly. In particular, because subclasses contain all the same information &lt;br /&gt;as their superclass, you can&lt;br&gt;&lt;br /&gt;use an instance of a subclass anywhere a superclass is expected. (Did you just &lt;br /&gt;have to read that sentence&lt;br&gt;&lt;br /&gt;four times before you understood it? I had to rewrite it a whole lot of times &lt;br /&gt;before it became even that&lt;br&gt;&lt;br /&gt;simple. Bear with me, its not that bad. Let's try an example.) Suppose you have &lt;br /&gt;a method that takes two&lt;br&gt;&lt;br /&gt;arguments: one of type Object, and one of type Number. You don't have to pass &lt;br /&gt;instances of those&lt;br&gt;&lt;br /&gt;particular classes to that method. For the Object argument, you can pass any &lt;br /&gt;subclass of Object (any&lt;br&gt;&lt;br /&gt;object, in other words), and for the Number argument you can pass in any &lt;br /&gt;instance of any subclass of&lt;br&gt;&lt;br /&gt;Number (Integer, Boolean, Float, and so on); you don't have to explicitly &lt;br /&gt;convert them first.&lt;br&gt;&lt;br /&gt;Casting downward in the class hierarchy is automatic, but casting upward is not. &lt;br /&gt;Converting an instance of&lt;br&gt;&lt;br /&gt;a subclass to an instance of a superclass loses the information the original &lt;br /&gt;subclass provided and requires&lt;br&gt;&lt;br /&gt;an explicit cast. To cast an object to another class, you use the same casting &lt;br /&gt;operation that you used for&lt;br&gt;&lt;br /&gt;base types:&lt;br&gt;&lt;br /&gt;(classname)object&lt;br&gt;&lt;br /&gt;In this case, classname is the name of the class you want to cast the object to, &lt;br /&gt;and object is a&lt;br&gt;&lt;br /&gt;reference to the object you're casting. Note that casting creates a reference to &lt;br /&gt;the old object of the type&lt;br&gt;&lt;br /&gt;classname; the old object still continues to exist as it did before.&lt;br&gt;&lt;br /&gt;Here's a (fictitious) example of a cast of an instance of the class GreenApple &lt;br /&gt;to an instance of the class&lt;br&gt;&lt;br /&gt;Apple (where GreenApple is theoretically a subclass of Apple with more &lt;br /&gt;information to define the&lt;br&gt;&lt;br /&gt;apple as green):&lt;br&gt;&lt;br /&gt;GreenApple a;&lt;br&gt;&lt;br /&gt;Apple a2;&lt;br&gt;&lt;br /&gt;a = new GreenApple();&lt;br&gt;&lt;br /&gt;a2 = (Apple) a;&lt;br&gt;&lt;br /&gt;In addition to casting objects to classes, you can also cast objects to &lt;br /&gt;interfaces-but only if that object's&lt;br&gt;&lt;br /&gt;class or one of its superclasses actually implements that interface. Casting an &lt;br /&gt;object to an interface means&lt;br&gt;&lt;br /&gt;that you can call one of that interface's methods even if that object's class &lt;br /&gt;does not actually implement that&lt;br&gt;&lt;br /&gt;interface. You'll learn more about interfaces in Week 3.&lt;br&gt;&lt;br /&gt;Converting Primitive Types to Objects and Vice Versa&lt;br&gt;&lt;br /&gt;Now you know how to cast a primitive type to another primitive type and how to &lt;br /&gt;cast between classes.&lt;br&gt;&lt;br /&gt;How can you cast one to the other?&lt;br&gt;&lt;br /&gt;You can't! Primitive types and objects are very different things in Java and you &lt;br /&gt;can't automatically cast or&lt;br&gt;&lt;br /&gt;convert between the two. However, the java.lang package includes several special &lt;br /&gt;classes that&lt;br&gt;&lt;br /&gt;correspond to each primitive data type: Integer for ints, Float for floats, &lt;br /&gt;Boolean for&lt;br&gt;&lt;br /&gt;booleans, and so on. Note that the class names have an initial capital letter, &lt;br /&gt;and the primitive types are&lt;br&gt;&lt;br /&gt;lowercase. Java treats these names very differently, so don't confuse them, or &lt;br /&gt;your methods and variables&lt;br&gt;&lt;br /&gt;won't behave the way you expect.&lt;br&gt;&lt;br /&gt;Using class methods defined in these classes, you can create an &lt;br /&gt;object-equivalent for all the primitive&lt;br&gt;&lt;br /&gt;types using new. The following line of code creates an instance of the Integer &lt;br /&gt;class with the value 35:&lt;br&gt;&lt;br /&gt;Integer intObject = new Integer(35);&lt;br&gt;&lt;br /&gt;Once you have actual objects, you can treat those values as objects. Then, when &lt;br /&gt;you want the primitive&lt;br&gt;&lt;br /&gt;values back again, there are methods for that as well-for example, the intValue() &lt;br /&gt;method extracts an&lt;br&gt;&lt;br /&gt;int primitive value from an Integer object:&lt;br&gt;&lt;br /&gt;int theInt = intObject.intValue(); // returns 35&lt;br&gt;&lt;br /&gt;See the Java API documentation for these special classes for specifics on the &lt;br /&gt;methods for converting&lt;br&gt;&lt;br /&gt;primitives to and from objects.&lt;br&gt;&lt;br /&gt;Note&lt;br&gt;&lt;br /&gt;In Java 1.0 there are special type classes for Boolean,&lt;br&gt;&lt;br /&gt;Character, Double, Float, Integer, and Long. Java 1.1&lt;br&gt;&lt;br /&gt;adds classes for Byte and Short, as well as a special wrapper class&lt;br&gt;&lt;br /&gt;for Void. The latter classes are used primarily for object reflection.&lt;br&gt;&lt;br /&gt;Odds and Ends&lt;br&gt;&lt;br /&gt;This section is a catchall for other information about working with objects, &lt;br /&gt;particularly the following:&lt;br&gt;&lt;br /&gt;l Comparing objects&lt;br&gt;&lt;br /&gt;l Finding out the class of any given object&lt;br&gt;&lt;br /&gt;l Testing to see whether an object is an instance of a given class&lt;br&gt;&lt;br /&gt;Comparing Objects&lt;br&gt;&lt;br /&gt;Yesterday you learned about operators for comparing values: equals, not equals, &lt;br /&gt;less than, and so on. Most&lt;br&gt;&lt;br /&gt;of these operators work only on primitive types, not on objects. If you try to &lt;br /&gt;use other values as operands,&lt;br&gt;&lt;br /&gt;the Java compiler produces errors.&lt;br&gt;&lt;br /&gt;The exception to this rule is with the operators for equality: == (equal) and != &lt;br /&gt;(not equal). These&lt;br&gt;&lt;br /&gt;operators, when used with objects, test whether the two operands refer to &lt;br /&gt;exactly the same object in&lt;br&gt;&lt;br /&gt;memory.&lt;br&gt;&lt;br /&gt;What should you do if you want to be able to compare instances of your class and &lt;br /&gt;have meaningful&lt;br&gt;&lt;br /&gt;results? You have to implement special methods in your class, and you have to &lt;br /&gt;call those methods using&lt;br&gt;&lt;br /&gt;those method names.&lt;br&gt;&lt;br /&gt;Technical Note&lt;br&gt;&lt;br /&gt;Java does not have the concept of operator overloading-that is, the&lt;br&gt;&lt;br /&gt;ability to redefine the behavior of the built-in operators using methods&lt;br&gt;&lt;br /&gt;in your own classes. The built-in operators remain defined only for&lt;br&gt;&lt;br /&gt;numbers.&lt;br&gt;&lt;br /&gt;A good example of this is the String class. It is possible to have two strings, &lt;br /&gt;two independent objects in&lt;br&gt;&lt;br /&gt;memory with the same values-that is, the same characters in the same order. &lt;br /&gt;According to the == operator,&lt;br&gt;&lt;br /&gt;however, those two String objects will not be equal, because, although their &lt;br /&gt;contents are the same, they&lt;br&gt;&lt;br /&gt;are not the same object.&lt;br&gt;&lt;br /&gt;The String class, therefore, defines a method called equals() that tests each &lt;br /&gt;character in the string&lt;br&gt;&lt;br /&gt;and returns true if the two strings have the same values. Listing 4.5 &lt;br /&gt;illustrates this.&lt;br&gt;&lt;br /&gt;Listing 4.5. A test of string equality.&lt;br&gt;&lt;br /&gt;1: class EqualsTest {&lt;br&gt;&lt;br /&gt;2: public static void main(String args[]) {&lt;br&gt;&lt;br /&gt;3: String str1, str2;&lt;br&gt;&lt;br /&gt;4: str1 = &amp;quot;she sells sea shells by the sea shore.&amp;quot;;&lt;br&gt;&lt;br /&gt;5: str2 = str1;&lt;br&gt;&lt;br /&gt;6:&lt;br&gt;&lt;br /&gt;7: System.out.println(&amp;quot;String1: &amp;quot; + str1);&lt;br&gt;&lt;br /&gt;8: System.out.println(&amp;quot;String2: &amp;quot; + str2);&lt;br&gt;&lt;br /&gt;9: System.out.println(&amp;quot;Same object? &amp;quot; + (str1 == str2));&lt;br&gt;&lt;br /&gt;10:&lt;br&gt;&lt;br /&gt;11: str2 = new String(str1);&lt;br&gt;&lt;br /&gt;12:&lt;br&gt;&lt;br /&gt;13: System.out.println(&amp;quot;String1: &amp;quot; + str1);&lt;br&gt;&lt;br /&gt;14: System.out.println(&amp;quot;String2: &amp;quot; + str2);&lt;br&gt;&lt;br /&gt;15: System.out.println(&amp;quot;Same object? &amp;quot; + (str1 == str2));&lt;br&gt;&lt;br /&gt;16: System.out.println(&amp;quot;Same value? &amp;quot; + str1.equals(str2));&lt;br&gt;&lt;br /&gt;17: }&lt;br&gt;&lt;br /&gt;18: }&lt;br&gt;&lt;br /&gt;String1: she sells sea shells by the sea shore.&lt;br&gt;&lt;br /&gt;String2: she sells sea shells by the sea shore.&lt;br&gt;&lt;br /&gt;Same object? true&lt;br&gt;&lt;br /&gt;String1: she sells sea shells by the sea shore.&lt;br&gt;&lt;br /&gt;String2: she sells sea shells by the sea shore.&lt;br&gt;&lt;br /&gt;Same object? false&lt;br&gt;&lt;br /&gt;Same value? true&lt;br&gt;&lt;br /&gt;Analysis&lt;br&gt;&lt;br /&gt;The first part of this program (lines 4 through 6) declares two&lt;br&gt;&lt;br /&gt;variables (str1 and str2) assigns the literal she sells sea&lt;br&gt;&lt;br /&gt;shells by the sea shore. to str1, and then assigns that&lt;br&gt;&lt;br /&gt;value to str2. As you learned earlier when we talked about object&lt;br&gt;&lt;br /&gt;references, now str1 and str2 point to the same object, and the&lt;br&gt;&lt;br /&gt;equality test at line 10 proves that.&lt;br&gt;&lt;br /&gt;In the second part, you create a new string object with the same value as str1 &lt;br /&gt;and assign str2 to that&lt;br&gt;&lt;br /&gt;new string object. Now you have two different string objects in str1 and str2, &lt;br /&gt;both with the same&lt;br&gt;&lt;br /&gt;value. Testing them to see whether they're the same object by using the == &lt;br /&gt;operator (line 16) returns the&lt;br&gt;&lt;br /&gt;expected answer (false-they are not the same object in memory), as does testing &lt;br /&gt;them using the&lt;br&gt;&lt;br /&gt;equals() method (line 17) (true-they have the same values).&lt;br&gt;&lt;br /&gt;Technical Note&lt;br&gt;&lt;br /&gt;Why can't you just use another literal when you change str2, rather&lt;br&gt;&lt;br /&gt;than using new? String literals are optimized in Java-if you create a&lt;br&gt;&lt;br /&gt;string using a literal, and then use another literal with the same&lt;br&gt;&lt;br /&gt;characters, Java knows enough to give you the first String object&lt;br&gt;&lt;br /&gt;back. Both strings are the same objects-to create two separate objects&lt;br&gt;&lt;br /&gt;you have to go out of your way.&lt;br&gt;&lt;br /&gt;Determining the Class of an Object&lt;br&gt;&lt;br /&gt;Want to find out the class of an object? Here's the way to do it for an object &lt;br /&gt;assigned to the variable obj:&lt;br&gt;&lt;br /&gt;String name = obj.getClass().getName();&lt;br&gt;&lt;br /&gt;What does this do? The getClass() method is defined in the Object class, and as &lt;br /&gt;such is available&lt;br&gt;&lt;br /&gt;for all objects. The result of that method is a Class object (where Class is &lt;br /&gt;itself a class), which has a&lt;br&gt;&lt;br /&gt;method called getName(). getName() returns a string representing the name of the &lt;br /&gt;class.&lt;br&gt;&lt;br /&gt;Another test that might be useful to you is the instanceof operator. instanceof &lt;br /&gt;has two operands:&lt;br&gt;&lt;br /&gt;an object on the left and the name of a class on the right. The expression &lt;br /&gt;returns true or false based on&lt;br&gt;&lt;br /&gt;whether the object is an instance of the named class or any of that class's &lt;br /&gt;subclasses:&lt;br&gt;&lt;br /&gt;&amp;quot;foo&amp;quot; instanceof String // true&lt;br&gt;&lt;br /&gt;Point pt = new Point(10, 10);&lt;br&gt;&lt;br /&gt;pt instanceof String // false&lt;br&gt;&lt;br /&gt;The instanceof operator can also be used for interfaces; if an object implements &lt;br /&gt;an interface, the&lt;br&gt;&lt;br /&gt;instanceof operator with that interface name on the right side returns true. &lt;br /&gt;You'll learn all about&lt;br&gt;&lt;br /&gt;interfaces in Week 3.&lt;br&gt;&lt;br /&gt;Class and Object Reflection (Java 1.1)&lt;br&gt;&lt;br /&gt;Reflection, also known as introspection, is a somewhat lofty term to describe &lt;br /&gt;the ability to &amp;quot;look inside&amp;quot; a&lt;br&gt;&lt;br /&gt;class or an object and get information about that object's variables and methods &lt;br /&gt;as well as actually set and&lt;br&gt;&lt;br /&gt;get the values of those variables and to call methods. Object reflection is &lt;br /&gt;useful for tools such as class&lt;br&gt;&lt;br /&gt;browsers or debuggers, where getting at the information of an object on-the-fly &lt;br /&gt;allows you to explore what&lt;br&gt;&lt;br /&gt;that object can do, or for component-based programs such as Java Beans, where &lt;br /&gt;the ability for one object&lt;br&gt;&lt;br /&gt;to query another object about what it can do (and then ask it to do something) &lt;br /&gt;is useful to building larger&lt;br&gt;&lt;br /&gt;applications.&lt;br&gt;&lt;br /&gt;The classes that support reflection of Java classes and objects will be part of &lt;br /&gt;the core Java 1.1 API (they&lt;br&gt;&lt;br /&gt;are not available in the 1.0.2 version of the JDK). A new package, &lt;br /&gt;java.lang.reflect, will contain&lt;br&gt;&lt;br /&gt;new classes to support reflection, which include the following:&lt;br&gt;&lt;br /&gt;l Field, for managing and finding out information about class and instance &lt;br /&gt;variables&lt;br&gt;&lt;br /&gt;l Method, for managing class and instance methods&lt;br&gt;&lt;br /&gt;Constructor, for managing the special methods for creating new instances of &lt;br /&gt;classes (you'll&lt;br&gt;&lt;br /&gt;learn more about constructors on Day 7)&lt;br&gt;&lt;br /&gt;l &lt;br&gt;&lt;br /&gt;l Array, for managing arrays&lt;br&gt;&lt;br /&gt;Modifier, for decoding modifier information about classes, variables and methods &lt;br /&gt;(more about&lt;br&gt;&lt;br /&gt;modifiers on Day 15, &amp;quot;Modifiers, Access Control, and Class Design&amp;quot;)&lt;br&gt;&lt;br /&gt;l &lt;br&gt;&lt;br /&gt;In addition, there will be a number of new methods available in the Class class &lt;br /&gt;to help tie together the&lt;br&gt;&lt;br /&gt;various reflection classes.&lt;br&gt;&lt;br /&gt;You can find out more about the new reflection classes and methods from&lt;br&gt;&lt;br /&gt;http://java.sun.com/products/JDK/1.1/designspecs/reflection/.&lt;br&gt;&lt;br /&gt;&lt;a href="http://www.sbinfosystem.com/"&gt;The Java Class Library&lt;/a&gt;&lt;br&gt;&lt;br /&gt;To finish up today, let's look at the Java class library. Actually, you've had &lt;br /&gt;some experience with some of&lt;br&gt;&lt;br /&gt;the Java classes already, so they shouldn't seem that strange.&lt;br&gt;&lt;br /&gt;The Java class library provides the set of classes that are guaranteed to be &lt;br /&gt;available in any commercial&lt;br&gt;&lt;br /&gt;Java environment (for example, in any Java development environment or in &lt;br /&gt;browsers such as Netscape).&lt;br&gt;&lt;br /&gt;Those classes are in the java package and include all the classes you've seen so &lt;br /&gt;far in this book, plus a&lt;br&gt;&lt;br /&gt;whole lot more classes you'll learn about later on in this book (and more you &lt;br /&gt;may not learn about at all).&lt;br&gt;&lt;br /&gt;The Java Developer's Kit comes with documentation for all of the Java class &lt;br /&gt;library, which includes&lt;br&gt;&lt;br /&gt;descriptions of each class's instance variables, methods, constructors, &lt;br /&gt;interfaces, and so on. You can get to&lt;br&gt;&lt;br /&gt;this documentation (called the Java Application Programmer's Interface, or API) &lt;br /&gt;via the Web at&lt;br&gt;&lt;br /&gt;http://java.sun.com:80/products/JDK/CurrentRelease/api/packages.html. A&lt;br&gt;&lt;br /&gt;shorter summary of the Java API is in appendix C as well. Exploring the Java &lt;br /&gt;class library and its methods&lt;br&gt;&lt;br /&gt;and instance variables is a great way to figure out what Java can and cannot do, &lt;br /&gt;as well as how it can&lt;br&gt;&lt;br /&gt;become a starting point for your own development.&lt;br&gt;&lt;br /&gt;Here are the class packages that are part of the Java class library:&lt;br&gt;&lt;br /&gt;java.lang-Classes that apply to the language itself, including the Object class, &lt;br /&gt;the String&lt;br&gt;&lt;br /&gt;class, and the System class. It also contains the special classes for the &lt;br /&gt;primitive types (Integer,&lt;br&gt;&lt;br /&gt;Character, Float, and so on). You'll get at least a glance at most of the &lt;br /&gt;classes in this package&lt;br&gt;&lt;br /&gt;in this first week.&lt;br&gt;&lt;br /&gt;l &lt;br&gt;&lt;br /&gt;java.util-Utility classes, such as Date, as well as simple collection classes, &lt;br /&gt;such as Vector&lt;br&gt;&lt;br /&gt;and Hashtable. You'll learn more about these classes in the Bonus Week.&lt;br&gt;&lt;br /&gt;l &lt;br&gt;&lt;br /&gt;java.io-Input and output classes for writing to and reading from streams (such &lt;br /&gt;as standard input&lt;br&gt;&lt;br /&gt;and output) and for handling files. Day 19, &amp;quot;Streams and I/O,&amp;quot; describes the &lt;br /&gt;classes in this package.&lt;br&gt;&lt;br /&gt;l &lt;br&gt;&lt;br /&gt;java.net-Classes for networking support, including Socket and URL (a class to &lt;br /&gt;represent&lt;br&gt;&lt;br /&gt;references to documents on the World Wide Web). You'll learn a little about &lt;br /&gt;networking on Day 14,&lt;br&gt;&lt;br /&gt;&amp;quot;Windows, Networking, and Other Tidbits,&amp;quot; and then on Day 26, &amp;quot;Client/Server &lt;br /&gt;Networking in&lt;br&gt;&lt;br /&gt;Java.&amp;quot;&lt;br&gt;&lt;br /&gt;l &lt;br&gt;&lt;br /&gt;java.awt-This is the Abstract Windowing Toolkit. It contains classes to &lt;br /&gt;implement graphical&lt;br&gt;&lt;br /&gt;user interface features, including classes for Window, Menu, Button, Font, &lt;br /&gt;CheckBox, and so&lt;br&gt;&lt;br /&gt;on. It also includes mechanisms for managing system events and for processing &lt;br /&gt;images (in the&lt;br&gt;&lt;br /&gt;java.awt.Image package). You'll learn all about the awt in Week 2.&lt;br&gt;&lt;br /&gt;l &lt;br&gt;&lt;br /&gt;l java.applet-Classes to implement Java applets.&lt;br&gt;&lt;br /&gt;In addition to the Java classes, your development environment may also include &lt;br /&gt;additional classes that&lt;br&gt;&lt;br /&gt;provide other utilities or functionality. Although these classes may be useful, &lt;br /&gt;because they are not part of&lt;br&gt;&lt;br /&gt;the standard Java library, they may not be available to other people trying to &lt;br /&gt;run your Java program unless&lt;br&gt;&lt;br /&gt;you explicitly include those classes with your program. This is particularly &lt;br /&gt;important for applets, because&lt;br&gt;&lt;br /&gt;applets are expected to be able to run on any platform, using any Java-enabled &lt;br /&gt;browser. Only classes&lt;br&gt;&lt;br /&gt;inside the java package are guaranteed to be available on all browsers and Java &lt;br /&gt;environments.&lt;br&gt;&lt;br /&gt;Summary&lt;br&gt;&lt;br /&gt;Objects, objects everywhere. Today, you've learned all about how to deal with &lt;br /&gt;objects: how to create&lt;br&gt;&lt;br /&gt;them, how to find out and change the values of their variables, and how to call &lt;br /&gt;their methods. You have&lt;br&gt;&lt;br /&gt;also learned how to copy and compare them and how to convert them into other &lt;br /&gt;objects. Finally, you have&lt;br&gt;&lt;br /&gt;learned a bit about the Java class libraries-which give you a whole slew of &lt;br /&gt;classes to play with in your&lt;br&gt;&lt;br /&gt;own programs.&lt;br&gt;&lt;br /&gt;You now have the fundamentals of how to deal with most simple things in the Java &lt;br /&gt;language. All you&lt;br&gt;&lt;br /&gt;have left are arrays, conditionals, and loops, which you'll learn about &lt;br /&gt;tomorrow. Then you'll learn how to&lt;br&gt;&lt;br /&gt;define and use classes in Java applications on Day 6, and launch directly into &lt;br /&gt;applets next week. With just&lt;br&gt;&lt;br /&gt;about everything you do in your Java programs, you'll always come back to &lt;br /&gt;objects.&lt;/p&gt;&lt;br /&gt;&lt;p&gt;&lt;br&gt;&lt;br /&gt;Q&amp;amp;A&lt;br&gt;&lt;br /&gt;Q: I'm confused about the differences between objects and the primitive data &lt;br /&gt;types, such as&lt;br&gt;&lt;br /&gt;int and boolean.&lt;br&gt;&lt;br /&gt;A: The primitive types in the language (byte, short, int, long, float, double, &lt;br /&gt;boolean,&lt;br&gt;&lt;br /&gt;and char) represent the smallest things in the language. They are not objects, &lt;br /&gt;although in many&lt;br&gt;&lt;br /&gt;ways they can be handled like objects-they can be assigned to variables and &lt;br /&gt;passed in and out of&lt;br&gt;&lt;br /&gt;methods. Most of the operations that work exclusively on objects, however, will &lt;br /&gt;not work with&lt;br&gt;&lt;br /&gt;primitive types.&lt;br&gt;&lt;br /&gt;Objects are instances of classes and, as such, are usually much more complex &lt;br /&gt;data types than&lt;br&gt;&lt;br /&gt;simple numbers and characters, often containing numbers and characters as &lt;br /&gt;instance or class&lt;br&gt;&lt;br /&gt;variables.&lt;br&gt;&lt;br /&gt;Q: No pointers in Java? If you don't have pointers, how are you supposed to do &lt;br /&gt;something like&lt;br&gt;&lt;br /&gt;linked lists, where you have a pointer from one nose to another so you can &lt;br /&gt;traverse them?&lt;br&gt;&lt;br /&gt;A: Java doesn't have no pointers at all; it has no explicit pointers. Object &lt;br /&gt;references are, effectively,&lt;br&gt;&lt;br /&gt;pointers. So to create something like a linked list, you would create a class &lt;br /&gt;called Node, which&lt;br&gt;&lt;br /&gt;would have an instance variable also of type Node. Then to link together node &lt;br /&gt;objects all you&lt;br&gt;&lt;br /&gt;need to do is assign a node object to the instance variable of the object just &lt;br /&gt;before it in the list.&lt;br&gt;&lt;br /&gt;Because object references are pointers, linked lists set up this way will behave &lt;br /&gt;as you would&lt;br&gt;&lt;br /&gt;expect them to.&lt;br&gt;&lt;br /&gt;Q: In the section on calling methods, you had examples of calling a method with &lt;br /&gt;a different&lt;br&gt;&lt;br /&gt;number of arguments each time-and it gave a different kind of result. How is &lt;br /&gt;that possible?&lt;br&gt;&lt;br /&gt;A: That's called method overloading. Overloading means that the same method can &lt;br /&gt;have different&lt;br&gt;&lt;br /&gt;behavior based on the arguments it's called with-and the number and type of &lt;br /&gt;arguments can vary.&lt;br&gt;&lt;br /&gt;When you define methods in your own classes, you define separate method &lt;br /&gt;signatures with&lt;br&gt;&lt;br /&gt;different sets of arguments and different definitions. When a method is called, &lt;br /&gt;Java figures out&lt;br&gt;&lt;br /&gt;which definition to execute based on the number and type of arguments with which &lt;br /&gt;you called it.&lt;br&gt;&lt;br /&gt;You'll learn all about this on Day 6.&lt;br&gt;&lt;br /&gt;Q: No operator overloading in Java? Why not? I thought Java was based on C++, &lt;br /&gt;and C++ has&lt;br&gt;&lt;br /&gt;operator overloading.&lt;br&gt;&lt;br /&gt;A: Java was indeed based on C++, but it was also designed to be simple, so many &lt;br /&gt;of C++'s features&lt;br&gt;&lt;br /&gt;have been removed. The argument against operator overloading is that because the &lt;br /&gt;operator can&lt;br&gt;&lt;br /&gt;be defined to mean anything; it makes it very difficult to figure out what any &lt;br /&gt;given operator is&lt;br&gt;&lt;br /&gt;doing at any one time. This can result in entirely unreadable code. When you use &lt;br /&gt;a method, you&lt;br&gt;&lt;br /&gt;know it can mean many things to many classes, but when you use an operator you &lt;br /&gt;would like to&lt;br&gt;&lt;br /&gt;know that it always means the same thing. Given the potential for abuse, the &lt;br /&gt;designers of Java felt&lt;br&gt;&lt;br /&gt;it was one of the &lt;a href="http://www.sbinfosystem.com/"&gt;C++&lt;/a&gt; features that was best left out.&lt;/p&gt;</description><thr:total xmlns:thr="http://purl.org/syndication/thread/1.0">0</thr:total></item><item><title>Java Help, Java Tutorials, Java Programming, Java Tricks</title><link>http://swebsolution.blogspot.com/2008/01/java-help-java-tutorials-java_08.html</link><author>noreply@blogger.com (Unknown)</author><pubDate>Tue, 8 Jan 2008 00:48:00 -0800</pubDate><guid isPermaLink="false">tag:blogger.com,1999:blog-7227425366482465228.post-1733347582880761025</guid><description>&lt;p&gt;&lt;br /&gt;&lt;a style="text-decoration: none; color: #333333; font-weight: bold" href="http://www.sbinfosystem.com/"&gt;&lt;br /&gt;&lt;a href="http://www.sbinfosystem.com/"&gt;Java Help, Java Tutorials, Java Programming, Java Tricks&lt;/a&gt;&lt;/a&gt; &lt;br&gt;&lt;br&gt;Lesson Three&lt;br&gt;&lt;br /&gt;Java Basics&lt;/p&gt;&lt;br /&gt;&lt;p&gt;&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;&lt;a href="http://www.sbinfosystem.com/"&gt;CONTENTS&lt;/a&gt;&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;Statements and Expressions&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;&lt;a href="http://www.sbinfosystem.com/"&gt;Variables and Data Types&lt;/a&gt;&lt;br&gt;&lt;br /&gt;Declaring Variables&lt;br&gt;&lt;br /&gt;Notes on Variable Names&lt;br&gt;&lt;br /&gt;Variable Types&lt;br&gt;&lt;br /&gt;Assigning Values to Variables&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;Comments&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;Literals&lt;br&gt;&lt;br /&gt;Number Literals&lt;br&gt;&lt;br /&gt;Boolean Literals&lt;br&gt;&lt;br /&gt;Character Literals&lt;br&gt;&lt;br /&gt;String Literals&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;Expressions and Operators&lt;br&gt;&lt;br /&gt;Arithmetic&lt;br&gt;&lt;br /&gt;More About Assignment&lt;br&gt;&lt;br /&gt;Incrementing and Decrementing&lt;br&gt;&lt;br /&gt;Comparisons&lt;br&gt;&lt;br /&gt;Logical Operators&lt;br&gt;&lt;br /&gt;Bitwise Operators&lt;br&gt;&lt;br /&gt;Operator Precedence&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;String Arithmetic&lt;br&gt;&lt;br /&gt;Summary&lt;br&gt;&lt;br /&gt;Q&amp;amp;A&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;Already this week you've learned about Java programming in very broad terms-what &lt;br /&gt;a Java program andan executable look like, and how to create simple classes. For &lt;br /&gt;the remainder of this week, you're going to get down to details and deal with &lt;br /&gt;the specifics of what the Java language looks like.&lt;br&gt;&lt;br /&gt;Today you won't define any classes or objects or worry about how any of them &lt;br /&gt;communicate inside a&lt;br&gt;&lt;br /&gt;Java program. Rather, you'll draw closer and examine simple Java statements-the &lt;br /&gt;basic things you can do&lt;br&gt;&lt;br /&gt;in Java within a method definition such as main().&lt;br&gt;&lt;br /&gt;Today you'll learn about the following:&lt;br&gt;&lt;br /&gt;Java statements and expressions&lt;br&gt;&lt;br /&gt;Variables and data types&lt;br&gt;&lt;br /&gt;&lt;a href="http://www.sbinfosystem.com/"&gt;Comments&lt;/a&gt;&lt;br&gt;&lt;br /&gt;Literals&lt;br&gt;&lt;br /&gt;Arithmetic&lt;br&gt;&lt;br /&gt;Comparisons&lt;br&gt;&lt;br /&gt;Logical operators&lt;br&gt;&lt;br /&gt;Technical Note&lt;br&gt;&lt;br /&gt;Java looks a lot like C++, and-by extension-like C. Much of the&lt;br&gt;&lt;br /&gt;syntax will be very familiar to you if you are used to working in these&lt;br&gt;&lt;br /&gt;languages. If you are an experienced C or C++ programmer, you may&lt;br&gt;&lt;br /&gt;want to pay special attention to the technical notes (such as this one),&lt;br&gt;&lt;br /&gt;because they provide information about the specific differences&lt;br&gt;&lt;br /&gt;between these and other traditional languages and Java.&lt;br&gt;&lt;br /&gt;Statements and Expressions&lt;br&gt;&lt;br /&gt;A statement indicates the simplest tasks you can accomplish in Java; a statement &lt;br /&gt;forms a single Java&lt;br&gt;&lt;br /&gt;operation. All the following are simple Java statements:&lt;br&gt;&lt;br /&gt;int i = 1;&lt;br&gt;&lt;br /&gt;import java.awt.Font;&lt;br&gt;&lt;br /&gt;System.out.println(&amp;quot;This motorcycle is a &amp;quot;&lt;br&gt;&lt;br /&gt;+ color + &amp;quot; &amp;quot; + make);&lt;br&gt;&lt;br /&gt;m.engineState = true;&lt;br&gt;&lt;br /&gt;Statements sometimes return values-for example, when you add two numbers &lt;br /&gt;together or test to see&lt;br&gt;&lt;br /&gt;whether one value is equal to another. These kind of statements are called &lt;br /&gt;expressions. You'll learn about&lt;br&gt;&lt;br /&gt;these later today.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;White space in Java statements, as with C, is unimportant. A statement can be &lt;br /&gt;contained on a single line&lt;br&gt;&lt;br /&gt;or on multiple lines, and the Java compiler will be able to read it just fine. &lt;br /&gt;The most important thing to&lt;br&gt;&lt;br /&gt;remember about Java statements is that each one ends with a semicolon (;). &lt;br /&gt;Forget the semicolon, and&lt;br&gt;&lt;br /&gt;your Java program won't compile.&lt;br&gt;&lt;br /&gt;Java also has compound statements, or blocks, which can be placed wherever a &lt;br /&gt;single statement can.&lt;br&gt;&lt;br /&gt;Block statements are surrounded by braces ({}). You'll learn more about blocks &lt;br /&gt;on Day 5, &amp;quot;Arrays,&lt;br&gt;&lt;br /&gt;Conditionals, and Loops.&amp;quot;&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;Variables and Data Types&lt;br&gt;&lt;br /&gt;Variables are locations in memory in which values can be stored. Each one has a &lt;br /&gt;name, a type, and a&lt;br&gt;&lt;br /&gt;value. Before you can use a variable, you have to declare it. After it is &lt;br /&gt;declared, you can then assign&lt;br&gt;&lt;br /&gt;values to it (you can also declare and assign a value to a variable at the same &lt;br /&gt;time, as you'll learn in this&lt;br&gt;&lt;br /&gt;section).&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;Java actually has three kinds of variables: instance variables, class variables, &lt;br /&gt;and local variables.&lt;br&gt;&lt;br /&gt;Instance variables, as you learned yesterday, are used to define the attributes &lt;br /&gt;of a particular object. Class&lt;br&gt;&lt;br /&gt;variables are similar to instance variables, except their values apply to all &lt;br /&gt;that class's instances (and to the&lt;br&gt;&lt;br /&gt;class itself) rather than having different values for each object.&lt;br&gt;&lt;br /&gt;Local variables are declared and used inside method definitions, for example, &lt;br /&gt;for index counters in loops,&lt;br&gt;&lt;br /&gt;as temporary variables, or to hold values that you need only inside the method &lt;br /&gt;definition itself. They can&lt;br&gt;&lt;br /&gt;also be used inside blocks, which you'll learn about on Day 5. Once the method &lt;br /&gt;(or block) finishes&lt;br&gt;&lt;br /&gt;executing, the variable definition and its value cease to exist. Use local &lt;br /&gt;variables to store information&lt;br&gt;&lt;br /&gt;needed by a single method and instance variables to store information needed by &lt;br /&gt;multiple methods in the&lt;br&gt;&lt;br /&gt;object.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;Although all three kinds of variables are declared in much the same ways, class &lt;br /&gt;and instance variables&lt;br&gt;&lt;br /&gt;are accessed and assigned in slightly different ways from local variables. Today &lt;br /&gt;you'll focus on variables&lt;br&gt;&lt;br /&gt;as used within method definitions; tomorrow you'll learn how to deal with &lt;br /&gt;instance and class variables.&lt;br&gt;&lt;br /&gt;Note Unlike other languages, Java does not have global variables-that is,&lt;br&gt;&lt;br /&gt;variables that are global to all parts of a program. Instance and class&lt;br&gt;&lt;br /&gt;variables can be used to communicate global information between&lt;br&gt;&lt;br /&gt;and among objects. Remember that Java is an object-oriented&lt;br&gt;&lt;br /&gt;language, so you should think in terms of objects and how they&lt;br&gt;&lt;br /&gt;interact, rather than in terms of programs.&lt;br&gt;&lt;br /&gt;Declaring Variables&lt;br&gt;&lt;br /&gt;To use any variable in a Java program, you must first declare it. Variable &lt;br /&gt;declarations consist of a type&lt;br&gt;&lt;br /&gt;and a variable name:&lt;br&gt;&lt;br /&gt;int myAge;&lt;br&gt;&lt;br /&gt;String myName;&lt;br&gt;&lt;br /&gt;boolean isTired;&lt;br&gt;&lt;br /&gt;Variable definitions can go anywhere in a method definition (that is, anywhere a &lt;br /&gt;regular Java statement&lt;br&gt;&lt;br /&gt;can go), although they are most commonly declared at the beginning of the &lt;br /&gt;definition before they are&lt;br&gt;&lt;br /&gt;used:&lt;br&gt;&lt;br /&gt;public static void main (String args[]) {&lt;br&gt;&lt;br /&gt;int count;&lt;br&gt;&lt;br /&gt;String title;&lt;br&gt;&lt;br /&gt;boolean isAsleep;&lt;br&gt;&lt;br /&gt;...&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;You can string together variable names with the same type on one line:&lt;br&gt;&lt;br /&gt;int x, y, z;&lt;br&gt;&lt;br /&gt;String firstName, LastName;&lt;br&gt;&lt;br /&gt;You can also give each variable an initial value when you declare it:&lt;br&gt;&lt;br /&gt;int myAge, mySize, numShoes = 28;&lt;br&gt;&lt;br /&gt;String myName = &amp;quot;Laura&amp;quot;;&lt;br&gt;&lt;br /&gt;boolean isTired = true;&lt;br&gt;&lt;br /&gt;int a = 4, b = 5, c = 6;&lt;br&gt;&lt;br /&gt;If there are multiple variables on the same line with only one initializer (as &lt;br /&gt;in the first of the previous&lt;br&gt;&lt;br /&gt;examples), the initial value applies to only the last variable in a declaration. &lt;br /&gt;You can also group&lt;br&gt;&lt;br /&gt;individual variables and initializers on the same line using commas, as with the &lt;br /&gt;last example.&lt;br&gt;&lt;br /&gt;Local variables must be given values before they can be used (your Java program &lt;br /&gt;will not compile if you&lt;br&gt;&lt;br /&gt;try to use an unassigned local variable). For this reason, it's a good idea &lt;br /&gt;always to give local variables&lt;br&gt;&lt;br /&gt;initial values. Instance and class variable definitions do not have this &lt;br /&gt;restriction. (Their initial value&lt;br&gt;&lt;br /&gt;depends on the type of the variable: null for instances of classes, 0 for &lt;br /&gt;numeric variables, '\0' for&lt;br&gt;&lt;br /&gt;characters, and false for booleans.)&lt;br&gt;&lt;br /&gt;Notes on Variable Names&lt;br&gt;&lt;br /&gt;Variable names in Java can start with a letter, an underscore (_), or a dollar &lt;br /&gt;sign ($). They cannot start&lt;br&gt;&lt;br /&gt;with a number. After the first character, your variable names can include any &lt;br /&gt;letter or number. Symbols,&lt;br&gt;&lt;br /&gt;such as %, *, @, and so on, are often reserved for operators in Java, so be &lt;br /&gt;careful when using symbols in&lt;br&gt;&lt;br /&gt;variable names.&lt;br&gt;&lt;br /&gt;In addition, the Java language uses the Unicode character set. Unicode is a &lt;br /&gt;character set definition that&lt;br&gt;&lt;br /&gt;not only offers characters in the standard ASCII character set, but also &lt;br /&gt;includes several thousand other&lt;br&gt;&lt;br /&gt;characters for representing most international alphabets. This means that you &lt;br /&gt;can use accented characters&lt;br&gt;&lt;br /&gt;and other glyphs as legal characters in variable names, as long as they have a &lt;br /&gt;Unicode character number&lt;br&gt;&lt;br /&gt;above 00C0.&lt;br&gt;&lt;br /&gt;Warning&lt;br&gt;&lt;br /&gt;The Unicode specification is a two-volume set of lists of thousands of&lt;br&gt;&lt;br /&gt;characters. If you don't understand Unicode, or don't think you have a&lt;br&gt;&lt;br /&gt;use for it, it's safest just to use plain numbers and letters in your&lt;br&gt;&lt;br /&gt;variable names. You'll learn a little more about Unicode later.&lt;br&gt;&lt;br /&gt;Finally, note that the Java language is case sensitive, which means that &lt;br /&gt;uppercase letters are different&lt;br&gt;&lt;br /&gt;from lowercase letters. This means that the variable X is different from the &lt;br /&gt;variable x, and a rose is not&lt;br&gt;&lt;br /&gt;a Rose is not a ROSE. Keep this in mind as you write your own Java programs and &lt;br /&gt;as you read Java&lt;br&gt;&lt;br /&gt;code other people have written.&lt;br&gt;&lt;br /&gt;By convention, Java variables have meaningful names, often made up of several &lt;br /&gt;words combined. The&lt;br&gt;&lt;br /&gt;first word is lowercase, but all following words have an initial uppercase &lt;br /&gt;letter:&lt;br&gt;&lt;br /&gt;Button theButton;&lt;br&gt;&lt;br /&gt;long reallyBigNumber;&lt;br&gt;&lt;br /&gt;boolean currentWeatherStateOfPlanetXShortVersion;&lt;br&gt;&lt;br /&gt;Variable Types&lt;br&gt;&lt;br /&gt;In addition to the variable name, each variable declaration must have a type, &lt;br /&gt;which defines what values&lt;br&gt;&lt;br /&gt;that variable can hold. The variable type can be one of three things:&lt;br&gt;&lt;br /&gt;One of the eight primitive data types&lt;br&gt;&lt;br /&gt;The name of a class or interface&lt;br&gt;&lt;br /&gt;An array&lt;br&gt;&lt;br /&gt;You'll learn about how to declare and use array variables on Day 5; this lesson &lt;br /&gt;focuses on the primitive&lt;br&gt;&lt;br /&gt;and class types.&lt;br&gt;&lt;br /&gt;Primitive Types&lt;br&gt;&lt;br /&gt;The eight primitive data types handle common types for integers, floating-point &lt;br /&gt;numbers, characters, and&lt;br&gt;&lt;br /&gt;boolean values (true or false). They're called primitive because they're built &lt;br /&gt;into the system and are&lt;br&gt;&lt;br /&gt;not actual objects, which makes them more efficient to use. Note that these data &lt;br /&gt;types are&lt;br&gt;&lt;br /&gt;machine-independent, which means that you can rely on their sizes and &lt;br /&gt;characteristics to be consistent&lt;br&gt;&lt;br /&gt;across your Java programs.&lt;br&gt;&lt;br /&gt;There are four Java integer types, each with a different range of values (as &lt;br /&gt;listed in Table 3.1). All are&lt;br&gt;&lt;br /&gt;signed, which means they can hold either positive or negative numbers. Which &lt;br /&gt;type you choose for your&lt;br&gt;&lt;br /&gt;variables depends on the range of values you expect that variable to hold; if a &lt;br /&gt;value becomes too big for&lt;br&gt;&lt;br /&gt;the variable type, it is silently truncated.&lt;br&gt;&lt;br /&gt;Table 3.1. Integer types.&lt;br&gt;&lt;br /&gt;Type Size Range&lt;br&gt;&lt;br /&gt;byte 8 bits -128 to 127&lt;br&gt;&lt;br /&gt;short 16 bits -32,768 to 32,767&lt;br&gt;&lt;br /&gt;int 32 bits -2,147,483,648 to 2,147,483,647&lt;br&gt;&lt;br /&gt;long 64 bits -9,223,372,036,854,775,808 to&lt;br&gt;&lt;br /&gt;9,223,372,036,854,775,807&lt;br&gt;&lt;br /&gt;Floating-point numbers are used for numbers with a decimal part. Java &lt;br /&gt;floating-point numbers are&lt;br&gt;&lt;br /&gt;compliant with IEEE 754 (an international standard for defining floating-point &lt;br /&gt;numbers and arithmetic).&lt;br&gt;&lt;br /&gt;There are two floating-point types: float (32 bits, single precision) and double &lt;br /&gt;(64 bits, double&lt;br&gt;&lt;br /&gt;precision).&lt;br&gt;&lt;br /&gt;The char type is used for individual characters. Because Java uses the Unicode &lt;br /&gt;character set, the char&lt;br&gt;&lt;br /&gt;type has 16 bits of precision, unsigned.&lt;br&gt;&lt;br /&gt;Finally, the boolean type can have one of two values, true or false. Note that &lt;br /&gt;unlike in other C-like&lt;br&gt;&lt;br /&gt;languages, boolean is not a number, nor can it be treated as one. All tests of &lt;br /&gt;boolean variables should&lt;br&gt;&lt;br /&gt;test for true or false.&lt;br&gt;&lt;br /&gt;Note that all the primitive types are in lowercase. Be careful when you use them &lt;br /&gt;in your programs that&lt;br&gt;&lt;br /&gt;you do use the lowercase, because there are also classes with the same names &lt;br /&gt;(and an initial capital&lt;br&gt;&lt;br /&gt;letter) that have different behavior-so, for example, the primitive type boolean &lt;br /&gt;is different from the&lt;br&gt;&lt;br /&gt;Boolean class. You'll learn more about these special classes and what they're &lt;br /&gt;used for on Day 4,&lt;br&gt;&lt;br /&gt;&amp;quot;Working with Objects.&amp;quot;&lt;br&gt;&lt;br /&gt;Class Types&lt;br&gt;&lt;br /&gt;In addition to the eight primitive data types, variables in Java can also be &lt;br /&gt;declared to hold an instance of&lt;br&gt;&lt;br /&gt;a particular class:&lt;br&gt;&lt;br /&gt;String LastName;&lt;br&gt;&lt;br /&gt;Font basicFont;&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;OvalShape myOval;&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;Each of these variables can hold instances of the named class or of any of its &lt;br /&gt;subclasses. The latter is&lt;br&gt;&lt;br /&gt;useful when you want a variable to be able to hold different instances of &lt;br /&gt;related classes. For example,&lt;br&gt;&lt;br /&gt;let's say you had a set of fruit classes-Apple, Pear, Strawberry, and so on- all &lt;br /&gt;of which inherited&lt;br&gt;&lt;br /&gt;from the general class Fruit. By declaring a variable of type Fruit, that &lt;br /&gt;variable can then hold&lt;br&gt;&lt;br /&gt;instances of any of the Fruit classes. Declaring a variable of type Object means &lt;br /&gt;that variable can&lt;br&gt;&lt;br /&gt;hold any object.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;Technical Note&lt;br&gt;&lt;br /&gt;Java does not have a typedef statement (as in C and C++). To&lt;br&gt;&lt;br /&gt;declare new types in Java, you declare a new class; then variables can&lt;br&gt;&lt;br /&gt;be declared to be of that class's type.&lt;br&gt;&lt;br /&gt;Assigning Values to Variables&lt;br&gt;&lt;br /&gt;Once a variable has been declared, you can assign a value to that variable by &lt;br /&gt;using the assignment&lt;br&gt;&lt;br /&gt;operator =, like this:&lt;br&gt;&lt;br /&gt;size = 14;&lt;br&gt;&lt;br /&gt;tooMuchCaffiene = true;&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;Comments&lt;br&gt;&lt;br /&gt;Java has three kinds of comments: two for regular comments in source code and &lt;br /&gt;one for the special&lt;br&gt;&lt;br /&gt;documentation system javadoc.&lt;br&gt;&lt;br /&gt;The symbols /* and */ surround multiline comments, as in C or C++. All text &lt;br /&gt;between the two&lt;br&gt;&lt;br /&gt;delimiters is ignored:&lt;br&gt;&lt;br /&gt;/* I don't know how I wrote this next part; I was working&lt;br&gt;&lt;br /&gt;really late one night and it just sort of appeared. I&lt;br&gt;&lt;br /&gt;suspect the code elves did it for me. It might be wise&lt;br&gt;&lt;br /&gt;not to try and change it.&lt;br&gt;&lt;br /&gt;*/&lt;br&gt;&lt;br /&gt;These comments cannot be nested; that is, you cannot have a comment inside a &lt;br /&gt;comment.&lt;br&gt;&lt;br /&gt;Double-slashes (//) can be used for a single line of comment. All the text up to &lt;br /&gt;the end of the line is&lt;br&gt;&lt;br /&gt;ignored:&lt;br&gt;&lt;br /&gt;int vices = 7; // are there really only 7 vices?&lt;br&gt;&lt;br /&gt;The final type of comment begins with /** and ends with */. The contents of &lt;br /&gt;these special comments&lt;br&gt;&lt;br /&gt;are used by the javadoc system, but are otherwise used identically to the first &lt;br /&gt;type of comment.&lt;br&gt;&lt;br /&gt;javadoc is used to generate API documentation from the code. You'll learn more &lt;br /&gt;about javadoc on&lt;br&gt;&lt;br /&gt;Day 22, &amp;quot;Java Programming Tools.&amp;quot;&lt;br&gt;&lt;br /&gt;Literals&lt;br&gt;&lt;br /&gt;Literal is a programming language term that essentially means that what you type &lt;br /&gt;is what you get. For&lt;br&gt;&lt;br /&gt;example, if you type 4 in a Java program, you automatically get an integer with &lt;br /&gt;the value 4. If you type&lt;br&gt;&lt;br /&gt;'a', you get a character with the value a. Literals are used to indicate simple &lt;br /&gt;values in your Java&lt;br&gt;&lt;br /&gt;programs.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;New Term&lt;br&gt;&lt;br /&gt;A literal is a simple value where &amp;quot;what you type is what you get.&amp;quot;&lt;br&gt;&lt;br /&gt;Numbers, characters, and strings are all examples of literals.&lt;br&gt;&lt;br /&gt;Literals may seem intuitive most of the time, but there are some special cases &lt;br /&gt;of literals in Java for&lt;br&gt;&lt;br /&gt;different kinds of numbers, characters, strings, and boolean values.&lt;br&gt;&lt;br /&gt;Number Literals&lt;br&gt;&lt;br /&gt;There are several integer literals. 4, for example, is a decimal integer literal &lt;br /&gt;of type int (although you&lt;br&gt;&lt;br /&gt;can assign it to a variable of type byte or short because it's small enough to &lt;br /&gt;fit into those types). A&lt;br&gt;&lt;br /&gt;decimal integer literal larger than an int is automatically of type long. You &lt;br /&gt;also can force a smaller&lt;br&gt;&lt;br /&gt;number to a long by appending an L or l to that number (for example, 4L is a &lt;br /&gt;long integer of value&lt;br&gt;&lt;br /&gt;4). Negative integers are preceded by a minus sign-for example, -45.&lt;br&gt;&lt;br /&gt;Integers can also be expressed as octal or hexadecimal: A leading 0 indicates &lt;br /&gt;that a number is octal-for&lt;br&gt;&lt;br /&gt;example, 0777 or 0004. A leading 0x (or 0X) means that it is in hex (0xFF, &lt;br /&gt;0XAf45). Hexadecimal&lt;br&gt;&lt;br /&gt;numbers can contain regular digits (0-9) or upper- or lowercase hex digits (a-f &lt;br /&gt;or A-F).&lt;br&gt;&lt;br /&gt;Floating-point literals usually have two parts, the integer part and the decimal &lt;br /&gt;part-for example,&lt;br&gt;&lt;br /&gt;5.77777. A floating-point literal results in a floating-point number of type &lt;br /&gt;double, regardless of the&lt;br&gt;&lt;br /&gt;precision of the number. You can force the number to the type float by appending &lt;br /&gt;the letter f (or F) to&lt;br&gt;&lt;br /&gt;that number-for example, 2.56F.&lt;br&gt;&lt;br /&gt;You can use exponents in floating-point literals using the letter e or E &lt;br /&gt;followed by the exponent (which&lt;br&gt;&lt;br /&gt;can be a negative number): 10e45 or .36E-2.&lt;br&gt;&lt;br /&gt;Boolean Literals&lt;br&gt;&lt;br /&gt;Boolean literals consist of the keywords true and false. These keywords can be &lt;br /&gt;used anywhere you&lt;br&gt;&lt;br /&gt;need a test or as the only possible values for boolean variables.&lt;br&gt;&lt;br /&gt;Character Literals&lt;br&gt;&lt;br /&gt;Character literals are expressed by a single character surrounded by single &lt;br /&gt;quotes: 'a', '#', '3', and&lt;br&gt;&lt;br /&gt;so on. Characters are stored as 16-bit Unicode characters. Table 3.2 lists the &lt;br /&gt;special codes that can&lt;br&gt;&lt;br /&gt;represent nonprintable characters, as well as characters from the Unicode &lt;br /&gt;character set. The letter d in the&lt;br&gt;&lt;br /&gt;octal, hex, and Unicode escapes represents a number or a hexadecimal digit (a-f &lt;br /&gt;or A-F).&lt;br&gt;&lt;br /&gt;Table 3.2. Character escape codes.&lt;br&gt;&lt;br /&gt;Escape Meaning&lt;br&gt;&lt;br /&gt;\n Newline&lt;br&gt;&lt;br /&gt;\t Tab&lt;br&gt;&lt;br /&gt;\b Backspace&lt;br&gt;&lt;br /&gt;\r Carriage return&lt;br&gt;&lt;br /&gt;\f Formfeed&lt;br&gt;&lt;br /&gt;\\ Backslash&lt;br&gt;&lt;br /&gt;\' Single quote&lt;br&gt;&lt;br /&gt;\&amp;quot; Double quote&lt;br&gt;&lt;br /&gt;\ddd Octal&lt;br&gt;&lt;br /&gt;\xdd Hexadecimal&lt;br&gt;&lt;br /&gt;\udddd Unicode character&lt;br&gt;&lt;br /&gt;Technical Note&lt;br&gt;&lt;br /&gt;C and C++ programmers should note that Java does not include&lt;br&gt;&lt;br /&gt;character codes for \a (bell) or \v (vertical tab).&lt;br&gt;&lt;br /&gt;String Literals&lt;br&gt;&lt;br /&gt;A combination of characters is a string. Strings in Java are instances of the &lt;br /&gt;class String. Strings are not&lt;br&gt;&lt;br /&gt;simply arrays of characters as they are in C or C++, although they do have many &lt;br /&gt;array-like&lt;br&gt;&lt;br /&gt;characteristics (for example, you can test their length, and access and change &lt;br /&gt;individual characters).&lt;br&gt;&lt;br /&gt;Because string objects are real objects in Java, they have methods that enable &lt;br /&gt;you to combine, test, and&lt;br&gt;&lt;br /&gt;modify strings very easily.&lt;br&gt;&lt;br /&gt;String literals consist of a series of characters inside double quotes:&lt;br&gt;&lt;br /&gt;&amp;quot;Hi, I'm a string literal.&amp;quot;&lt;br&gt;&lt;br /&gt;&amp;quot;&amp;quot; //an empty string&lt;br&gt;&lt;br /&gt;Strings can contain character constants such as newline, tab, and Unicode &lt;br /&gt;characters:&lt;br&gt;&lt;br /&gt;&amp;quot;A string with a \t tab in it&amp;quot;&lt;br&gt;&lt;br /&gt;&amp;quot;Nested strings are \&amp;quot;strings inside of\&amp;quot; other strings&amp;quot;&lt;br&gt;&lt;br /&gt;&amp;quot;This string brought to you by Java\u2122&amp;quot;&lt;br&gt;&lt;br /&gt;In the last example, the Unicode code sequence for \u2122 produces a trademark &lt;br /&gt;symbol ( ).&lt;br&gt;&lt;br /&gt;Note&lt;br&gt;&lt;br /&gt;Just because you can represent a character using a Unicode escape&lt;br&gt;&lt;br /&gt;does not mean your computer can display that character-the computer&lt;br&gt;&lt;br /&gt;or operating system you are running may not support Unicode, or the&lt;br&gt;&lt;br /&gt;font you're using may not have a glyph (picture) for that character.&lt;br&gt;&lt;br /&gt;All that Unicode escapes in Java provide is a way to encode Unicode&lt;br&gt;&lt;br /&gt;characters for systems that support Unicode.&lt;br&gt;&lt;br /&gt;Java 1.1 will provide better capabilities for the display of Unicode&lt;br&gt;&lt;br /&gt;characters and for handling international character sets.&lt;br&gt;&lt;br /&gt;When you use a string literal in your Java program, Java automatically creates &lt;br /&gt;an instance of the class&lt;br&gt;&lt;br /&gt;String for you with the value you give it. Strings are unusual in this respect; &lt;br /&gt;the other literals do not&lt;br&gt;&lt;br /&gt;behave in this way (none of the primitive data types are actual objects), and &lt;br /&gt;usually creating a new object&lt;br&gt;&lt;br /&gt;involves explicitly creating a new instance of a class. You'll learn more about &lt;br /&gt;strings, the String class,&lt;br&gt;&lt;br /&gt;and the things you can do with strings later today and tomorrow.&lt;br&gt;&lt;br /&gt;Expressions and Operators&lt;br&gt;&lt;br /&gt;Expressions are the simplest form of statement in Java that actually &lt;br /&gt;accomplishes something: All&lt;br&gt;&lt;br /&gt;expressions, when evaluated, return a value (other statements don't necessarily &lt;br /&gt;do so). Arithmetic and&lt;br&gt;&lt;br /&gt;tests for equality and magnitude are common examples of expressions. Because &lt;br /&gt;they return a value, you&lt;br&gt;&lt;br /&gt;can assign that result to a variable or test that value in other Java &lt;br /&gt;statements.&lt;br&gt;&lt;br /&gt;Most of the expressions in Java use operators. Operators are special symbols for &lt;br /&gt;things like arithmetic,&lt;br&gt;&lt;br /&gt;various forms of assignment, increment and decrement, and logical operations.&lt;br&gt;&lt;br /&gt;New Term&lt;br&gt;&lt;br /&gt;Expressions are statements that return a value.&lt;br&gt;&lt;br /&gt;New Term&lt;br&gt;&lt;br /&gt;Operators are special symbols that are commonly used in expressions.&lt;br&gt;&lt;br /&gt;Arithmetic&lt;br&gt;&lt;br /&gt;Java has five operators for basic arithmetic (see Table 3.3).&lt;br&gt;&lt;br /&gt;Table 3.3. Arithmetic operators.&lt;br&gt;&lt;br /&gt;Operator Meaning Example&lt;br&gt;&lt;br /&gt;+ Addition 3 + 4&lt;br&gt;&lt;br /&gt;- Subtraction 5 - 7&lt;br&gt;&lt;br /&gt;* Multiplication 5 * 5&lt;br&gt;&lt;br /&gt;/ Division 14 / 7&lt;br&gt;&lt;br /&gt;% Modulus 20 % 7&lt;br&gt;&lt;br /&gt;Each operator takes two operands, one on either side of the operator. The &lt;br /&gt;subtraction operator (-) can&lt;br&gt;&lt;br /&gt;also be used to negate a single operand.&lt;br&gt;&lt;br /&gt;Integer division results in an integer. Because integers don't have decimal &lt;br /&gt;fractions, any remainder is&lt;br&gt;&lt;br /&gt;ignored. The expression 31 / 9, for example, results in 3 (9 goes into 31 only 3 &lt;br /&gt;times).&lt;br&gt;&lt;br /&gt;Modulus (%) gives the remainder once the operands have been evenly divided. For &lt;br /&gt;example, 31 % 9&lt;br&gt;&lt;br /&gt;results in 4 because 9 goes into 31 three times, with 4 left over.&lt;br&gt;&lt;br /&gt;Note that the result type of most arithmetic operations involving integers is an &lt;br /&gt;int regardless of the&lt;br&gt;&lt;br /&gt;original type of the operands (shorts and bytes are both automatically converted &lt;br /&gt;to int). If either or&lt;br&gt;&lt;br /&gt;both operands is of type long, the result is of type long. If one operand is an &lt;br /&gt;integer and another is a&lt;br&gt;&lt;br /&gt;floating-point number, the result is a floating point. (If you're interested in &lt;br /&gt;the details of how Java&lt;br&gt;&lt;br /&gt;promotes and converts numeric types from one type to another, you may want to &lt;br /&gt;check out the Java&lt;br&gt;&lt;br /&gt;Language Specification on Sun's official Java Web site at http://java.sun.com/; &lt;br /&gt;that's more&lt;br&gt;&lt;br /&gt;detail than I want to cover here.)&lt;br&gt;&lt;br /&gt;Listing 3.1 is an example of simple arithmetic in Java.&lt;br&gt;&lt;br /&gt;Listing 3.1. Simple arithmetic.&lt;br&gt;&lt;br /&gt;1: class ArithmeticTest {&lt;br&gt;&lt;br /&gt;2: public static void main (String args[]) {&lt;br&gt;&lt;br /&gt;3: short x = 6;&lt;br&gt;&lt;br /&gt;4: int y = 4;&lt;br&gt;&lt;br /&gt;5: float a = 12.5f;&lt;br&gt;&lt;br /&gt;6: float b = 7f;&lt;br&gt;&lt;br /&gt;7:&lt;br&gt;&lt;br /&gt;8: System.out.println(&amp;quot;x is &amp;quot; + x + &amp;quot;, y is &amp;quot; + y);&lt;br&gt;&lt;br /&gt;9: System.out.println(&amp;quot;x + y = &amp;quot; + (x + y));&lt;br&gt;&lt;br /&gt;10: System.out.println(&amp;quot;x - y = &amp;quot; + (x - y));&lt;br&gt;&lt;br /&gt;11: System.out.println(&amp;quot;x / y = &amp;quot; + (x / y));&lt;br&gt;&lt;br /&gt;12: System.out.println(&amp;quot;x % y = &amp;quot; + (x % y));&lt;br&gt;&lt;br /&gt;13:&lt;br&gt;&lt;br /&gt;14: System.out.println(&amp;quot;a is &amp;quot; + a + &amp;quot;, b is &amp;quot; + b);&lt;br&gt;&lt;br /&gt;15: System.out.println(&amp;quot;a / b = &amp;quot; + (a / b));&lt;br&gt;&lt;br /&gt;16: }&lt;br&gt;&lt;br /&gt;17: }&lt;br&gt;&lt;br /&gt;x is 6, y is 4&lt;br&gt;&lt;br /&gt;x + y = 10&lt;br&gt;&lt;br /&gt;x - y = 2&lt;br&gt;&lt;br /&gt;x / y = 1&lt;br&gt;&lt;br /&gt;x % y = 2&lt;br&gt;&lt;br /&gt;a is 12.5, b is 7&lt;br&gt;&lt;br /&gt;a / b = 1.78571&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;Analysis&lt;br&gt;&lt;br /&gt;In this simple Java application (note the main() method), you&lt;br&gt;&lt;br /&gt;initially define four variables in lines 3 through 6: x and y, which are&lt;br&gt;&lt;br /&gt;integers (type int), and a and b, which are floating-point numbers&lt;br&gt;&lt;br /&gt;(type float). Keep in mind that the default type for floating-point&lt;br&gt;&lt;br /&gt;literals (such as 12.5) is double, so to make sure these are&lt;br&gt;&lt;br /&gt;numbers of type float, you have to use an f after each one (lines 5&lt;br&gt;&lt;br /&gt;and 6).&lt;br&gt;&lt;br /&gt;The remainder of the program merely does some math with integers and &lt;br /&gt;floating-point numbers and&lt;br&gt;&lt;br /&gt;prints out the results.&lt;br&gt;&lt;br /&gt;There is one other thing to mention about this program: the method &lt;br /&gt;System.out.println().&lt;br&gt;&lt;br /&gt;You've seen this method on previous days, but you haven't really learned exactly &lt;br /&gt;what it does. The&lt;br&gt;&lt;br /&gt;System.out.println() method merely prints a message to the standard output of &lt;br /&gt;your system-to&lt;br&gt;&lt;br /&gt;the screen, to a special window, or maybe just to a special log file, depending &lt;br /&gt;on your system and the&lt;br&gt;&lt;br /&gt;development environment you're running. The System.out.println() method takes a &lt;br /&gt;single&lt;br&gt;&lt;br /&gt;argument-a string-but you can use + to concatenate multiple values into a single &lt;br /&gt;string, as you'll learn&lt;br&gt;&lt;br /&gt;later today.&lt;br&gt;&lt;br /&gt;More About Assignment&lt;br&gt;&lt;br /&gt;Variable assignment is a form of expression; in fact, because one assignment &lt;br /&gt;expression results in a&lt;br&gt;&lt;br /&gt;value, you can string them together like this:&lt;br&gt;&lt;br /&gt;x = y = z = 0;&lt;br&gt;&lt;br /&gt;In this example, all three variables now have the value 0.&lt;br&gt;&lt;br /&gt;The right side of an assignment expression is always evaluated before the &lt;br /&gt;assignment takes place. This&lt;br&gt;&lt;br /&gt;means that expressions such as x = x + 2 do the right thing; 2 is added to the &lt;br /&gt;value of x, and then&lt;br&gt;&lt;br /&gt;that new value is reassigned to x. In fact, this sort of operation is so common &lt;br /&gt;that Java has several&lt;br&gt;&lt;br /&gt;operators to do a shorthand version of this, borrowed from C and C++. Table 3.4 &lt;br /&gt;shows these shorthand&lt;br&gt;&lt;br /&gt;assignment operators.&lt;br&gt;&lt;br /&gt;Table 3.4. Assignment operators.&lt;br&gt;&lt;br /&gt;Expression Meaning&lt;br&gt;&lt;br /&gt;x += y x = x + y&lt;br&gt;&lt;br /&gt;x -= y x = x - y&lt;br&gt;&lt;br /&gt;x *= y x = x * y&lt;br&gt;&lt;br /&gt;x /= y x = x / y&lt;br&gt;&lt;br /&gt;Technical Note&lt;br&gt;&lt;br /&gt;Technically, the shorthand assignment and longhand expressions are&lt;br&gt;&lt;br /&gt;not exactly equivalent, particularly in cases where x or y may&lt;br&gt;&lt;br /&gt;themselves be complicated expressions and your code relies on side&lt;br&gt;&lt;br /&gt;effects of those expressions. In most instances, however, they are&lt;br&gt;&lt;br /&gt;functionally equivalent. For more information about very complicated&lt;br&gt;&lt;br /&gt;expressions, evaluation order, and side effects, you may want to&lt;br&gt;&lt;br /&gt;consult the Java Language Specification.&lt;br&gt;&lt;br /&gt;Incrementing and Decrementing&lt;br&gt;&lt;br /&gt;As in C and C++, the ++ and -- operators are used to increment or decrement a &lt;br /&gt;variable's value by 1.&lt;br&gt;&lt;br /&gt;For example, x++ increments the value of x by 1 just as if you had used the &lt;br /&gt;expression x = x + 1.&lt;br&gt;&lt;br /&gt;Similarly x-- decrements the value of x by 1. (Unlike C and C++, Java allows x &lt;br /&gt;to be floating point.)&lt;br&gt;&lt;br /&gt;These increment and decrement operators can be prefixed or postfixed; that is, &lt;br /&gt;the ++ or -- can appear&lt;br&gt;&lt;br /&gt;before or after the value it increments or decrements. For simple increment or &lt;br /&gt;decrement expressions,&lt;br&gt;&lt;br /&gt;which one you use isn't overly important. In complex assignments, where you are &lt;br /&gt;assigning the result of&lt;br&gt;&lt;br /&gt;an increment or decrement expression, which one you use makes a difference.&lt;br&gt;&lt;br /&gt;Take, for example, the following two expressions:&lt;br&gt;&lt;br /&gt;y = x++;&lt;br&gt;&lt;br /&gt;y = ++x;&lt;br&gt;&lt;br /&gt;These two expressions yield very different results because of the difference &lt;br /&gt;between prefix and postfix.&lt;br&gt;&lt;br /&gt;When you use postfix operators (x++ or x--), y gets the value of x before x is &lt;br /&gt;changed; using prefix,&lt;br&gt;&lt;br /&gt;the value of x is assigned to y after the change has occurred. Listing 3.2 is a &lt;br /&gt;Java example of how all this&lt;br&gt;&lt;br /&gt;works.&lt;br&gt;&lt;br /&gt;Listing 3.2. Test of prefix and postfix increment operators.&lt;br&gt;&lt;br /&gt;1: class PrePostFixTest {&lt;br&gt;&lt;br /&gt;2:&lt;br&gt;&lt;br /&gt;3: public static void main (String args[]) {&lt;br&gt;&lt;br /&gt;4: int x = 0;&lt;br&gt;&lt;br /&gt;5: int y = 0;&lt;br&gt;&lt;br /&gt;6:&lt;br&gt;&lt;br /&gt;7: System.out.println(&amp;quot;x and y are &amp;quot; + x + &amp;quot; and &amp;quot; + y );&lt;br&gt;&lt;br /&gt;8: x++;&lt;br&gt;&lt;br /&gt;9: System.out.println(&amp;quot;x++ results in &amp;quot; + x);&lt;br&gt;&lt;br /&gt;10: ++x;&lt;br&gt;&lt;br /&gt;11: System.out.println(&amp;quot;++x results in &amp;quot; + x);&lt;br&gt;&lt;br /&gt;12: System.out.println(&amp;quot;Resetting x back to 0.&amp;quot;);&lt;br&gt;&lt;br /&gt;13: x = 0;&lt;br&gt;&lt;br /&gt;14: System.out.println(&amp;quot;------------&amp;quot;);&lt;br&gt;&lt;br /&gt;15: y = x++;&lt;br&gt;&lt;br /&gt;16: System.out.println(&amp;quot;y = x++ (postfix) results in:&amp;quot;);&lt;br&gt;&lt;br /&gt;17: System.out.println(&amp;quot;x is &amp;quot; + x);&lt;br&gt;&lt;br /&gt;18: System.out.println(&amp;quot;y is &amp;quot; + y);&lt;br&gt;&lt;br /&gt;19: System.out.println(&amp;quot;------------&amp;quot;);&lt;br&gt;&lt;br /&gt;20:&lt;br&gt;&lt;br /&gt;21: y = ++x;&lt;br&gt;&lt;br /&gt;22: System.out.println(&amp;quot;y = ++x (prefix) results in:&amp;quot;);&lt;br&gt;&lt;br /&gt;23: System.out.println(&amp;quot;x is &amp;quot; + x);&lt;br&gt;&lt;br /&gt;24: System.out.println(&amp;quot;y is &amp;quot; + y);&lt;br&gt;&lt;br /&gt;25: System.out.println(&amp;quot;------------&amp;quot;);&lt;br&gt;&lt;br /&gt;26:&lt;br&gt;&lt;br /&gt;27: }&lt;br&gt;&lt;br /&gt;28: }&lt;br&gt;&lt;br /&gt;x and y are 0 and 0&lt;br&gt;&lt;br /&gt;x++ results in 1&lt;br&gt;&lt;br /&gt;++x results in 2&lt;br&gt;&lt;br /&gt;Resetting x back to 0.&lt;br&gt;&lt;br /&gt;------------&lt;br&gt;&lt;br /&gt;y = x++ (postfix) results in:&lt;br&gt;&lt;br /&gt;x is 1&lt;br&gt;&lt;br /&gt;y is 0&lt;br&gt;&lt;br /&gt;------------&lt;br&gt;&lt;br /&gt;y = ++x (prefix) results in:&lt;br&gt;&lt;br /&gt;x is 2&lt;br&gt;&lt;br /&gt;y is 2&lt;br&gt;&lt;br /&gt;------------&lt;br&gt;&lt;br /&gt;In the first part of this example, you increment x alone using both prefix and &lt;br /&gt;postfix increment&lt;br&gt;&lt;br /&gt;operators. In each, x is incremented by 1 each time. In this simple form, using &lt;br /&gt;either prefix or&lt;br&gt;&lt;br /&gt;postfix works the same way.&lt;br&gt;&lt;br /&gt;In the second part of this example, you use the expression y = x++, in which the &lt;br /&gt;postfix increment&lt;br&gt;&lt;br /&gt;operator is used. In this result, the value of x is incremented after that value &lt;br /&gt;is assigned to y. Hence the&lt;br&gt;&lt;br /&gt;result: y is assigned the original value of x (0), and then x is incremented by &lt;br /&gt;1.&lt;br&gt;&lt;br /&gt;In the third part, you use the prefix expression y = ++x. Here, the reverse &lt;br /&gt;occurs: x is incremented&lt;br&gt;&lt;br /&gt;before its value is assigned to y. Because x is 1 from the previous step, its &lt;br /&gt;value is incremented (to 2),&lt;br&gt;&lt;br /&gt;and then that value is assigned to y. Both x and y end up being 2.&lt;br&gt;&lt;br /&gt;Technical Note&lt;br&gt;&lt;br /&gt;Technically, this description is not entirely correct. In reality, Java&lt;br&gt;&lt;br /&gt;always completely evaluates all expressions on the right of an&lt;br&gt;&lt;br /&gt;expression before assigning that value to a variable, so the concept of&lt;br&gt;&lt;br /&gt;&amp;quot;assigning x to y before x is incremented&amp;quot; isn't precisely right.&lt;br&gt;&lt;br /&gt;Instead, Java takes the value of x and &amp;quot;remembers&amp;quot; it, evaluates&lt;br&gt;&lt;br /&gt;(increments) x, and then assigns the original value of x to y. Although&lt;br&gt;&lt;br /&gt;in most simple cases this distinction may not be important, for more&lt;br&gt;&lt;br /&gt;complex expressions with side effects, it may change the behavior of&lt;br&gt;&lt;br /&gt;the expression overall. See the Language Specification for many more&lt;br&gt;&lt;br /&gt;details about expression evaluation in Java.&lt;br&gt;&lt;br /&gt;Comparisons&lt;br&gt;&lt;br /&gt;Java has several expressions for testing equality and magnitude. All of these &lt;br /&gt;expressions return a boolean&lt;br&gt;&lt;br /&gt;value (that is, true or false). Table 3.5 shows the comparison operators.&lt;br&gt;&lt;br /&gt;Table 3.5. Comparison operators.&lt;br&gt;&lt;br /&gt;Operator Meaning Example&lt;br&gt;&lt;br /&gt;== Equal x == 3&lt;br&gt;&lt;br /&gt;!= Not equal x != 3&lt;br&gt;&lt;br /&gt;&amp;lt; Less than x &amp;lt; 3&lt;br&gt;&lt;br /&gt;&amp;gt; Greater than x &amp;gt; 3&lt;br&gt;&lt;br /&gt;&amp;lt;= Less than or equal to x &amp;lt;= 3&lt;br&gt;&lt;br /&gt;&amp;gt;= Greater than or equal to x &amp;gt;= 3&lt;br&gt;&lt;br /&gt;Logical Operators&lt;br&gt;&lt;br /&gt;Expressions that result in boolean values (for example, the comparison &lt;br /&gt;operators) can be combined by&lt;br&gt;&lt;br /&gt;using logical operators that represent the logical combinations AND, OR, XOR, &lt;br /&gt;and logical NOT.&lt;br&gt;&lt;br /&gt;For AND combinations, use either the &amp;amp; or &amp;amp;&amp;amp; operators. The entire expression &lt;br /&gt;will be true only if both&lt;br&gt;&lt;br /&gt;expressions on either side of the operator are also true; if either expression &lt;br /&gt;is false, the entire expression&lt;br&gt;&lt;br /&gt;is false. The difference between the two operators is in expression evaluation. &lt;br /&gt;Using &amp;amp;, both sides of the&lt;br&gt;&lt;br /&gt;expression are evaluated regardless of the outcome. Using &amp;amp;&amp;amp;, if the left side &lt;br /&gt;of the expression is false,&lt;br&gt;&lt;br /&gt;the entire expression is assumed to be false (the value of the right side &lt;br /&gt;doesn't matter), so the expression&lt;br&gt;&lt;br /&gt;returns false, and the right side of the expression is never evaluated. (This is &lt;br /&gt;often called a&lt;br&gt;&lt;br /&gt;&amp;quot;short-circuited&amp;quot; expression.)&lt;br&gt;&lt;br /&gt;For OR expressions, use either | or ||. OR expressions result in true if either &lt;br /&gt;or both of the expressions&lt;br&gt;&lt;br /&gt;on either side is also true; if both expression operands are false, the &lt;br /&gt;expression is false. As with &amp;amp; and&lt;br&gt;&lt;br /&gt;&amp;amp;&amp;amp;, the single | evaluates both sides of the expression regardless of the &lt;br /&gt;outcome; and || is&lt;br&gt;&lt;br /&gt;short-circuited: If the left expression is true, the expression returns true and &lt;br /&gt;the right side is never&lt;br&gt;&lt;br /&gt;evaluated.&lt;br&gt;&lt;br /&gt;In addition, there is the XOR operator ^, which returns true only if its &lt;br /&gt;operands are different (one true&lt;br&gt;&lt;br /&gt;and one false, or vice versa) and false otherwise (even if both are true).&lt;br&gt;&lt;br /&gt;In general, only the &amp;amp;&amp;amp; and || are commonly used as actual logical combinations. &lt;br /&gt;&amp;amp;, |, and ^ are more&lt;br&gt;&lt;br /&gt;commonly used for bitwise logical operations.&lt;br&gt;&lt;br /&gt;For NOT, use the ! operator with a single expression argument. The value of the &lt;br /&gt;NOT expression is the&lt;br&gt;&lt;br /&gt;negation of the expression; if x is true, !x is false.&lt;br&gt;&lt;br /&gt;Bitwise Operators&lt;br&gt;&lt;br /&gt;Finally, here's a short summary of the bitwise operators in Java. Most of these &lt;br /&gt;expressions are inherited&lt;br&gt;&lt;br /&gt;from C and C++ and are used to perform operations on individual bits in &lt;br /&gt;integers. This book does not go&lt;br&gt;&lt;br /&gt;into bitwise operations; it's an advanced topic covered better in books on C or &lt;br /&gt;C++. Table 3.6&lt;br&gt;&lt;br /&gt;summarizes the bitwise operators.&lt;br&gt;&lt;br /&gt;Table 3.6. Bitwise operators.&lt;br&gt;&lt;br /&gt;Operator Meaning&lt;br&gt;&lt;br /&gt;&amp;amp; Bitwise AND&lt;br&gt;&lt;br /&gt;| Bitwise OR&lt;br&gt;&lt;br /&gt;^ Bitwise XOR&lt;br&gt;&lt;br /&gt;&amp;lt;&amp;lt; Left shift&lt;br&gt;&lt;br /&gt;&amp;gt;&amp;gt; Right shift&lt;br&gt;&lt;br /&gt;&amp;gt;&amp;gt;&amp;gt; Zero fill right shift&lt;br&gt;&lt;br /&gt;~ Bitwise complement&lt;br&gt;&lt;br /&gt;&amp;lt;&amp;lt;= Left shift assignment (x = x &amp;lt;&amp;lt; y)&lt;br&gt;&lt;br /&gt;&amp;gt;&amp;gt;= Right shift assignment (x = x &amp;gt;&amp;gt; y)&lt;br&gt;&lt;br /&gt;&amp;gt;&amp;gt;&amp;gt;= Zero fill right shift assignment (x = x&lt;br&gt;&lt;br /&gt;&amp;gt;&amp;gt;&amp;gt; y)&lt;br&gt;&lt;br /&gt;x&amp;amp;=y AND assignment (x = x &amp;amp; y)&lt;br&gt;&lt;br /&gt;x|=y OR assignment (x = x | y)&lt;br&gt;&lt;br /&gt;x^=y XOR assignment (x = x ^ y)&lt;br&gt;&lt;br /&gt;Operator Precedence&lt;br&gt;&lt;br /&gt;Operator precedence determines the order in which expressions are evaluated. &lt;br /&gt;This, in some cases, can&lt;br&gt;&lt;br /&gt;determine the overall value of the expression. For example, take the following &lt;br /&gt;expression:&lt;br&gt;&lt;br /&gt;y = 6 + 4 / 2&lt;br&gt;&lt;br /&gt;Depending on whether the 6 + 4 expression or the 4 / 2 expression is evaluated &lt;br /&gt;first, the value of y&lt;br&gt;&lt;br /&gt;can end up being 5 or 8. Operator precedence determines the order in which &lt;br /&gt;expressions are evaluated,&lt;br&gt;&lt;br /&gt;so you can predict the outcome of an expression. In general, increment and &lt;br /&gt;decrement are evaluated&lt;br&gt;&lt;br /&gt;before arithmetic, arithmetic expressions are evaluated before comparisons, and &lt;br /&gt;comparisons are&lt;br&gt;&lt;br /&gt;evaluated before logical expressions. Assignment expressions are evaluated last.&lt;br&gt;&lt;br /&gt;Table 3.7 shows the specific precedence of the various operators in Java. &lt;br /&gt;Operators further up in the table&lt;br&gt;&lt;br /&gt;are evaluated first; operators on the same line have the same precedence and are &lt;br /&gt;evaluated left to right&lt;br&gt;&lt;br /&gt;based on how they appear in the expression itself. For example, given that same &lt;br /&gt;expression y = 6 + 4&lt;br&gt;&lt;br /&gt;/ 2, you now know, according to this table, that division is evaluated before &lt;br /&gt;addition, so the value of y&lt;br&gt;&lt;br /&gt;will be 8.&lt;br&gt;&lt;br /&gt;Table 3.7. Operator precedence.&lt;br&gt;&lt;br /&gt;Operator Notes&lt;br&gt;&lt;br /&gt;. [] () Parentheses (()) are used to group expressions; dot&lt;br&gt;&lt;br /&gt;(.) is used for access to methods and variables&lt;br&gt;&lt;br /&gt;within objects and classes (discussed tomorrow);&lt;br&gt;&lt;br /&gt;square brackets ([]) are used for arrays (this is&lt;br&gt;&lt;br /&gt;discussed later on in the week)&lt;br&gt;&lt;br /&gt;++ -- ! ~&lt;br&gt;&lt;br /&gt;instanceof&lt;br&gt;&lt;br /&gt;The instanceof operator returns true or&lt;br&gt;&lt;br /&gt;false based on whether the object is an instance&lt;br&gt;&lt;br /&gt;of the named class or any of that class's subclasses&lt;br&gt;&lt;br /&gt;(discussed tomorrow)&lt;br&gt;&lt;br /&gt;new (type)expression The new operator is used for creating new instances&lt;br&gt;&lt;br /&gt;of classes; () in this case is for casting a value to&lt;br&gt;&lt;br /&gt;another type (you'll learn about both of these&lt;br&gt;&lt;br /&gt;tomorrow)&lt;br&gt;&lt;br /&gt;* / % Multiplication, division, modulus&lt;br&gt;&lt;br /&gt;+ - Addition, subtraction&lt;br&gt;&lt;br /&gt;&amp;lt;&amp;lt; &amp;gt;&amp;gt; &amp;gt;&amp;gt;&amp;gt; Bitwise left and right shift&lt;br&gt;&lt;br /&gt;&amp;lt; &amp;gt; &amp;lt;= &amp;gt;= Relational comparison tests&lt;br&gt;&lt;br /&gt;== != Equality&lt;br&gt;&lt;br /&gt;&amp;amp; AND&lt;br&gt;&lt;br /&gt;^ XOR&lt;br&gt;&lt;br /&gt;| OR&lt;br&gt;&lt;br /&gt;&amp;amp;&amp;amp; Logical AND&lt;br&gt;&lt;br /&gt;|| Logical OR&lt;br&gt;&lt;br /&gt;? : Shorthand for if...then...else (discussed on&lt;br&gt;&lt;br /&gt;Day 5)&lt;br&gt;&lt;br /&gt;= += -= *= /= %= ^= Various assignments&lt;br&gt;&lt;br /&gt;&amp;amp;= |= &amp;lt;&amp;lt;= &amp;gt;&amp;gt;= &amp;gt;&amp;gt;&amp;gt;= More assignments&lt;br&gt;&lt;br /&gt;You can always change the order in which expressions are evaluated by using &lt;br /&gt;parentheses around the&lt;br&gt;&lt;br /&gt;expressions you want to evaluate first. You can nest parentheses to make sure &lt;br /&gt;expressions evaluate in the&lt;br&gt;&lt;br /&gt;order you want them to (the innermost parenthetic expression is evaluated &lt;br /&gt;first). The following&lt;br&gt;&lt;br /&gt;expression results in a value of 5, because the 6 + 4 expression is evaluated &lt;br /&gt;first, and then the result of&lt;br&gt;&lt;br /&gt;that expression (10) is divided by 2:&lt;br&gt;&lt;br /&gt;y = (6 + 4) / 2&lt;br&gt;&lt;br /&gt;Parentheses also can be useful in cases where the precedence of an expression &lt;br /&gt;isn't immediately clear-in&lt;br&gt;&lt;br /&gt;other words, they can make your code easier to read. Adding parentheses doesn't &lt;br /&gt;hurt, so if they help you&lt;br&gt;&lt;br /&gt;figure out how expressions are evaluated, go ahead and use them.&lt;br&gt;&lt;br /&gt;String Arithmetic&lt;br&gt;&lt;br /&gt;One special expression in Java is the use of the addition operator (+) to create &lt;br /&gt;and concatenate strings. In&lt;br&gt;&lt;br /&gt;most of the examples shown today and in earlier lessons, you've seen lots of &lt;br /&gt;lines that looked something&lt;br&gt;&lt;br /&gt;like this:&lt;br&gt;&lt;br /&gt;System.out.println(name + &amp;quot; is a &amp;quot; + color + &amp;quot; beetle&amp;quot;);&lt;br&gt;&lt;br /&gt;The output of that line (to the standard output) is a single string, with the &lt;br /&gt;values of the variables (name&lt;br&gt;&lt;br /&gt;and color), inserted in the appropriate spots in the string. So what's going on &lt;br /&gt;here?&lt;br&gt;&lt;br /&gt;The + operator, when used with strings and other objects, creates a single &lt;br /&gt;string that contains the&lt;br&gt;&lt;br /&gt;concatenation of all its operands. If any of the operands in string &lt;br /&gt;concatenation is not a string, it is&lt;br&gt;&lt;br /&gt;automatically converted to a string, making it easy to create these sorts of &lt;br /&gt;output lines.&lt;br&gt;&lt;br /&gt;Technical Note&lt;br&gt;&lt;br /&gt;An object or type can be converted to a string if you implement the&lt;br&gt;&lt;br /&gt;method toString(). All objects have a default string&lt;br&gt;&lt;br /&gt;representation, but most classes override toString() to provide a&lt;br&gt;&lt;br /&gt;more meaningful printable representation.&lt;br&gt;&lt;br /&gt;String concatenation makes lines such as the previous one especially easy to &lt;br /&gt;construct. To create a string,&lt;br&gt;&lt;br /&gt;just add all the parts together-the descriptions plus the variables-and print it &lt;br /&gt;to the standard output, to the&lt;br&gt;&lt;br /&gt;screen, to an applet, or anywhere.&lt;br&gt;&lt;br /&gt;The += operator, which you learned about earlier, also works for strings. For &lt;br /&gt;example, take the following&lt;br&gt;&lt;br /&gt;expression:&lt;br&gt;&lt;br /&gt;myName += &amp;quot; Jr.&amp;quot;;&lt;br&gt;&lt;br /&gt;This expression is equivalent to this:&lt;br&gt;&lt;br /&gt;myName = myName + &amp;quot; Jr.&amp;quot;;&lt;br&gt;&lt;br /&gt;just as it would be for numbers. In this case, it changes the value of myName, &lt;br /&gt;which might be something&lt;br&gt;&lt;br /&gt;like John Smith to have a Jr. at the end (John Smith Jr.).&lt;br&gt;&lt;br /&gt;Summary&lt;br&gt;&lt;br /&gt;As you have learned in the last two lessons, a Java program is made up primarily &lt;br /&gt;of classes and objects.&lt;br&gt;&lt;br /&gt;Classes and objects, in turn, are made up of methods and variables, and methods &lt;br /&gt;are made up of&lt;br&gt;&lt;br /&gt;statements and expressions. It is those last two things that you've learned &lt;br /&gt;about today; the basic building&lt;br&gt;&lt;br /&gt;blocks that enable you to create classes and methods and build them up to a &lt;br /&gt;full-fledged Java program.&lt;br&gt;&lt;br /&gt;Today, you have learned about variables, how to declare them and assign values &lt;br /&gt;to them; literals for&lt;br&gt;&lt;br /&gt;easily creating numbers, characters, and strings; and operators for arithmetic, &lt;br /&gt;tests, and other simple&lt;br&gt;&lt;br /&gt;operations. With this basic syntax, you can move on tomorrow to learning about &lt;br /&gt;working with objects&lt;br&gt;&lt;br /&gt;and building simple, useful Java programs.&lt;br&gt;&lt;br /&gt;To finish up this summary, Table 3.8 is a list of all the operators you have &lt;br /&gt;learned about today so that&lt;br&gt;&lt;br /&gt;you can refer back to them.&lt;br&gt;&lt;br /&gt;Table 3.8. Operator summary.&lt;br&gt;&lt;br /&gt;Operator Meaning&lt;br&gt;&lt;br /&gt;+ Addition&lt;br&gt;&lt;br /&gt;- Subtraction&lt;br&gt;&lt;br /&gt;* Multiplication&lt;br&gt;&lt;br /&gt;/ Division&lt;br&gt;&lt;br /&gt;% Modulus&lt;br&gt;&lt;br /&gt;&amp;lt; Less than&lt;br&gt;&lt;br /&gt;&amp;gt; Greater than&lt;br&gt;&lt;br /&gt;&amp;lt;= Less than or equal to&lt;br&gt;&lt;br /&gt;&amp;gt;= Greater than or equal to&lt;br&gt;&lt;br /&gt;== Equal&lt;br&gt;&lt;br /&gt;!= Not equal&lt;br&gt;&lt;br /&gt;&amp;amp;&amp;amp; Logical AND&lt;br&gt;&lt;br /&gt;|| Logical OR&lt;br&gt;&lt;br /&gt;! Logical NOT&lt;br&gt;&lt;br /&gt;&amp;amp; AND&lt;br&gt;&lt;br /&gt;| OR&lt;br&gt;&lt;br /&gt;^ XOR&lt;br&gt;&lt;br /&gt;&amp;lt;&amp;lt; Left shift&lt;br&gt;&lt;br /&gt;&amp;gt;&amp;gt; Right shift&lt;br&gt;&lt;br /&gt;&amp;gt;&amp;gt;&amp;gt; Zero fill right shift&lt;br&gt;&lt;br /&gt;~ Complement&lt;br&gt;&lt;br /&gt;= Assignment&lt;br&gt;&lt;br /&gt;++ Increment&lt;br&gt;&lt;br /&gt;---- Decrement&lt;br&gt;&lt;br /&gt;+= Add and assign&lt;br&gt;&lt;br /&gt;-= Subtract and assign&lt;br&gt;&lt;br /&gt;*= Multiply and assign&lt;br&gt;&lt;br /&gt;/= Divide and assign&lt;br&gt;&lt;br /&gt;%= Modulus and assign&lt;br&gt;&lt;br /&gt;&amp;amp;= AND and assign&lt;br&gt;&lt;br /&gt;|= OR and assign&lt;br&gt;&lt;br /&gt;&amp;lt;&amp;lt;= Left shift and assign&lt;br&gt;&lt;br /&gt;^= XOR and assign&lt;br&gt;&lt;br /&gt;&amp;gt;&amp;gt;= Right shift and assign&lt;br&gt;&lt;br /&gt;&amp;gt;&amp;gt;&amp;gt;= Zero fill right shift and assign&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;Q&amp;amp;A&lt;br&gt;&lt;br /&gt;Q: I didn't see any way to define constants.&lt;br&gt;&lt;br /&gt;A: You can't create local constants in Java; you can create only constant &lt;br /&gt;instance and class&lt;br&gt;&lt;br /&gt;variables. You'll learn how to do this tomorrow.&lt;br&gt;&lt;br /&gt;Q: What happens if you assign an integer value to a variable that is too large &lt;br /&gt;for that variable&lt;br&gt;&lt;br /&gt;to hold?&lt;br&gt;&lt;br /&gt;A: Logically, you would think that the variable is just converted to the next &lt;br /&gt;larger type, but this isn't&lt;br&gt;&lt;br /&gt;what happens. What does happen is called overflow. This means that if a number &lt;br /&gt;becomes too&lt;br&gt;&lt;br /&gt;big for its variable, that number wraps around to the smallest possible negative &lt;br /&gt;number for that&lt;br&gt;&lt;br /&gt;type and starts counting upward toward zero again.&lt;br&gt;&lt;br /&gt;Because this can result in some very confusing (and wrong) results, make sure &lt;br /&gt;that you declare&lt;br&gt;&lt;br /&gt;the right integer type for all your numbers. If there's a chance a number will &lt;br /&gt;overflow its type,&lt;br&gt;&lt;br /&gt;use the next larger type instead.&lt;br&gt;&lt;br /&gt;Q: How can you find out the type of a given variable?&lt;br&gt;&lt;br /&gt;A: If you're using any of the primitive types (int, float, boolean), and so on, &lt;br /&gt;you can't. If you&lt;br&gt;&lt;br /&gt;care about the type, you can convert the value to some other type by using &lt;br /&gt;casting. (You'll learn&lt;br&gt;&lt;br /&gt;about this tomorrow.)&lt;br&gt;&lt;br /&gt;If you're using class types, you can use the instanceof operator, which you'll &lt;br /&gt;learn more&lt;br&gt;&lt;br /&gt;about tomorrow.&lt;br&gt;&lt;br /&gt;Q: Why does Java have all these shorthand operators for arithmetic and &lt;br /&gt;assignment? It's&lt;br&gt;&lt;br /&gt;really hard to read that way.&lt;br&gt;&lt;br /&gt;A: The syntax of Java is based on C++, and therefore on C. One of C's implicit &lt;br /&gt;goals is the&lt;br&gt;&lt;br /&gt;capability of doing very powerful things with a minimum of typing. Because of &lt;br /&gt;this, shorthand&lt;br&gt;&lt;br /&gt;operators, such as the wide array of assignments, are common.&lt;br&gt;&lt;br /&gt;There's no rule that says you have to use these operators in your own programs, &lt;br /&gt;however. If you&lt;br&gt;&lt;br /&gt;find your code to be more readable using the long form, no one will come to your &lt;br /&gt;house and&lt;br&gt;&lt;br /&gt;&lt;a href="http://www.sbinfosystem.com/"&gt;make you change it&lt;/a&gt;.&lt;br&gt;&lt;br /&gt;Q: You covered simple math in this section using operators. I'm assuming that &lt;br /&gt;Java has ways&lt;br&gt;&lt;br /&gt;of doing more complex math operations?&lt;br&gt;&lt;br /&gt;A: You assume correctly. A special class in the java.lang package, called &lt;br /&gt;java.lang.Math,&lt;br&gt;&lt;br /&gt;has a number of methods for exponential, trigonometric, and other basic math &lt;br /&gt;operations. In fact,&lt;br&gt;&lt;br /&gt;because you call these methods using the Math class itself, these are prime &lt;br /&gt;examples of class&lt;br&gt;&lt;br /&gt;methods. You'll learn more about this tomorrow.&lt;/p&gt;</description><thr:total xmlns:thr="http://purl.org/syndication/thread/1.0">0</thr:total></item><item><title>Java Help, Java Tutorials, Java Programming, Java Tricks</title><link>http://swebsolution.blogspot.com/2008/01/java-help-java-tutorials-java_04.html</link><author>noreply@blogger.com (Unknown)</author><pubDate>Fri, 4 Jan 2008 06:54:00 -0800</pubDate><guid isPermaLink="false">tag:blogger.com,1999:blog-7227425366482465228.post-8726632707398653588</guid><description>&lt;p align="justify"&gt;&lt;b&gt;Lesson Two&lt;/b&gt; &lt;br&gt;&lt;br&gt;Thinking in Objects: An Analogy&lt;br&gt;&lt;br /&gt;CONTENTS&lt;br&gt;&lt;br /&gt; Objects and Classes&lt;br&gt;&lt;br /&gt;   Behavior and Attributes&lt;br&gt;&lt;br /&gt;   Attributes&lt;br&gt;&lt;br /&gt;   Behavior&lt;br&gt;&lt;br /&gt;   Creating a Class&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt; Inheritance, Interfaces, and Packages&lt;br&gt;&lt;br /&gt;   Inheritance&lt;br&gt;&lt;br /&gt;   Creating a Class Hierarchy&lt;br&gt;&lt;br /&gt;   How Inheritance Works&lt;br&gt;&lt;br /&gt;   Single and Multiple Inheritance&lt;br&gt;&lt;br /&gt;   Interfaces and Packages&lt;br&gt;&lt;br /&gt;   Creating a Subclass&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt; Summary&lt;br&gt;&lt;br /&gt; Q&amp;amp;A&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;Object-oriented programming (OOP) is one of the biggest programming ideas of &lt;br /&gt;recent years, and you might worry that you must spend ears learning all about &lt;br /&gt;object-oriented programming methodologies and how they can make your life easier &lt;br /&gt;than The Old Way of programming. It all comes down to organizing your programs &lt;br /&gt;in ways that echo how things are put together in the real world.&lt;br&gt;&lt;br /&gt;Today you'll get an overview of object-oriented programming concepts in Java and &lt;br /&gt;how they relate to how you structure your own &lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;programs:&lt;br&gt;&lt;br /&gt;What classes and objects are and how they relate to each other&lt;br&gt;&lt;br /&gt;The two main parts of a class or object: its behaviors and its attributes&lt;br&gt;&lt;br /&gt;Class inheritance and how inheritance affects the way you design your programs&lt;br&gt;&lt;br /&gt;Some information about packages and interfaces&lt;br&gt;&lt;br /&gt;If you're already familiar with object-oriented programming, much of today's &lt;br /&gt;lesson will be old hat to you.&lt;br&gt;&lt;br /&gt;You may want to skim it and go to a movie today instead. Tomorrow, you'll get &lt;br /&gt;into more specific details.&lt;br&gt;&lt;br /&gt;Thinking in Objects: An Analogy&lt;br&gt;&lt;br /&gt;Consider, if you will, Legos. Legos, for those who do not spend much time with &lt;br /&gt;children, are small plastic building blocks in various colors and sizes. They &lt;br /&gt;have small round bits on one side that fit into small round holes on other Legos &lt;br /&gt;so that they fit together snugly to create larger shapes. With different Lego &lt;br /&gt;parts (Lego wheels, Lego engines, Lego hinges, Lego pulleys), you can put &lt;br /&gt;together castles, automobiles, giant robots that swallow cities, or just about &lt;br /&gt;anything else you can imagine. Each Lego part is a small object that fits &lt;br /&gt;together with other small objects in predefined ways to create other larger &lt;br /&gt;objects. That is roughly how object-oriented&lt;br&gt;&lt;br /&gt;programming works: putting together smaller elements to build larger ones.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;Here's another example. You can walk into a computer store and, with a little &lt;br /&gt;background and often some help, assemble an entire pc computer system from &lt;br /&gt;various components: a motherboard, a CPU chip, a video card, a hard disk, a &lt;br /&gt;keyboard, and so on. Ideally, when you finish assembling all the various &lt;br /&gt;self-contained units, you have a system in which all the units work together to &lt;br /&gt;create a larger system with which you can solve the problems you bought the &lt;br /&gt;computer for in the first place.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;Internally, each of those components may be vastly complicated and engineered by &lt;br /&gt;different companies with different methods of design. But you don't need to know &lt;br /&gt;how the component works, what every chip on the board does, or how, when you &lt;br /&gt;press the A key, an A gets sent to your computer. As the assembler of the &lt;br /&gt;overall system, each component you use is a self-contained unit, and all you are &lt;br /&gt;interested in is how the units interact with each other. Will this video card &lt;br /&gt;fit into the slots on the motherboard, and will this monitor work with this &lt;br /&gt;video card? Will each particular component speak the right commands to the other &lt;br /&gt;components it interacts with so that each part of the computer is understood by &lt;br /&gt;every other part? Once you know what the interactions are between the components &lt;br /&gt;and can match the interactions, putting together the overall system is easy.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;What does this have to do with programming? Everything. Object-oriented &lt;br /&gt;programming works in exactly this same way. Using object-oriented programming, &lt;br /&gt;your overall program is made up of lots of different self-contained components &lt;br /&gt;(objects), each of which has a specific role in the program and all of which can &lt;br /&gt;talk to each other in predefined ways.&lt;br&gt;&lt;br /&gt;Objects and Classes&lt;br&gt;&lt;br /&gt;Object-oriented programming is modeled on how, in the real world, objects are &lt;br /&gt;often ade up of many kindsof smaller objects. This capability of combining &lt;br /&gt;objects, owever, is only one very general aspect ofobject-oriented programming. &lt;br /&gt;Object-oriented rogramming provides several other concepts and features to make &lt;br /&gt;creating and using objects easier and more flexible, and the most important of &lt;br /&gt;these features is classes.&lt;br&gt;&lt;br /&gt;When you write a program in an object-oriented language, you don't define actual &lt;br /&gt;objects. You define classes of objects, where a class is a template for multiple &lt;br /&gt;objects with similar features. Classes embody all the features of a particular &lt;br /&gt;set of objects. For example, you might have a Tree class that describes the &lt;br /&gt;features of all trees (has leaves and roots, grows, creates chlorophyll). The &lt;br /&gt;Tree class serves as an abstract model for the concept of a tree-to reach out &lt;br /&gt;and grab, or interact with, or cut down a tree you have to have a concrete&lt;br&gt;&lt;br /&gt;instance of that tree. Of course, once you have a tree class, you can create &lt;br /&gt;lots of different instances of that tree, and each different tree instance can &lt;br /&gt;have different features (short, tall, bushy, drops leaves in autumn), while &lt;br /&gt;still behaving like and being immediately recognizable as a tree (see Figure &lt;br /&gt;2.1).&lt;br&gt;&lt;br /&gt;Figure 2.1 : The Tree class and several Tree instances.&lt;br&gt;&lt;br /&gt;New Term&lt;br&gt;&lt;br /&gt;A class is a generic template for a set of objects with similar features.&lt;br&gt;&lt;br /&gt;An instance of a class is another word for an actual object. If class is the &lt;br /&gt;general (generic) representation of an object, an instance is its concrete &lt;br /&gt;representation. So what, precisely, is the difference between an instance and an &lt;br /&gt;object? Nothing, really. Object is the more general term, but both instances and &lt;br /&gt;objects are the concrete representation of a class. In fact, the terms instance &lt;br /&gt;and object are often used interchangeably in OOP lingo.&lt;br&gt;&lt;br /&gt;An instance of a tree and a tree object are both the same thing.&lt;br&gt;&lt;br /&gt;New Term&lt;br&gt;&lt;br /&gt;An instance is the specific concrete representation of a class.&lt;br&gt;&lt;br /&gt;Instances and objects are the same thing.&lt;br&gt;&lt;br /&gt;What about an example closer to the sort of things you might want to do in Java &lt;br /&gt;programming? You might create a class for the user interface element called a &lt;br /&gt;button. The Button class defines the features of a button (its label, its size, &lt;br /&gt;its appearance) and how it behaves. (Does it need a single-click or a &lt;br /&gt;double-click to activate it? Does it change color when it's clicked? What does &lt;br /&gt;it do when it's activated?) After you define the Button class, you can then &lt;br /&gt;easily create instances of that button-that is, button objects-that all take on &lt;br /&gt;the basic features of the button as defined by the class, but may have different &lt;br /&gt;appearances and behavior based on what you want that particular button to do. By &lt;br /&gt;creating a Button class, you don't have to keep rewriting the code for each &lt;br /&gt;individual button you want to use in your program, and you can reuse the Button &lt;br /&gt;class to create different kinds of buttons as you need them in this program and &lt;br /&gt;in other programs.&lt;br&gt;&lt;br /&gt;Tip&lt;br&gt;&lt;br /&gt;If you're used to programming in C, you can think of a class as sort of creating &lt;br /&gt;a new composite data type by using struct and typedef. Classes, however, can &lt;br /&gt;provide much more than just a collection of data, as you'll discover in the rest &lt;br /&gt;of today's lesson. When you write a Java program, you design and construct a set &lt;br /&gt;of classes. Then when your program runs, instances of those classes are created &lt;br /&gt;and discarded as needed. Your task, as a Java programmer, is to create the right &lt;br /&gt;set of classes to accomplish what your program needs to accomplish. Fortunately, &lt;br /&gt;you don't have to start from the very beginning: The Java environment comes with &lt;br /&gt;a standard set&lt;br&gt;&lt;br /&gt;of classes (called a class library) that implement a lot of the basic behavior &lt;br /&gt;you need-not only for basic programming tasks (classes to provide basic math &lt;br /&gt;functions, arrays, strings, and so on), but also for graphics and networking &lt;br /&gt;behavior. In many cases, the Java class libraries may be enough so that all you &lt;br /&gt;have to do in your Java program is create a single class that uses the standard &lt;br /&gt;class libraries. For complicated Java programs, you may have to create a whole &lt;br /&gt;set of classes with defined interactions between them.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;New Term&lt;br&gt;&lt;br /&gt;A class library is a collection of classes intended to be reused repeatedly in &lt;br /&gt;different programs. The standard Java class libraries contain quite a few &lt;br /&gt;classes for accomplishing basic programming tasks in Java.&lt;br&gt;&lt;br /&gt;Behavior and Attributes&lt;br&gt;&lt;br /&gt;Every class you write in Java has two basic features: attributes and behavior. &lt;br /&gt;In this section you'll learn about each one as it applies to a theoretical &lt;br /&gt;simple class called Motorcycle. To finish up this section, you'll create the &lt;br /&gt;Java code to implement a representation of a motorcycle.&lt;br&gt;&lt;br /&gt;Attributes&lt;br&gt;&lt;br /&gt;Attributes are the individual things that differentiate one object from another &lt;br /&gt;and determine the appearance, state, or other qualities of that object. Let's &lt;br /&gt;create a theoretical class called Motorcycle. A motorcycle class might include &lt;br /&gt;the following attributes and have these typical values:&lt;br&gt;&lt;br /&gt;l Color: red, green, silver, brown&lt;br&gt;&lt;br /&gt;l Style: cruiser, sport bike, standard&lt;br&gt;&lt;br /&gt;l Make: Honda, BMW, Bultaco&lt;br&gt;&lt;br /&gt;Attributes of an object can also include information about its state; for &lt;br /&gt;example, you could have features for engine condition (off or on) or current &lt;br /&gt;gear selected.&lt;br&gt;&lt;br /&gt;Attributes are defined in classes by variables. Those variables' types and names &lt;br /&gt;are defined in the class, and each object can have its own values for those &lt;br /&gt;variables. Because each instance of a class can have different values for its &lt;br /&gt;variables, these variables are often called instance variables.&lt;br&gt;&lt;br /&gt;New Term&lt;br&gt;&lt;br /&gt;An instance variable defines the attributes of the object. Instance variables' &lt;br /&gt;types and names are defined in the class, but their values&lt;br&gt;&lt;br /&gt;are set and changed in the object. Instance variables may be initially set when &lt;br /&gt;an object is created and stay constant throughout the life of the object, or &lt;br /&gt;they may be able to change at will as the program runs. Change the value of the &lt;br /&gt;variable, and you&lt;br&gt;&lt;br /&gt;change an object's attributes. In addition to instance variables, there are also &lt;br /&gt;class variables, which apply to the class itself and to all its&lt;br&gt;&lt;br /&gt;instances. Unlike instance variables, whose values are stored in the instance, &lt;br /&gt;class variables' values are stored in the class itself. You'll learn about class &lt;br /&gt;variables later on this week and more specifics about instance variables &lt;br /&gt;tomorrow.&lt;br&gt;&lt;br /&gt;Behavior&lt;br&gt;&lt;br /&gt;A class's behavior determines how an instance of that class operates; for &lt;br /&gt;example, how it will &amp;quot;react&amp;quot; if asked to do something by another class or object &lt;br /&gt;or if its internal state changes. Behavior is the only way objects can do &lt;br /&gt;anything to themselves or have anything done to them. For example, to go back to &lt;br /&gt;the theoretical Motorcycle class, here are some behaviors that the Motorcycle &lt;br /&gt;class might have:&lt;br&gt;&lt;br /&gt;l Start the engine&lt;br&gt;&lt;br /&gt;l Stop the engine&lt;br&gt;&lt;br /&gt;l Speed up&lt;br&gt;&lt;br /&gt;l Change gear&lt;br&gt;&lt;br /&gt;l Stall&lt;br&gt;&lt;br /&gt;To define an object's behavior, you create methods, a set of Java statements &lt;br /&gt;that accomplish some task. Methods look and behave just like functions in other &lt;br /&gt;languages but are defined and accessible solely inside a class. Java does not &lt;br /&gt;have functions defined outside classes (as C++ does).&lt;br&gt;&lt;br /&gt;New Term&lt;br&gt;&lt;br /&gt;Methods are functions defined inside classes that operate on instances&lt;br&gt;&lt;br /&gt;of those classes.&lt;br&gt;&lt;br /&gt;While methods can be used solely to operate on an individual object, methods are &lt;br /&gt;also used between objects to communicate with each other. A class or an object &lt;br /&gt;can call methods in another class or object to communicate changes in the &lt;br /&gt;environment or to ask that object to change its state. Just as there are &lt;br /&gt;instance and class variables, there are also instance and class methods. &lt;br /&gt;Instance methods (which are so common that they're usually just called methods) &lt;br /&gt;apply and operate on an instance of a class; class methods apply and operate on &lt;br /&gt;the class itself. You'll learn more about class methods later on this week.&lt;br&gt;&lt;br /&gt;Creating a Class&lt;br&gt;&lt;br /&gt;Up to this point, today's lesson has been pretty theoretical. In this section, &lt;br /&gt;you'll create a working example of the Motorcycle class so that you can see how &lt;br /&gt;instance variables and methods are defined in a class in Java. You'll also &lt;br /&gt;create a Java application that creates a new instance of the Motorcycle class &lt;br /&gt;and shows its instance variables.&lt;br&gt;&lt;br /&gt;Note&lt;br&gt;&lt;br /&gt;I'm not going to go into a lot of detail about the actual syntax of this example &lt;br /&gt;here. Don't worry too much about it if you're not really sure &lt;br&gt;&lt;br /&gt;what's going on; it will become clear to you later on this week. All you really &lt;br /&gt;need to worry about in this example is understanding the&lt;br&gt;&lt;br /&gt;basic parts of this class definition. Ready? Let's start with a basic class &lt;br /&gt;definition. Open the text editor you've been using to create Java source code &lt;br /&gt;and enter the following (remember, upper- and lowercase matters):&lt;br&gt;&lt;br /&gt;class Motorcycle {&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;Congratulations! You've now created a class. Of course, it doesn't do very much &lt;br /&gt;at the moment, but that's a&lt;br&gt;&lt;br /&gt;Java class at its very simplest.&lt;br&gt;&lt;br /&gt;First, let's create some instance variables for this class-three of them, to be &lt;br /&gt;specific. Just below the first line,&lt;br&gt;&lt;br /&gt;add the following three lines:&lt;br&gt;&lt;br /&gt;String make;&lt;br&gt;&lt;br /&gt;String color;&lt;br&gt;&lt;br /&gt;boolean engineState = false;&lt;br&gt;&lt;br /&gt;Here you've created three instance variables: Two, make and color, can contain &lt;br /&gt;String objects (a string is the generic term for a series of characters; String, &lt;br /&gt;with a capital S, is part of that standard class library mentioned earlier). The &lt;br /&gt;third, engineState, is a boolean variable that refers to whether the engine is &lt;br /&gt;off or on; a value of false means that the engine is off, and true means that &lt;br /&gt;the engine is on. Note that boolean is lowercase b.&lt;br&gt;&lt;br /&gt;New Term&lt;br&gt;&lt;br /&gt;A boolean is a value of either true or false.&lt;br&gt;&lt;br /&gt;Technical Note&lt;br&gt;&lt;br /&gt;boolean in Java is a real data type that can have the values true or false. &lt;br /&gt;Unlike in C, booleans are not numbers. You'll hear about this again tomorrow so &lt;br /&gt;that you won't forget. Now let's add some behavior (methods) to the class. There &lt;br /&gt;are all kinds of things a motorcycle can do, but to keep things short, let's add &lt;br /&gt;just one method-a method that starts the engine. Add the following lines below &lt;br /&gt;the&lt;br&gt;&lt;br /&gt;instance variables in your class definition:&lt;br&gt;&lt;br /&gt;void startEngine() {&lt;br&gt;&lt;br /&gt;if (engineState == true)&lt;br&gt;&lt;br /&gt;System.out.println(&amp;quot;The engine is already on.&amp;quot;);&lt;br&gt;&lt;br /&gt;else {&lt;br&gt;&lt;br /&gt;engineState = true;&lt;br&gt;&lt;br /&gt;System.out.println(&amp;quot;The engine is now on.&amp;quot;);&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;The startEngine() method tests to see whether the engine is already running (in &lt;br /&gt;the part engineState == true) and, if it is, merely prints a message to that &lt;br /&gt;effect. If the engine isn't already running, it changes the state of the engine &lt;br /&gt;to true (turning the engine on) and then prints a message. Finally, because the &lt;br /&gt;startEngine() method doesn't return a value, its definition includes the word &lt;br /&gt;void at the beginning. (You can also define methods to return values; you'll &lt;br /&gt;learn more about method definitions on Day 6, &amp;quot;Creating Classes and Applications &lt;br /&gt;in Java.&amp;quot;)&lt;br&gt;&lt;br /&gt;Tip&lt;br&gt;&lt;br /&gt;Here and throughout this book, whenever I refer to the name of a method, I'll &lt;br /&gt;add empty parentheses to the end of the name (for&lt;br&gt;&lt;br /&gt;example, as I did in the first sentence of the previous paragraph: &amp;quot;The &lt;br /&gt;startEngine() method…&amp;quot; This is a convention used in the&lt;br&gt;&lt;br /&gt;programming community at large to indicate that a particular name is a method &lt;br /&gt;and not a variable. The parentheses are silent.&lt;br&gt;&lt;br /&gt;With your methods and variables in place, save the program to a file called &lt;br /&gt;Motorcycle.java (remember that you should always name your Java source files the &lt;br /&gt;same names as the class they define). Listing 2.1 shows what your program should &lt;br /&gt;look like so far.&lt;br&gt;&lt;br /&gt;Listing 2.1. The Motorcycle.java file.&lt;br&gt;&lt;br /&gt;1:class Motorcycle {&lt;br&gt;&lt;br /&gt;2:&lt;br&gt;&lt;br /&gt;3: String make;&lt;br&gt;&lt;br /&gt;4: String color;&lt;br&gt;&lt;br /&gt;5: boolean engineState = false;&lt;br&gt;&lt;br /&gt;6:&lt;br&gt;&lt;br /&gt;7: void startEngine() {&lt;br&gt;&lt;br /&gt;8: if (engineState == true)&lt;br&gt;&lt;br /&gt;9: System.out.println(&amp;quot;The engine is already on.&amp;quot;);&lt;br&gt;&lt;br /&gt;10: else {&lt;br&gt;&lt;br /&gt;11: engineState = true;&lt;br&gt;&lt;br /&gt;12: System.out.println(&amp;quot;The engine is now on.&amp;quot;);&lt;br&gt;&lt;br /&gt;13: }&lt;br&gt;&lt;br /&gt;14: }&lt;br&gt;&lt;br /&gt;15:}&lt;br&gt;&lt;br /&gt;Tip&lt;br&gt;&lt;br /&gt;The indentation of each part of the class isn't important to the Java compiler. &lt;br /&gt;Using some form of indentation, however, makes your lass definition easier for &lt;br /&gt;you and other people to read. The indentation used here, with instance variables &lt;br /&gt;and methods indented from the lass definition, is the style used throughout this &lt;br /&gt;book. The Java class libraries use a similar indentation. You can choose any &lt;br /&gt;indentation style hat you like.&lt;br&gt;&lt;br /&gt;Before you compile this class, let's add one more method just below the &lt;br /&gt;startEngine() method (that is, between lines 14 and 15). The showAtts() method &lt;br /&gt;is used to print the current values of all the instance variables in an instance &lt;br /&gt;of your Motorcycle class. Here's what it looks like:&lt;br&gt;&lt;br /&gt;void showAtts() {&lt;br&gt;&lt;br /&gt;System.out.println(&amp;quot;This motorcycle is a &amp;quot;&lt;br&gt;&lt;br /&gt;+ color + &amp;quot; &amp;quot; + make);&lt;br&gt;&lt;br /&gt;if (engineState == true)&lt;br&gt;&lt;br /&gt;System.out.println(&amp;quot;The engine is on.&amp;quot;);&lt;br&gt;&lt;br /&gt;else System.out.println(&amp;quot;The engine is off.&amp;quot;);&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;The showAtts() method prints two lines to the screen: the make and color of the &lt;br /&gt;motorcycle object and whether the engine is on or off. Now you have a Java class &lt;br /&gt;with three instance variables and two methods defined. Save that file again, and &lt;br /&gt;compile it using one of the following methods:&lt;br&gt;&lt;br /&gt;Note&lt;br&gt;&lt;br /&gt;After this point, I'm going to assume you know how to compile and run Java &lt;br /&gt;programs. I won't repeat this information after this.&lt;br&gt;&lt;br /&gt;Windows&lt;br&gt;&lt;br /&gt;From inside a DOS shell, CD to the directory containing your Java source file, &lt;br /&gt;and use the javac command to compile it:&lt;br&gt;&lt;br /&gt;javac Motorcycle.java&lt;br&gt;&lt;br /&gt;Macintosh&lt;br&gt;&lt;br /&gt;Drag and drop the Motorcycle.java file onto the Java Compiler icon.&lt;br&gt;&lt;br /&gt;Salaris&lt;br&gt;&lt;br /&gt;From a command line, CD to the directory containing your Java source file, and &lt;br /&gt;use the javac command to compile it:&lt;br&gt;&lt;br /&gt;javac Motorcycle.java&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;When you run this little program using the java or Java Runner programs, you'll &lt;br /&gt;get an error. Why? When you run a compiled Java class directly, Java assumes &lt;br /&gt;that the class is an application and looks for a main()&lt;br&gt;&lt;br /&gt;method. Because we haven't defined a main() method inside the class, the Java &lt;br /&gt;interpreter (java) gives you&lt;br&gt;&lt;br /&gt;an error something like one of these two errors:&lt;br&gt;&lt;br /&gt;In class Motorcycle: void main(String argv[]) is not defined&lt;br&gt;&lt;br /&gt;Exception in thread &amp;quot;main&amp;quot;: java.lang.UnknownError&lt;br&gt;&lt;br /&gt;To do something with the Motorcycle class-for example, to create instances of &lt;br /&gt;that class and play with them-you're going to need to create a separate Java &lt;br /&gt;applet or application that uses this class or add a main()method to this one. &lt;br /&gt;For simplicity's sake, let's do the latter. Listing 2.2 shows the main() method &lt;br /&gt;you'll add to the Motorcycle class. You'll want to add this method to your &lt;br /&gt;Motorcycle.java source file just before the last closing brace (}), underneath &lt;br /&gt;the startEngine() and showAtts() methods.&lt;br&gt;&lt;br /&gt;Listing 2.2. The main() method for Motorcycle.java.&lt;br&gt;&lt;br /&gt;1: public static void main (String args[]) {&lt;br&gt;&lt;br /&gt;2: Motorcycle m = new Motorcycle();&lt;br&gt;&lt;br /&gt;3: m.make = &amp;quot;Yamaha RZ350&amp;quot;;&lt;br&gt;&lt;br /&gt;4: m.color = &amp;quot;yellow&amp;quot;;&lt;br&gt;&lt;br /&gt;5: System.out.println(&amp;quot;Calling showAtts...&amp;quot;);&lt;br&gt;&lt;br /&gt;6: m.showAtts();&lt;br&gt;&lt;br /&gt;7: System.out.println(&amp;quot;--------&amp;quot;);&lt;br&gt;&lt;br /&gt;8: System.out.println(&amp;quot;Starting engine...&amp;quot;);&lt;br&gt;&lt;br /&gt;9: m.startEngine();&lt;br&gt;&lt;br /&gt;10: System.out.println(&amp;quot;--------&amp;quot;);&lt;br&gt;&lt;br /&gt;11: System.out.println(&amp;quot;Calling showAtts...&amp;quot;);&lt;br&gt;&lt;br /&gt;12: m.showAtts();&lt;br&gt;&lt;br /&gt;13: System.out.println(&amp;quot;--------&amp;quot;);&lt;br&gt;&lt;br /&gt;14: System.out.println(&amp;quot;Starting engine...&amp;quot;);&lt;br&gt;&lt;br /&gt;15: m.startEngine();&lt;br&gt;&lt;br /&gt;16:}&lt;br&gt;&lt;br /&gt;With the main() method in place, the Motorcycle class is now an official &lt;br /&gt;application, and you can&lt;br&gt;&lt;br /&gt;compile it again and this time it'll run. Here's how the output should look:&lt;br&gt;&lt;br /&gt;Calling showAtts...&lt;br&gt;&lt;br /&gt;This motorcycle is a yellow Yamaha RZ350&lt;br&gt;&lt;br /&gt;The engine is off.&lt;br&gt;&lt;br /&gt;--------&lt;br&gt;&lt;br /&gt;Starting engine...&lt;br&gt;&lt;br /&gt;The engine is now on.&lt;br&gt;&lt;br /&gt;--------&lt;br&gt;&lt;br /&gt;Calling showAtts...&lt;br&gt;&lt;br /&gt;This motorcycle is a yellow Yamaha RZ350&lt;br&gt;&lt;br /&gt;The engine is on.&lt;br&gt;&lt;br /&gt;--------&lt;br&gt;&lt;br /&gt;Starting engine...&lt;br&gt;&lt;br /&gt;The engine is already on.&lt;br&gt;&lt;br /&gt;Analysis&lt;br&gt;&lt;br /&gt;The contents of the main() method are all going to look very new to you, so &lt;br /&gt;let's go through it line by line so that you at least have a basic idea of what &lt;br /&gt;it does (you'll get details about the specifics of all of this tomorrow and the &lt;br /&gt;day after).&lt;br&gt;&lt;br /&gt;The first line declares the main() method. The first line of the main() method &lt;br /&gt;always looks like this; you'll learn the specifics of each part later this week.&lt;br&gt;&lt;br /&gt;Line 2, Motorcycle m = new Motorcycle();, creates a new instance of the &lt;br /&gt;Motorcycle class and stores a reference to it in the variable m. Remember, you &lt;br /&gt;don't usually operate directly on classes in your Java programs; instead, you &lt;br /&gt;create objects from those classes and then call methods in those objects. Lines &lt;br /&gt;3 and 4 set the instance variables for this Motorcycle object: The make is now a &lt;br /&gt;Yamaha RZ350 (a very pretty motorcycle from the mid-1980s), and the color is &lt;br /&gt;yellow.&lt;br&gt;&lt;br /&gt;Lines 5 and 6 call the showAtts() method, defined in your Motorcycle object. &lt;br /&gt;(Actually, only 6 does; 5 just prints a message that you're about to call this &lt;br /&gt;method.) The new motorcycle object then prints out the values of its instance &lt;br /&gt;variables-the make and color as you set in the previous lines-and shows that the &lt;br /&gt;engine is off.&lt;br&gt;&lt;br /&gt;Line 7 prints a divider line to the screen; this is just for prettier output.&lt;br&gt;&lt;br /&gt;Line 9 calls the startEngine() method in the motorcycle object to start the &lt;br /&gt;engine. The engine should now be on.&lt;br&gt;&lt;br /&gt;Line 11 prints the values of the instance variables again. This time, the report &lt;br /&gt;should say the engine is now on. &lt;br&gt;&lt;br /&gt;Line 15 tries to start the engine again, just for fun. Because the engine is &lt;br /&gt;already on, this should print the message The engine is already on.&lt;br&gt;&lt;br /&gt;Listing 2.3 shows the final Motorcycle class, in case you've been having trouble &lt;br /&gt;compiling and running the one you've got (and remember, this example and all the &lt;br /&gt;examples in this book are available on the CD that accompanies the book):&lt;br&gt;&lt;br /&gt;Listing 2.3. The final version of Motorcycle.java.&lt;br&gt;&lt;br /&gt;1: class Motorcycle {&lt;br&gt;&lt;br /&gt;2:&lt;br&gt;&lt;br /&gt;3: String make;&lt;br&gt;&lt;br /&gt;4: String color;&lt;br&gt;&lt;br /&gt;5: boolean engineState;&lt;br&gt;&lt;br /&gt;6:&lt;br&gt;&lt;br /&gt;7: void startEngine() {&lt;br&gt;&lt;br /&gt;8: if (engineState == true)&lt;br&gt;&lt;br /&gt;9: System.out.println(&amp;quot;The engine is already on.&amp;quot;);&lt;br&gt;&lt;br /&gt;10: else {&lt;br&gt;&lt;br /&gt;11: engineState = true;&lt;br&gt;&lt;br /&gt;12: System.out.println(&amp;quot;The engine is now on.&amp;quot;);&lt;br&gt;&lt;br /&gt;13: }&lt;br&gt;&lt;br /&gt;14: }&lt;br&gt;&lt;br /&gt;15:&lt;br&gt;&lt;br /&gt;16: void showAtts() {&lt;br&gt;&lt;br /&gt;17: System.out.println(&amp;quot;This motorcycle is a &amp;quot;&lt;br&gt;&lt;br /&gt;18: + color + &amp;quot; &amp;quot; + make);&lt;br&gt;&lt;br /&gt;19: if (engineState == true)&lt;br&gt;&lt;br /&gt;20: System.out.println(&amp;quot;The engine is on.&amp;quot;);&lt;br&gt;&lt;br /&gt;21: else System.out.println(&amp;quot;The engine is off.&amp;quot;);&lt;br&gt;&lt;br /&gt;22: }&lt;br&gt;&lt;br /&gt;23:&lt;br&gt;&lt;br /&gt;24: public static void main (String args[]) {&lt;br&gt;&lt;br /&gt;25: Motorcycle m = new Motorcycle();&lt;br&gt;&lt;br /&gt;26: m.make = &amp;quot;Yamaha RZ350&amp;quot;;&lt;br&gt;&lt;br /&gt;27: m.color = &amp;quot;yellow&amp;quot;;&lt;br&gt;&lt;br /&gt;28: System.out.println(&amp;quot;Calling showAtts...&amp;quot;);&lt;br&gt;&lt;br /&gt;29: m.showAtts();&lt;br&gt;&lt;br /&gt;30: System.out.println(&amp;quot;------&amp;quot;);&lt;br&gt;&lt;br /&gt;31: System.out.println(&amp;quot;Starting engine...&amp;quot;);&lt;br&gt;&lt;br /&gt;32: m.startEngine();&lt;br&gt;&lt;br /&gt;33: System.out.println(&amp;quot;------&amp;quot;);&lt;br&gt;&lt;br /&gt;34: System.out.println(&amp;quot;Calling showAtts...&amp;quot;);&lt;br&gt;&lt;br /&gt;35: m.showAtts();&lt;br&gt;&lt;br /&gt;36: System.out.println(&amp;quot;------&amp;quot;);&lt;br&gt;&lt;br /&gt;37: System.out.println(&amp;quot;Starting engine...&amp;quot;);&lt;br&gt;&lt;br /&gt;38: m.startEngine();&lt;br&gt;&lt;br /&gt;39: }&lt;br&gt;&lt;br /&gt;40:}&lt;br&gt;&lt;br /&gt;Inheritance, Interfaces, and Packages&lt;br&gt;&lt;br /&gt;Now that you have a basic grasp of classes, objects, methods, variables, and how &lt;br /&gt;to put them all together in a Java program, it's time to confuse you again. &lt;br /&gt;Inheritance, interfaces, and packages are all mechanisms for organizing classes &lt;br /&gt;and class behaviors. The Java class libraries use all these concepts, and the &lt;br /&gt;best class libraries you write for your own programs will also use these &lt;br /&gt;concepts.&lt;br&gt;&lt;br /&gt;Inheritance&lt;br&gt;&lt;br /&gt;Inheritance is one of the most crucial concepts in object-oriented programming, &lt;br /&gt;and it has a very direct effect on how you design and write your Java classes. &lt;br /&gt;Inheritance is a powerful mechanism that means when you write a class you only &lt;br /&gt;have to specify how that class is different from some other class; inheritance &lt;br /&gt;will give you automatic access to the information contained in that other class.&lt;br&gt;&lt;br /&gt;With inheritance, all classes-those you write, those from other class libraries &lt;br /&gt;that you use, and those from the standard utility classes as well-are arranged &lt;br /&gt;in a strict hierarchy (see Figure 2.2). Each class has a superclass (the class &lt;br /&gt;above it in the hierarchy), and each class can have one or more subclasses &lt;br /&gt;(classes below that class in the hierarchy). Classes further down in the &lt;br /&gt;hierarchy are said to inherit from classes further up in the hierarchy.&lt;br&gt;&lt;br /&gt;Figure 2.2 : A class hierarchy.&lt;br&gt;&lt;br /&gt;Subclasses inherit all the methods and variables from their superclasses-that &lt;br /&gt;is, in any particular class, if the superclass defines behavior that your class &lt;br /&gt;needs, you don't have to redefine it or copy that code from some other class. &lt;br /&gt;Your class automatically gets that behavior from its superclass, that superclass &lt;br /&gt;gets behavior from its superclass, and so on all the way up the hierarchy. Your &lt;br /&gt;class becomes a combination of all the features of the classes above it in the &lt;br /&gt;hierarchy.&lt;br&gt;&lt;br /&gt;New Term&lt;br&gt;&lt;br /&gt;Inheritance is a concept in object-oriented programming where all classes are &lt;br /&gt;arranged in a strict hierarchy. Each class in the hierarchy&lt;br&gt;&lt;br /&gt;has superclasses (classes above it in the hierarchy) and any number of &lt;br /&gt;subclasses (classes below it in the hierarchy). Subclasses inherit&lt;br&gt;&lt;br /&gt;attributes and behavior from their superclasses.&lt;br&gt;&lt;br /&gt;At the top of the Java class hierarchy is the class Object; all classes inherit &lt;br /&gt;from this one superclass. Object is the most general class in the hierarchy; it &lt;br /&gt;defines behavior inherited by all the classes in Java. Each class further down &lt;br /&gt;in the hierarchy adds more information and becomes more tailored to a specific &lt;br /&gt;purpose. In this way, you can think of a class hierarchy as defining very &lt;br /&gt;abstract concepts at the top of the hierarchy and those ideas becoming more &lt;br /&gt;concrete the farther down the chain of superclasses you go. Most of the time &lt;br /&gt;when you write new Java classes, you'll want to create a class that has all the &lt;br /&gt;information some other class has, plus some extra information. For example, you &lt;br /&gt;may want a version of a Button with its own built-in label. To get all the &lt;br /&gt;Button information, all you have to do is define your class to inherit from &lt;br /&gt;Button. Your class will automatically get all the behavior defined in Button &lt;br /&gt;(and in Button's superclasses), so all you have to worry about are the things &lt;br /&gt;that make your class different from Button itself.&lt;br&gt;&lt;br /&gt;This mechanism for defining new classes as the differences between them and &lt;br /&gt;their superclasses is called subclassing.&lt;br&gt;&lt;br /&gt;Subclassing involves creating a new class that inherits from some other class in &lt;br /&gt;the class hierarchy. Using subclassing, you only need to define the differences &lt;br /&gt;between your class and its parent; the additional behavior is all available to &lt;br /&gt;your class through inheritance.&lt;br&gt;&lt;br /&gt;New Term&lt;br&gt;&lt;br /&gt;Subclassing is the process of creating a new class that inherits from some other &lt;br /&gt;already-existing class.&lt;br&gt;&lt;br /&gt;What if your class defines an entirely new behavior and isn't really a subclass &lt;br /&gt;of another class? Your class can also inherit directly from Object, which still &lt;br /&gt;allows it to fit neatly into the Java class hierarchy. In fact, if you create a &lt;br /&gt;class definition that doesn't indicate its superclass in the first line, Java &lt;br /&gt;automatically assumes you're inheriting from Object. The Motorcycle class you &lt;br /&gt;created in the previous section inherited from&lt;br&gt;&lt;br /&gt;Object.&lt;br&gt;&lt;br /&gt;Creating a Class Hierarchy&lt;br&gt;&lt;br /&gt;If you're creating a larger set of classes for a very complex program, it makes &lt;br /&gt;sense for your classes not only to inherit from the existing class hierarchy, &lt;br /&gt;but also to make up a hierarchy themselves. This may take some planning &lt;br /&gt;beforehand when you're trying to figure out how to organize your Java code, but &lt;br /&gt;the advantages are significant once it's done:&lt;br&gt;&lt;br /&gt;When you develop your classes in a hierarchy, you can factor out information &lt;br /&gt;common to multiple classes in superclasses, and then reuse that superclass's &lt;br /&gt;information over and over again. Each subclass gets that common information from &lt;br /&gt;its superclass.&lt;br&gt;&lt;br /&gt;l &lt;br&gt;&lt;br /&gt;Changing (or inserting) a class further up in the hierarchy automatically &lt;br /&gt;changes the behavior of its subclasses-no need to change or recompile any of the &lt;br /&gt;lower classes because they get the new information through inheritance and not &lt;br /&gt;by copying any of the code.&lt;br&gt;&lt;br /&gt;l &lt;br&gt;&lt;br /&gt;For example, let's go back to that Motorcycle class and pre tend you created a &lt;br /&gt;Java program to implement all the features of a motorcycle. It's done, it works, &lt;br /&gt;and everything is fine. Now, your next task is to create a Java class called &lt;br /&gt;Car.&lt;br&gt;&lt;br /&gt;Car and Motorcycle have many similar features-both are vehicles driven by &lt;br /&gt;engines. Both have transmissions, headlamps, and speedometers. So your first &lt;br /&gt;impulse may be to open your Motorcycle class file and copy over a lot of the &lt;br /&gt;information you already defined into the new class Car.&lt;br&gt;&lt;br /&gt;A far better plan is to factor out the common information for Car and Motorcycle &lt;br /&gt;into a more general class hierarchy. This may be a lot of work just for the &lt;br /&gt;classes Motorcycle and Car, but once you add Bicycle, Scooter, Truck, and so on, &lt;br /&gt;having common behavior in a reusable superclass significantly reduces the amount &lt;br /&gt;of work you have to do overall.&lt;br&gt;&lt;br /&gt;Let's design a class hierarchy that might serve this purpose. Starting at the &lt;br /&gt;top is the class Object, which is the root of all Java classes. The most general &lt;br /&gt;class to which a motorcycle and a car both belong might be called Vehicle. A &lt;br /&gt;vehicle, generally, is defined as a thing that propels someone from one place to &lt;br /&gt;another. In the Vehicle class, you define only the behavior that enables someone &lt;br /&gt;to be propelled from point a to point b, and nothing more.&lt;br&gt;&lt;br /&gt;Below Vehicle? How about two classes: PersonPoweredVehicle and &lt;br /&gt;EnginePoweredVehicle? EnginePoweredVehicle is different from Vehicle because it &lt;br /&gt;has an engine, and the behaviors might include stopping and starting the engine, &lt;br /&gt;having certain amounts of gasoline and oil, and perhaps the speed or gear in &lt;br /&gt;which the engine is running. Person-powered vehicles have some kind of mechanism &lt;br /&gt;for translating people motion into vehicle motion-pedals, for example. Figure &lt;br /&gt;2.3 shows what you have so far.&lt;br&gt;&lt;br /&gt;Figure 2.3 : The basic vehicle hierarchy.&lt;br&gt;&lt;br /&gt;Now let's become even more specific. With EnginePoweredVehicle, you might have &lt;br /&gt;several classes:&lt;br&gt;&lt;br /&gt;Motorcycle, Car, Truck, and so on. Or you can factor out still more behavior and &lt;br /&gt;have intermediate&lt;br&gt;&lt;br /&gt;classes for TwoWheeled and FourWheeled vehicles, with different behaviors for &lt;br /&gt;each (see Figure 2.4).&lt;br&gt;&lt;br /&gt;Figure 2.4 : Two-wheeled and four-wheeled vehicles.&lt;br&gt;&lt;br /&gt;Finally, with a subclass for the two-wheeled engine-powered vehicles, you can &lt;br /&gt;have a class for motorcycles. Alternatively, you could additionally define &lt;br /&gt;scooters and mopeds, both of which are two-wheeled engine-powered vehicles but &lt;br /&gt;have different qualities from motorcycles. Where do qualities such as make or &lt;br /&gt;color come in? Wherever you want them to go-or, more usually, where they fit &lt;br /&gt;most naturally in the class hierarchy. You can define the make and color on &lt;br /&gt;Vehicle, and all the subclasses will have those variables as well. The point to &lt;br /&gt;remember is that you have to define a feature or a behavior only once in the &lt;br /&gt;hierarchy; it's automatically reused by each subclass.&lt;br&gt;&lt;br /&gt;How Inheritance Works&lt;br&gt;&lt;br /&gt;How does inheritance work? How is it that instances of one class can &lt;br /&gt;automatically get variables and methods from the classes further up in the &lt;br /&gt;hierarchy?&lt;br&gt;&lt;br /&gt;For instance variables, when you create a new instance of a class, you get a &lt;br /&gt;&amp;quot;slot&amp;quot; for each variable defined in the current class and for each variable &lt;br /&gt;defined in all its superclasses. In this way, all the classes combine to form a &lt;br /&gt;template for the current object, and then each object fills in the information &lt;br /&gt;appropriate to its situation. Methods operate similarly: New objects have access &lt;br /&gt;to all the method names of its class and its superclasses, but method &lt;br /&gt;definitions are chosen dynamically when a method is called. That is, if you call &lt;br /&gt;a method on a particular object, Java first checks the object's class for the &lt;br /&gt;definition of that method. If it's not defined in the object's class, it looks &lt;br /&gt;in that class's superclass, and so on up the chain until the method definition &lt;br /&gt;is found (see Figure 2.5).&lt;br&gt;&lt;br /&gt;Figure 2.5 : How methods are located.&lt;br&gt;&lt;br /&gt;Things get complicated when a subclass defines a method that has the same &lt;br /&gt;signature (name, number, and type of arguments) as a method defined in a &lt;br /&gt;superclass. In this case, the method definition that is found first (starting at &lt;br /&gt;the bottom and working upward toward the top of the hierarchy) is the one that &lt;br /&gt;is actually executed. Therefore, you can intentionally define a method in a &lt;br /&gt;subclass that has the same signature as a method in a superclass, which then &lt;br /&gt;&amp;quot;hides&amp;quot; the superclass's method. This is called overriding a method. You'll &lt;br /&gt;learn all about methods on Day 7, &amp;quot;More About Methods.&amp;quot;&lt;br&gt;&lt;br /&gt;New Term&lt;br&gt;&lt;br /&gt;Overriding a method is creating a method in a subclass that has the same &lt;br /&gt;signature (name, number, and type of arguments) as a method in a superclass. &lt;br /&gt;That new method then hides the superclass's method&lt;br&gt;&lt;br /&gt;(see Figure 2.6).&lt;br&gt;&lt;br /&gt;Figure 2.6 : Overriding methods.&lt;br&gt;&lt;br /&gt;Single and Multiple Inheritance&lt;br&gt;&lt;br /&gt;Java's form of inheritance, as you learned in the previous sections, is called &lt;br /&gt;single inheritance. Single inheritance means that each Java class can have only &lt;br /&gt;one superclass (although any given superclass can have multiple subclasses).&lt;br&gt;&lt;br /&gt;In other object-oriented programming languages, such as C++, classes can have &lt;br /&gt;more than one superclass, and they inherit combined variables and methods from &lt;br /&gt;all those classes. This is called multiple inheritance. Multiple inheritance can &lt;br /&gt;provide enormous power in terms of being able to create classes that factor just &lt;br /&gt;about all imaginable behavior, but it can also significantly complicate class &lt;br /&gt;definitions and the code to produce them.&lt;br&gt;&lt;br /&gt;Java makes inheritance simpler by being only singly inherited.&lt;br&gt;&lt;br /&gt;Interfaces and Packages&lt;br&gt;&lt;br /&gt;There are two remaining concepts to discuss here: packages and interfaces. Both &lt;br /&gt;are advanced topics for implementing and designing groups of classes and class &lt;br /&gt;behavior. You'll learn about both interfaces and packages on Day 16, &amp;quot;Packages &lt;br /&gt;and Interfaces,&amp;quot; but they are worth at least introducing here. &lt;br&gt;&lt;br /&gt;Recall that each Java class has only a single superclass, and it inherits &lt;br /&gt;variables and methods from that superclass and all its superclasses. Although &lt;br /&gt;single inheritance makes the relationship between classes and the functionality &lt;br /&gt;those classes implement easy to understand and to design, it can also be &lt;br /&gt;somewhat restrictive-in particular, when you have similar behavior that needs to &lt;br /&gt;be duplicated across different &amp;quot;branches&amp;quot; of the class hierarchy. Java solves &lt;br /&gt;this problem of shared behavior by using the concept of interfaces, which &lt;br /&gt;collect &lt;br&gt;&lt;br /&gt;method names into one place and then allow you to add those methods as a group &lt;br /&gt;to the various classes that need them. Note that interfaces contain only method &lt;br /&gt;names and interfaces (arguments, for example), not actual definitions.&lt;br&gt;&lt;br /&gt;Although a single Java class can have only one superclass (due to single &lt;br /&gt;inheritance), that class can also implement any number of interfaces. By &lt;br /&gt;implementing an interface, a class provides method implementations (definitions) &lt;br /&gt;for the method names defined by the interface. If two very disparate classes &lt;br /&gt;implement the same interface, they can both respond to the same method calls (as &lt;br /&gt;defined by that interface), although what each class actually does in response &lt;br /&gt;to those method calls may be very different.&lt;br&gt;&lt;br /&gt;New Term&lt;br&gt;&lt;br /&gt;An interface is a collection of method names, without definitions, that can be &lt;br /&gt;added to classes to provide additional behavior not included&lt;br&gt;&lt;br /&gt;with those methods the class defined itself or inherited from its superclasses. &lt;br /&gt;You don't need to know very much about interfaces right now. You'll learn more &lt;br /&gt;as the book progresses, so if all this is very confusing, don't panic! The final &lt;br /&gt;new Java concept for today is packages. Packages in Java are a way of grouping &lt;br /&gt;together related classes and interfaces in a single library or collection. &lt;br /&gt;Packages enable modular groups of classes to be available only if they are &lt;br /&gt;needed and eliminate potential conflicts between class names in different groups &lt;br /&gt;of classes.&lt;br&gt;&lt;br /&gt;You'll learn all about packages, including how to create and use them, in Week &lt;br /&gt;3. For now, there are only a&lt;br&gt;&lt;br /&gt;few things you need to know:&lt;br&gt;&lt;br /&gt;l The class libraries in the Java Developer's Kit are contained in a package &lt;br /&gt;called java. The classes in the java package are guaranteed to be available in &lt;br /&gt;any Java implementation and are the only classes guaranteed to be available &lt;br /&gt;across different implementations. The java package itself contains other &lt;br /&gt;packages for classes that define the language, the input and output classes, &lt;br /&gt;some basic networking, the&lt;br&gt;&lt;br /&gt;window toolkit functions, and classes that define applets. Classes in other &lt;br /&gt;packages (for example, classes in the sun or netscape packages) may be available &lt;br /&gt;only in specific implementations. &lt;br&gt;&lt;br /&gt;By default, your Java classes have access to only the classes in java.lang (the &lt;br /&gt;base language package inside the java package). To use classes from any other &lt;br /&gt;package, you have to either refer to them explicitly by package name or import &lt;br /&gt;them into your source file.&lt;br&gt;&lt;br /&gt;l &lt;br&gt;&lt;br /&gt;To refer to a class within a package, list all the packages that class is &lt;br /&gt;contained in and the class name, all separated by periods (.). For example, take &lt;br /&gt;the Color class, which is contained in the awt package (awt stands for Abstract &lt;br /&gt;Windowing Toolkit). The awt package, in turn, is inside the java package. To &lt;br /&gt;refer to the Color class in your program, you use the notation java.awt.Color.&lt;br&gt;&lt;br /&gt;l &lt;br&gt;&lt;br /&gt;Creating a Subclass&lt;br&gt;&lt;br /&gt;To finish up today, let's create a class that is a subclass of another class and &lt;br /&gt;override some methods. You'll also get a basic feel for how packages work in &lt;br /&gt;this example.&lt;br&gt;&lt;br /&gt;Probably the most typical instance of creating a subclass, at least when you &lt;br /&gt;first start programming in Java, is creating an applet. All applets are &lt;br /&gt;subclasses of the class Applet (which is part of the java.applet package). By &lt;br /&gt;creating a subclass of Applet, you automatically get all the behavior from the &lt;br /&gt;window toolkit and the layout classes that enable your applet to be drawn in the &lt;br /&gt;right place on the page and to interact with system operations, such as &lt;br /&gt;keypresses and mouse clicks. In this example, you'll create an applet similar to &lt;br /&gt;the Hello World applet from yesterday, but one that draws the Hello string in a &lt;br /&gt;larger font and a different color. To start this example, let's first construct &lt;br /&gt;the class definition itself. Let's go to your text editor, and enter the &lt;br /&gt;following class definition:&lt;br&gt;&lt;br /&gt;public class HelloAgainApplet extends java.applet.Applet {&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;Here, you're creating a class called HelloAgainApplet. Note the part that says &lt;br /&gt;extends java.applet.Applet-that's the part that says your applet class is a &lt;br /&gt;subclass of the Applet class. Note that because the Applet class is contained in &lt;br /&gt;the java.applet package, you don't have automatic access to that class, and you &lt;br /&gt;have to refer to it explicitly by package and class name.&lt;br&gt;&lt;br /&gt;The other part of this class definition is the public keyword. Public means that &lt;br /&gt;your class is available to the Java system at large once it is loaded. Most of &lt;br /&gt;the time you need to make a class public only if you want it to be visible to &lt;br /&gt;all the other classes in your Java program, but applets, in particular, must be &lt;br /&gt;declared to be public. (You'll learn more about public classes in Week 3.)&lt;br&gt;&lt;br /&gt;A class definition with nothing in it doesn't really have much of a point; &lt;br /&gt;without adding or overriding any of its superclasses' variables or methods, &lt;br /&gt;there's no reason to create a subclass at all. Let's add some information to &lt;br /&gt;this class, inside the two enclosing braces, to make it different from its &lt;br /&gt;superclass. &lt;br&gt;&lt;br /&gt;First, add an instance variable to contain a Font object:&lt;br&gt;&lt;br /&gt;Font f = new Font(&amp;quot;TimesRoman&amp;quot;, Font.BOLD, 36);&lt;br&gt;&lt;br /&gt;The f instance variable now contains a new instance of the class Font, part of &lt;br /&gt;the java.awt package. This particular Font object is a Times Roman font, &lt;br /&gt;boldface, 36 points high. In the previous Hello World applet, the font used for &lt;br /&gt;the text was the default font: 12-point Times Roman. Using a Font object, you &lt;br /&gt;can change the font of the text you draw in your applet.&lt;br&gt;&lt;br /&gt;By creating an instance variable to hold this font object, you make it available &lt;br /&gt;to all the methods in your class. Now let's create a method that uses it.&lt;br&gt;&lt;br /&gt;When you write applets, there are several &amp;quot;standard&amp;quot; methods defined in the &lt;br /&gt;applet superclasses that you will commonly override in your applet class. These &lt;br /&gt;include methods to initialize the applet, to make it start running, to handle &lt;br /&gt;operations such as mouse movements or mouse clicks, or to clean up when the &lt;br /&gt;applet stops running. One of those standard methods is the paint() method, which &lt;br /&gt;actually displays your applet onscreen. The default definition of paint() &lt;br /&gt;doesn't do anything-it's an empty method. By overriding paint(), you tell the &lt;br /&gt;applet just what to draw on the screen. Here's a definition of paint():&lt;br&gt;&lt;br /&gt;public void paint(Graphics g) {&lt;br&gt;&lt;br /&gt;g.setFont(f);&lt;br&gt;&lt;br /&gt;g.setColor(Color.red);&lt;br&gt;&lt;br /&gt;g.drawString(&amp;quot;Hello again!&amp;quot;, 5, 40);&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;There are two things to know about the paint() method. First, note that this &lt;br /&gt;method is declared public, just as the applet itself was. The paint() method is &lt;br /&gt;actually public for a different reason-because the method it's overriding is &lt;br /&gt;also public. If a superclass's method is defined as public, your override method &lt;br /&gt;also has to be public, or you'll get an error when you compile the class.&lt;br&gt;&lt;br /&gt;Second, note that the paint() method takes a single argument: an instance of the &lt;br /&gt;Graphics class. The Graphics class provides platform-independent behavior for &lt;br /&gt;rendering fonts, colors, and behavior for drawing basic lines and shapes. You'll &lt;br /&gt;learn a lot more about the Graphics class in Week 2, when you create more &lt;br /&gt;extensive applets.&lt;br&gt;&lt;br /&gt;Inside your paint() method, you've done three things:&lt;br&gt;&lt;br /&gt;You've told the graphics object that the default drawing font will be the one &lt;br /&gt;contained in the instance&lt;br&gt;&lt;br /&gt;variable f.&lt;br&gt;&lt;br /&gt;l &lt;br&gt;&lt;br /&gt;l You've told the graphics object that the default color is an instance of the &lt;br /&gt;Color class for the color red. Finally, you've drawn your &amp;quot;Hello Again!&amp;quot; string &lt;br /&gt;onto the screen, at the x and y positions of 5 and 25. The string will be &lt;br /&gt;rendered in the new font and color.&lt;br&gt;&lt;br /&gt;l &lt;br&gt;&lt;br /&gt;For an applet this simple, this is all you need to do. Here's what the applet &lt;br /&gt;looks like so far:&lt;br&gt;&lt;br /&gt;public class HelloAgainApplet extends java.applet.Applet {&lt;br&gt;&lt;br /&gt;Font f = new Font(&amp;quot;TimesRoman&amp;quot;,Font.BOLD,36);&lt;br&gt;&lt;br /&gt;public void paint(Graphics g) {&lt;br&gt;&lt;br /&gt;g.setFont(f);&lt;br&gt;&lt;br /&gt;g.setColor(Color.red);&lt;br&gt;&lt;br /&gt;g.drawString(&amp;quot;Hello again!&amp;quot;, 5, 40);&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;If you've been paying close attention, you'll notice that something is wrong &lt;br /&gt;with this example up to this point. If you don't know what it is, try saving &lt;br /&gt;this file (remember, save it to the same name as the class: &lt;br /&gt;HelloAgainApplet.java) and compiling it. You should get a bunch of errors &lt;br /&gt;similar to this one:&lt;br&gt;&lt;br /&gt;HelloAgainApplet.java:7: Class Graphics not found in type declaration.&lt;br&gt;&lt;br /&gt;Why are you getting these errors? Because the classes you're referring to in &lt;br /&gt;this class, such as Graphics and Font, are part of a package that isn't &lt;br /&gt;available by default. Remember that the only package you have access to &lt;br /&gt;automatically in your Java programs is java.lang. You referred to the Applet &lt;br /&gt;class in the first line of the class definition by referring to its full package &lt;br /&gt;name (java.applet.Applet). Further on in the program,&lt;br&gt;&lt;br /&gt;however, you referred to all kinds of other classes as if they were available. &lt;br /&gt;The compiler catches this and tells you that you don't have access to those &lt;br /&gt;other classes.&lt;br&gt;&lt;br /&gt;There are two ways to solve this problem: Refer to all external classes by full &lt;br /&gt;package name or import the appropriate class or package at the beginning of your &lt;br /&gt;class file. Which one you choose to do is mostly a matter of choice, although if &lt;br /&gt;you find yourself referring to a class in another package lots of times, you may &lt;br /&gt;want to import it to cut down on the amount of typing.&lt;br&gt;&lt;br /&gt;In this example, you'll import the classes you need. There are three of them: &lt;br /&gt;Graphics, Font, and Color.&lt;br&gt;&lt;br /&gt;All three are part of the java.awt package. Here are the lines to import these &lt;br /&gt;classes. These lines go at the&lt;br&gt;&lt;br /&gt;top of your program, before the actual class definition:&lt;br&gt;&lt;br /&gt;import java.awt.Graphics;&lt;br&gt;&lt;br /&gt;import java.awt.Font;&lt;br&gt;&lt;br /&gt;import java.awt.Color;&lt;br&gt;&lt;br /&gt;Tip&lt;br&gt;&lt;br /&gt;You also can import an entire package of public classes by using an asterisk (*) &lt;br /&gt;in place of a specific class name. For example, to&lt;br&gt;&lt;br /&gt;import all the classes in the awt package, you can use this line:&lt;br&gt;&lt;br /&gt;import java.awt.*;&lt;br&gt;&lt;br /&gt;Now, with the proper classes imported into your program, HelloAgainApplet.java &lt;br /&gt;should compile cleanly to a class file. Listing 2.4 shows the final version to &lt;br /&gt;double-check.&lt;br&gt;&lt;br /&gt;Listing 2.4. The final version of HelloAgainApplet.java.&lt;br&gt;&lt;br /&gt;1:import java.awt.Graphics;&lt;br&gt;&lt;br /&gt;2:import java.awt.Font;&lt;br&gt;&lt;br /&gt;3:import java.awt.Color;&lt;br&gt;&lt;br /&gt;4:&lt;br&gt;&lt;br /&gt;5:public class HelloAgainApplet extends java.applet.Applet {&lt;br&gt;&lt;br /&gt;6:&lt;br&gt;&lt;br /&gt;7: Font f = new Font(&amp;quot;TimesRoman&amp;quot;,Font.BOLD,36);&lt;br&gt;&lt;br /&gt;8:&lt;br&gt;&lt;br /&gt;9: public void paint(Graphics g) {&lt;br&gt;&lt;br /&gt;10: g.setFont(f);&lt;br&gt;&lt;br /&gt;11: g.setColor(Color.red);&lt;br&gt;&lt;br /&gt;12: g.drawString(&amp;quot;Hello again!&amp;quot;, 5, 40);&lt;br&gt;&lt;br /&gt;13: }&lt;br&gt;&lt;br /&gt;14:}&lt;br&gt;&lt;br /&gt;To test it, create an HTML file with the &amp;lt;APPLET&amp;gt; tag as you did yesterday. &lt;br /&gt;Here's an HTML file to use:&lt;br&gt;&lt;br /&gt;&amp;lt;HTML&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;HEAD&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;TITLE&amp;gt;Another Applet&amp;lt;/TITLE&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;/HEAD&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;BODY&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;P&amp;gt;My second Java applet says:&lt;br&gt;&lt;br /&gt;&amp;lt;BR&amp;gt;&amp;lt;APPLET CODE=&amp;quot;HelloAgainApplet.class&amp;quot; WIDTH=200 HEIGHT=50&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;/APPLET&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;/BODY&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;/HTML&amp;gt;&lt;br&gt;&lt;br /&gt;For this HTML example, your Java class file is in the same directory as this &lt;br /&gt;HTML file. Save the file to HelloAgainApplet.html and fire up your Java-enabled &lt;br /&gt;browser or the Java applet viewer. Figure 2.7 shows the result you should be &lt;br /&gt;getting (the &amp;quot;Hello Again!&amp;quot; string is red).&lt;br&gt;&lt;br /&gt;Figure 2.7 : The HelloAgain applet.&lt;br&gt;&lt;br /&gt;Summary&lt;br&gt;&lt;br /&gt;If this is your first encounter with object-oriented programming, a lot of the &lt;br /&gt;information in this lesson is going to seem really theoretical and overwhelming. &lt;br /&gt;Fear not-the further along in this book you get, and the more Java classes and &lt;br /&gt;applications you create, the easier it is to understand. &lt;br&gt;&lt;br /&gt;One of the biggest hurdles of object-oriented programming is not necessarily the &lt;br /&gt;concepts; it's their names. OOP has lots of jargon surrounding it. To summarize &lt;br /&gt;today's material, here's a glossary of terms and concepts you learned today:&lt;br&gt;&lt;br /&gt;class: A template for an object, which contains variables and methods &lt;br /&gt;representing behavior and&lt;br&gt;&lt;br /&gt;attributes. Classes can inherit variables and methods from other classes.&lt;br&gt;&lt;br /&gt;class method: A method defined in a class, which operates on the class itself &lt;br /&gt;and can be called via the class or any of its instances.&lt;br&gt;&lt;br /&gt;class variable: A variable that is &amp;quot;owned&amp;quot; by the class and all its instances as &lt;br /&gt;a whole and is stored in the class.&lt;br&gt;&lt;br /&gt;instance: The same thing as an object; each object is an instance of some class.&lt;br&gt;&lt;br /&gt;instance method: A method defined in a class, which operates on an instance of &lt;br /&gt;that class. Instance methods are usually called just methods.&lt;br&gt;&lt;br /&gt;instance variable: A variable that is owned by an individual instance and whose &lt;br /&gt;value is stored in the instance.&lt;br&gt;&lt;br /&gt;interface: A collection of abstract behavior specifications that individual &lt;br /&gt;classes can then implement.&lt;br&gt;&lt;br /&gt;object: A concrete instance of some class. Multiple objects that are instances &lt;br /&gt;of the same class have access to the same methods, but often have different &lt;br /&gt;values for their instance variables.&lt;br&gt;&lt;br /&gt;package: A collection of classes and interfaces. Classes from packages other &lt;br /&gt;than java.lang must be explicitly imported or referred to by full package name.&lt;br&gt;&lt;br /&gt;subclass: A class lower in the inheritance hierarchy than its parent, the &lt;br /&gt;superclass. When you create a new class, it's often called subclassing.&lt;br&gt;&lt;br /&gt;superclass: A class further up in the inheritance hierarchy than its child, the &lt;br /&gt;subclass.&lt;br&gt;&lt;br /&gt;Q&amp;amp;A&lt;br&gt;&lt;br /&gt;Q: Methods are effectively functions that are defined inside classes. If they &lt;br /&gt;look like functions and act like functions, why aren't they called functions?&lt;br&gt;&lt;br /&gt;A: Some object-oriented programming languages do call them functions (C++ calls &lt;br /&gt;them member functions). Other object-oriented languages differentiate between &lt;br /&gt;functions inside and outside a body of a class or object, where having separate &lt;br /&gt;terms is important to understanding how each works.&lt;br&gt;&lt;br /&gt;Because the difference is relevant in other languages and because the term &lt;br /&gt;method is now in such common use in object-oriented technology, Java uses the &lt;br /&gt;word as well.&lt;br&gt;&lt;br /&gt;Q: I understand instance variables and methods, but not the idea of class &lt;br /&gt;variables and methods.&lt;br&gt;&lt;br /&gt;A: Most everything you do in a Java program will be with objects. Some behaviors &lt;br /&gt;and attributes, however, make more sense if they are stored in the class itself &lt;br /&gt;rather than in the object. For example, to create a new instance of a class, you &lt;br /&gt;need a method that is defined and available in the class itself. (Otherwise, how &lt;br /&gt;can you create an object? You need an object to call the method, but you don't &lt;br /&gt;have&lt;br&gt;&lt;br /&gt;an object yet.) Class variables, on the other hand, are often used when you have &lt;br /&gt;an attribute whose value you want to share with all the instances of a class.&lt;br&gt;&lt;br /&gt;Most of the time, you'll use instance variables and methods. You'll learn more &lt;br /&gt;about class variables and methods later this week.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;&amp;nbsp;&lt;/p&gt;</description><thr:total xmlns:thr="http://purl.org/syndication/thread/1.0">0</thr:total></item><item><title>Java Help, Java Tutorials, Java Programming, Java Tricks</title><link>http://swebsolution.blogspot.com/2008/01/java-help-java-tutorials-java.html</link><author>noreply@blogger.com (Unknown)</author><pubDate>Fri, 4 Jan 2008 06:47:00 -0800</pubDate><guid isPermaLink="false">tag:blogger.com,1999:blog-7227425366482465228.post-4135847947968469918</guid><description>&lt;p align="justify"&gt;&lt;b&gt;Lesson One&lt;/b&gt; &lt;br&gt;&lt;br&gt;Java Programming&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;What Is Java?&lt;br&gt;&lt;br /&gt;Java's Past, Present, and Future&lt;br&gt;&lt;br /&gt;Why Learn Java?&lt;br&gt;&lt;br /&gt;Java Is Platform Independent&lt;br&gt;&lt;br /&gt;Java Is Object Oriented&lt;br&gt;&lt;br /&gt;Java Is Easy to Learn &lt;br&gt;&lt;br /&gt;Getting Started Programming in Java&lt;br&gt;&lt;br /&gt;Getting a Java Development Environment&lt;br&gt;&lt;br /&gt;Installing the JDK and Sample Files&lt;br&gt;&lt;br /&gt;Configuring the JDK&lt;br&gt;&lt;br /&gt;Creating a Java Application&lt;br&gt;&lt;br /&gt;Creating a Java Applet&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;Troubleshooting&lt;br&gt;&lt;br /&gt;Summary&lt;br&gt;&lt;br /&gt;Q&amp;amp;A&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;What exactly Java is, and its current status&lt;br&gt;&lt;br /&gt;Why you should learn Java-its various features and advantages over other &lt;br /&gt;programming languages Getting started programming in Java-what you'll need in &lt;br /&gt;terms of software and background, as well as some basic terminology&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;How to create your first Java programs-to close this day, you'll create both a &lt;br /&gt;simple Java application and a simple Java applet!&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;What Is Java?&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;Based on the enormous amount of press Java is getting and the amount of &lt;br /&gt;excitement it has generated, you may get the impression that Java will save the &lt;br /&gt;world-or at least solve all the problems of the Internet.&lt;br&gt;&lt;br /&gt;Not so. Java's hype has run far ahead of its capabilities, and while Java is &lt;br /&gt;indeed new and interesting, it really is another programming language with which &lt;br /&gt;you write programs that run on the Internet. In this respect, Java is closer to &lt;br /&gt;popular programming languages such as C, C++, Visual Basic, or Pascal, than it &lt;br /&gt;is to a page description language such as HTML, or a very simple scripting &lt;br /&gt;language such as JavaScript.&lt;br&gt;&lt;br /&gt;More specifically, Java is an object-oriented programming language developed by &lt;br /&gt;Sun Microsystems, a company best known for its high-end UNIX workstations. &lt;br /&gt;Modeled after C++, the Java language was designed to be small, simple, and &lt;br /&gt;portable across platforms and operating systems, both at the source and at the &lt;br /&gt;binary level, which means that Java programs (applets and applications) can run &lt;br /&gt;on any machine that has the Java virtual machine installed (you'll learn more &lt;br /&gt;about this later).&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;Java is usually mentioned in the context of the World Wide Web, where browsers &lt;br /&gt;such as Netscape's Navigator and Microsoft's Internet Explorer claim to be &amp;quot;Java &lt;br /&gt;enabled.&amp;quot; Java enabled means that the browser in question can download and play &lt;br /&gt;Java programs, called applets, on the reader's system. Applets appear in a Web &lt;br /&gt;page much the same way as images do, but unlike images, applets are dynamic and &lt;br /&gt;interactive. Applets can be used to create animation, figures, forms that &lt;br /&gt;immediately respond to inputfrom the reader, games, or other interactive effects &lt;br /&gt;on the same Web pages among the text and graphics.&lt;br&gt;&lt;br /&gt;Figure 1.1 shows an applet running in Netscape 3.0. (This applet, at http://prominence.com/java/poetry/, &lt;br /&gt;is an electronic version of the refrigerator magnets&lt;br&gt;&lt;br /&gt;that you can move around to create poetry or messages.)&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;New Term&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;Applets are programs that are downloaded from the World Wide Web by a Web &lt;br /&gt;browser and run inside an HTML Web page. You'll need a Java-enabled browser such &lt;br /&gt;as Netscape Navigator or Microsoft's&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;Internet Explorer to run applets.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;To create an applet, you write it in the Java language, compile it using a Java &lt;br /&gt;compiler, and refer to that applet in your HTML Web pages. You put the resulting &lt;br /&gt;HTML and Java files on a Web site in the same&lt;br&gt;&lt;br /&gt;way that you make ordinary HTML and image files available. Then, when someone &lt;br /&gt;using a Java-enabled browser views your page with the embedded applet, that &lt;br /&gt;browser downloads the applet to the local system and executes it, allowing your &lt;br /&gt;reader to view and interact with your applet in all its glory. (Readers using &lt;br /&gt;other browsers may see text, a static graphic, or nothing.) You'll learn more &lt;br /&gt;about how applets, browsers, and the World Wide Web work together later in this &lt;br /&gt;book.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;While applets are probably the most popular use of Java, the important thing to &lt;br /&gt;understand about Java s that you can do so much more with it than create and use &lt;br /&gt;applets. Java was written as a full-fledged general-purpose programming language &lt;br /&gt;in which you can accomplish the same sorts of tasks and solve the same sorts of &lt;br /&gt;problems that you can in other programming languages, such as C or C++.&lt;br&gt;&lt;br /&gt;Java's Past, Present, and Future&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;The Java language was developed at Sun Microsystems in 1991 as part of a &lt;br /&gt;research project to develop software for consumer electronics devices-television &lt;br /&gt;sets, VCRs, toasters, and the other sorts of machines&lt;br&gt;&lt;br /&gt;you can buy at any department store. Java's goals at that time were to be small, &lt;br /&gt;fast, efficient, and easily portable to a wide range of hardware devices. Those &lt;br /&gt;same goals made Java an ideal language for&lt;br&gt;&lt;br /&gt;distributing executable programs via the World Wide Web and also a &lt;br /&gt;general-purpose programming language for developing programs that are easily &lt;br /&gt;usable and portable across different platforms.&lt;br&gt;&lt;br /&gt;The Java language was used in several projects within Sun (under the name Oak), &lt;br /&gt;but did not get very much commercial attention until it was paired with HotJava. &lt;br /&gt;HotJava, an experimental World Wide Web&lt;br&gt;&lt;br /&gt;browser, was written in 1994 in a matter of months, both as a vehicle for &lt;br /&gt;downloading and running applets and also as an example of the sort of complex &lt;br /&gt;application that can be written in Java. Although HotJava got a lot of attention &lt;br /&gt;in the Web community, it wasn't until Netscape incorporated HotJava's&lt;br&gt;&lt;br /&gt;ability to play applets into its own browser that Java really took off and &lt;br /&gt;started to generate the excitement that it has both on and off the World Wide &lt;br /&gt;Web. Java has generated so much excitement, in fact, that&lt;br&gt;&lt;br /&gt;inside Sun the Java group spun off into its own subsidiary called JavaSoft.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;Versions of Java itself, or, as it's most commonly called, the Java API, &lt;br /&gt;correspond to versions of Sun's Java Developer's Kit, or JDK. As of this &lt;br /&gt;writing, the current version of the JDK is 1.0.2. Previously&lt;br&gt;&lt;br /&gt;released versions of the JDK (alphas and betas) did not have all the features or &lt;br /&gt;had a number of security-related bugs. Most Java tools and browsers conform to &lt;br /&gt;the features in the 1.0.2 JDK, and all the&lt;br&gt;&lt;br /&gt;examples in this book run on that version as well.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;The next major release of the JDK and therefore of the Java API will be 1.1, &lt;br /&gt;with a prerelease version available sometime in the later part of 1996. This &lt;br /&gt;release will have few changes to the language, but a&lt;br&gt;&lt;br /&gt;number of additional capabilities and features added to the class library. &lt;br /&gt;Throughout this book, if a feature will change or will be enhanced in 1.1, we'll &lt;br /&gt;let you know, and in the last two days of this book&lt;br&gt;&lt;br /&gt;you'll find out more about new Java features for 1.1 and for the future.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;Currently, to program in Java, you'll need a Java development environment of &lt;br /&gt;some sort for your platform. Sun's JDK works just fine for this purpose and &lt;br /&gt;includes tools for compiling and testing Java&lt;br&gt;&lt;br /&gt;applets and applications. In addition, a wide variety of excellent Java &lt;br /&gt;development environments have been developed, including Sun's own Java Workshop, &lt;br /&gt;Symantec's Café, Microsoft's Visual J++ (which is&lt;br&gt;&lt;br /&gt;indeed a Java tool, despite its name), and Natural Intelligence's Roaster, with &lt;br /&gt;more development tools appearing all the time.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;To run and view Java applets, you'll need a Java-enabled browser or other tool. &lt;br /&gt;As mentioned before, recent versions of Netscape Navigator (2.0 and higher) and &lt;br /&gt;Internet Explorer (3.0) can both run Java&lt;br&gt;&lt;br /&gt;applets. (Note that for Windows you'll need the 32-bit version of Netscape, and &lt;br /&gt;for Macintosh you'll need Netscape 3.0.) You can also use Sun's own HotJava &lt;br /&gt;browser to view applets, as long as you have the 1.0&lt;br&gt;&lt;br /&gt;prebeta version (older versions are not compatible with newer applets, and vice &lt;br /&gt;versa). Even if you don't have a Java-enabled browser, many development tools &lt;br /&gt;provide simple viewers with which you can run&lt;br&gt;&lt;br /&gt;your applets. The JDK comes with one of these; it's called the appletviewer. &lt;br&gt;&lt;br /&gt;Note&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;If you're running Windows 3.x as your main system, very few toolsexist for you &lt;br /&gt;to be able to work with Java. As I write this, the only&lt;br&gt;&lt;br /&gt;Java tool available for writing and running Java applets is a version ofthe JDK &lt;br /&gt;from IBM called the ADK. You can write applets using this&lt;br&gt;&lt;br /&gt;tool, and view them using the applet viewer that comes with thatpackage (neither &lt;br /&gt;Netscape nor Internet Explorer will run Java applets&lt;br&gt;&lt;br /&gt;on Windows 3.1). See http://www.alphaWorks.ibm.com/ for more information.&lt;br&gt;&lt;br /&gt;What's in store for Java in the future? A number of new developments have been &lt;br /&gt;brewing (pardon the&lt;br&gt;&lt;br /&gt;pun):&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;Sun is developing a number of new features for the Java environment, including a &lt;br /&gt;number of new class libraries for database integration, multimedia, electronic &lt;br /&gt;commerce, and other uses. Sun also&lt;br&gt;&lt;br /&gt;has a Java-based Web server, a Java-based hardware chip (with which you can &lt;br /&gt;write Java-specific systems), and a Java-based operating system. You'll learn &lt;br /&gt;about all these things later in this book.&lt;br&gt;&lt;br /&gt;The 1.1 release of the JDK will include many of these features; others will be &lt;br /&gt;released as separate&lt;br&gt;&lt;br /&gt;packages.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;Sun is also developing a framework called Java Beans, which will allow the &lt;br /&gt;development of component objects in Java, similarly to Microsoft's ActiveX (OLE) &lt;br /&gt;tech-nology. These different&lt;br&gt;&lt;br /&gt;components can then be easily combined and interact with each other using &lt;br /&gt;standard component assembly tools. You'll learn more about Java Beans later in &lt;br /&gt;this book.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;Java capabilities will be incorporated into a wide variety of operating systems, &lt;br /&gt;including Solaris, Windows 95, and MacOS. This means that Java applications (as &lt;br /&gt;opposed to applets) can run nearly&lt;br&gt;&lt;br /&gt;anywhere without needing additional software to be installed.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;Many companies are working on performance enhancements for Java programs, &lt;br /&gt;including the aforementioned Java chip and what are called just-in-time &lt;br /&gt;compilers.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;Why Learn Java?&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;At the moment, probably the most compelling reason to learn Java-and probably &lt;br /&gt;the reason you bought this book-is that applets are written in Java. Even if &lt;br /&gt;that were not the case, Java as a programming&lt;br&gt;&lt;br /&gt;language has significant advantages over other languages and other environments &lt;br /&gt;that make it suitable for just about any programming task. This section &lt;br /&gt;describes some of those advantages.&lt;br&gt;&lt;br /&gt;Java Is Platform Independent Platform independence-that is, the ability of a &lt;br /&gt;program to move easily from one computer system to&lt;br&gt;&lt;br /&gt;another-is one of the most significant advantages that Java has over other &lt;br /&gt;programming languages, particularly if your software needs to run on many &lt;br /&gt;different platforms. If you're writing software for the&lt;br&gt;&lt;br /&gt;World Wide Web, being able to run the same program on many different systems is &lt;br /&gt;crucial to that program's success. Java is platform independent at both the &lt;br /&gt;source and the binary level.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;New Term&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;Platform independence means that a program can run on anycomputer system. Java &lt;br /&gt;programs can run on any system for which a Java virtual machine has been &lt;br /&gt;installed.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;At the source level, Java's primitive data types have consistent sizes across &lt;br /&gt;all development platforms. Java's foundation class libraries make it easy to &lt;br /&gt;write code that can be moved from platform to platform&lt;br&gt;&lt;br /&gt;without the need to rewrite it to work with that platform. When you write a &lt;br /&gt;program in Java, you don't need to rely on features of that particular operating &lt;br /&gt;system to accomplish basic tasks. Platform&lt;br&gt;&lt;br /&gt;independence at the source level means that you can move Java source files from &lt;br /&gt;system to system and have them compile and run cleanly on any system.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;Platform independence in Java doesn't stop at the source level, however. Java &lt;br /&gt;compiled binary files are also platform independent and can run on multiple &lt;br /&gt;platforms (if they have a Java virtual machine&lt;br&gt;&lt;br /&gt;available) without the need to recompile the source.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;Normally, when you compile a program written in C or in most other languages, &lt;br /&gt;the compiler translates your program into machine code or processor &lt;br /&gt;instructions. Those instructions are specific to the&lt;br&gt;&lt;br /&gt;processor your computer is running-so, for example, if you compile your code on &lt;br /&gt;an Intel-based system,the resulting program will run only on other Intel-based &lt;br /&gt;systems. If you want to use the same program on&lt;br&gt;&lt;br /&gt;another system, you have to go back to your original source code, get a compiler &lt;br /&gt;for that system, and recompile your code so that you have a program specific to &lt;br /&gt;that system. Figure 1.2 shows the result of&lt;br&gt;&lt;br /&gt;this system: multiple executable programs for multiple systems.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;Things are different when you write code in Java. The Java development &lt;br /&gt;environment actually has two parts: a Java compiler and a Java interpreter. The &lt;br /&gt;Java compiler takes your Java program and, instead of&lt;br&gt;&lt;br /&gt;generating machine codes from your source files, it generates bytecodes. &lt;br /&gt;Bytecodes are instructions that look a lot like machine code, but are not &lt;br /&gt;specific to any one processor.&lt;br&gt;&lt;br /&gt;To execute a Java program, you run a program called a bytecode interpreter, &lt;br /&gt;which in turn reads the bytecodes and executes your Java program (see Figure &lt;br /&gt;1.3). The Java bytecode interpreter is often also&lt;br&gt;&lt;br /&gt;called the Java virtual machine or the Java runtime.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;New Term&lt;br&gt;&lt;br /&gt;Java bytecodes are a special set of machine instructions that are notspecific to &lt;br /&gt;any one processor or computer system. A platform-specific bytecode interpreter &lt;br /&gt;executes the Java bytecodes. The bytecodeinterpreter is also called the Java &lt;br /&gt;virtual machine or the Java runtime&lt;br&gt;&lt;br /&gt;interpreter.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;Where do you get the bytecode interpreter? For applets, the bytecode interpreter &lt;br /&gt;is built into every Java-enabled browser, so you don't have to worry about &lt;br /&gt;it-Java applets just automatically run. For more&lt;br&gt;&lt;br /&gt;general Java applications, you'll need to have the interpreter installed on your &lt;br /&gt;system in order to run that Java program. Right now, you can get the Java &lt;br /&gt;interpreter as part of your development environment, or if&lt;br&gt;&lt;br /&gt;you buy a Java program, you'll get it with that package. In the future, however, &lt;br /&gt;the Java bytecode interpreter will most likely come with every new operating &lt;br /&gt;system-buy a Windows machine, and you'll&lt;br&gt;&lt;br /&gt;get Java for free.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;Why go through all the trouble of adding this extra layer of the bytecode &lt;br /&gt;interpreter? Having your Java programs in bytecode form means that instead of &lt;br /&gt;being specific to any one system, your programs can be&lt;br&gt;&lt;br /&gt;run on any platform and any operating or window system as long as the Java &lt;br /&gt;interpreter is available. This capability of a single binary file to be &lt;br /&gt;executable across platforms is crucial to what makes applets work&lt;br&gt;&lt;br /&gt;because the World Wide Web itself is also platform independent. Just as HTML &lt;br /&gt;files can be read on any platform, so can applets be executed on any platform &lt;br /&gt;that has a Java-enabled browser.&lt;br&gt;&lt;br /&gt;The disadvantage of using bytecodes is in execution speed. Because &lt;br /&gt;system-specific programs run directly on the hardware for which they are &lt;br /&gt;compiled, they run significantly faster than Java bytecodes,&lt;br&gt;&lt;br /&gt;which must be processed by the interpreter. For many basic Java programs, speed &lt;br /&gt;may not be an issue. If you write programs that require more execution speed &lt;br /&gt;than the Java interpreter can provide, you have&lt;br&gt;&lt;br /&gt;several solutions available to you, including being able to link native code &lt;br /&gt;into your Java program or using special tools (called just-in-time compilers) to &lt;br /&gt;convert your Java bytecodes into native code and&lt;br&gt;&lt;br /&gt;speed up their execution. Note that by using any of these solutions, you lose &lt;br /&gt;the portability that Java bytecodes provide. You'll learn about each of these &lt;br /&gt;mechanisms on Day 20, &amp;quot;Using Native Methods and&lt;br&gt;&lt;br /&gt;Libraries.&amp;quot;&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;Java Is Object Oriented&lt;br&gt;&lt;br /&gt;To some, the object-oriented programming (OOP) technique is merely a way of &lt;br /&gt;organizing programs, and it can be accomplished using any language. Working with &lt;br /&gt;a real object-oriented language and&lt;br&gt;&lt;br /&gt;programming environment, however, enables you to take full advantage of &lt;br /&gt;object-oriented methodology and its capabilities for creating flexible, modular &lt;br /&gt;programs and reusing code.&lt;br&gt;&lt;br /&gt;Many of Java's object-oriented concepts are inherited from C++, the language on &lt;br /&gt;which it is based, but it borrows many concepts from other object-oriented &lt;br /&gt;languages as well. Like most object-oriented&lt;br&gt;&lt;br /&gt;programming languages, Java includes a set of class libraries that provide basic &lt;br /&gt;data types, system input and output capabilities, and other utility functions. &lt;br /&gt;These basic libraries are part of the standard Java&lt;br&gt;&lt;br /&gt;environment, which also includes simple libraries, form networking, common &lt;br /&gt;Internet protocols, and user interface toolkit functions. Because these class &lt;br /&gt;libraries are written in Java, they are portable across&lt;br&gt;&lt;br /&gt;platforms as all Java applications are.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;You'll learn more about object-oriented programming and Java tomorrow.&lt;br&gt;&lt;br /&gt;Java Is Easy to Learn&lt;br&gt;&lt;br /&gt;In addition to its portability and object orientation, one of Java's initial &lt;br /&gt;design goals was to be small and simple, and therefore easier to write, easier &lt;br /&gt;to compile, easier to debug, and, best of all, easy to learn.&lt;br&gt;&lt;br /&gt;Keeping the language small also makes it more robust because there are fewer &lt;br /&gt;chances for programmers to make mistakes that are difficult to fix. Despite its &lt;br /&gt;size and simple design, however, Java still has a&lt;br&gt;&lt;br /&gt;great deal of power and flexibility.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;Java is modeled after C and C++, and much of the syntax and object-oriented &lt;br /&gt;structure is borrowed from the latter. If you are familiar with C++, learning &lt;br /&gt;Java will be particularly easy for you because you have&lt;br&gt;&lt;br /&gt;most of the foundation already. (In fact, you may find yourself skipping through &lt;br /&gt;the first week of this book fairly rapidly. Go ahead; I won't mind.)Although &lt;br /&gt;Java looks similar to C and C++, most of the more complex parts of those &lt;br /&gt;languages have been&lt;br&gt;&lt;br /&gt;excluded from Java, making the language simpler without sacrificing much of its &lt;br /&gt;power. There are no pointers in Java, nor is there pointer arithmetic. Strings &lt;br /&gt;and arrays are real objects in Java. Memory&lt;br&gt;&lt;br /&gt;management is automatic. To an experienced programmer, these omissions may be &lt;br /&gt;difficult to get used to, but to beginners or programmers who have worked in &lt;br /&gt;other languages, they make the Java language&lt;br&gt;&lt;br /&gt;far easier to learn.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;However, while Java's design makes it easier to learn than other programming &lt;br /&gt;languages, working with a programming language is still a great deal more &lt;br /&gt;complicated than, say, working in HTML. If you have&lt;br&gt;&lt;br /&gt;no programming language background at all, you may find Java difficult to &lt;br /&gt;understand and to grasp. But don't be discouraged! Learning programming is a &lt;br /&gt;valuable skill for the Web and for computers in&lt;br&gt;&lt;br /&gt;general, and Java is a terrific language to start out with.&lt;br&gt;&lt;br /&gt;Getting Started Programming in JavaEnough background! For the second half of &lt;br /&gt;this day let's actually dive into simple Java programming and&lt;br&gt;&lt;br /&gt;create two Java programs: a standalone Java application and an applet that you &lt;br /&gt;can view in a Java-enabled browser. Although both these programs are extremely &lt;br /&gt;simple, they will give you an idea of&lt;br&gt;&lt;br /&gt;what a Java program looks like and how to compile and run it.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;Getting a Java Development Environment In order to write Java programs, you &lt;br /&gt;will, of course, need a Java development environment. (Although&lt;br&gt;&lt;br /&gt;browsers such as Netscape allow you to play Java applets, they don't let you &lt;br /&gt;write them. For that you'll need a separate tool.) Sun's JDK, which is available &lt;br /&gt;for downloading at the JavaSoft Web site&lt;br&gt;&lt;br /&gt;(http://www.javasoft.com/) and included on the CD for this book, will do just &lt;br /&gt;fine. It runs on Solaris, Windows 95 and NT, and Macintosh. However, despite the &lt;br /&gt;JDK's popularity, it is not the easiest&lt;br&gt;&lt;br /&gt;development tool to use. If you're used to using a graphical user &lt;br /&gt;interface-based development tool with an integrated editor and debugger, you'll &lt;br /&gt;most likely find the JDK's command-line interfaces rather&lt;br&gt;&lt;br /&gt;primitive. Fortunately, the JDK is not the only tool in town. &lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;As mentioned earlier, a number of third-party development environments (called &lt;br /&gt;integrated development environments, or IDEs) are also available for developing &lt;br /&gt;in Java. These include Sun's Java Workshop for&lt;br&gt;&lt;br /&gt;Solaris, Windows NT and Windows 95 (you can get more information about it athttp://www.sun.com/developer-products/java/); &lt;br /&gt;Symantec's Café for Windows 95,&lt;br&gt;&lt;br /&gt;Windows NT, and Macintosh (http://cafe.symantec.com/); Microsoft's Visual J++ &lt;br /&gt;forWindows 95 and Windows NT (http://www.microsoft.com/visualj/); and Natural&lt;br&gt;&lt;br /&gt;Intelligence's Roaster(http://www.natural.com/pages/products/roaster/index.html). &lt;br /&gt;All three are&lt;br&gt;&lt;br /&gt;commercial programs, but you might be able to download trial or limited versions &lt;br /&gt;of these programs to&lt;br&gt;&lt;br /&gt;try them out. You'll learn more about the features and capabilities of the &lt;br /&gt;various Java IDEs on Day 22,&lt;br&gt;&lt;br /&gt;&amp;quot;Java Programming Tools.&amp;quot;&lt;br&gt;&lt;br /&gt;Note&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;I find the graphical development environments far easier to use than&lt;br&gt;&lt;br /&gt;the standard JDK. If you have the money and the time to invest in one&lt;br&gt;&lt;br /&gt;of these tools, I highly recommend you do so. It'll make your Java&lt;br&gt;&lt;br /&gt;development experience much more pleasant.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;Installing the JDK and Sample Files&lt;br&gt;&lt;br /&gt;Sun's JDK for Solaris, Windows, and Macintosh is included as part of the CD-ROM &lt;br /&gt;that comes with this&lt;br&gt;&lt;br /&gt;book. Also on the CD-ROM are all of the code examples from this book-a great &lt;br /&gt;help if you don't want to&lt;br&gt;&lt;br /&gt;type them all in again. To install either the JDK or the sample files (or both), &lt;br /&gt;use one of the following&lt;br&gt;&lt;br /&gt;procedures:&lt;br&gt;&lt;br /&gt;Note&lt;br&gt;&lt;br /&gt;If you don't have access to a CD-ROM drive, you can also get accessto these &lt;br /&gt;files over the World Wide Web. You can download the JDK&lt;br&gt;&lt;br /&gt;itself from http://java.sun.com/products/JDK/1.0.2/and install it per the &lt;br /&gt;instructions on those pages. The sample files&lt;br&gt;&lt;br /&gt;from this book are available on the Web site for this book:http://www.lne.com/Web/JavaProf/.&lt;br&gt;&lt;br /&gt;If you download the JDK and source files, as opposed to getting themoff the &lt;br /&gt;CD-ROM, make sure you read the section &amp;quot;Configuring the&lt;br&gt;&lt;br /&gt;JDK&amp;quot; to make sure everything is set up right.Windows Sun's JDK runs on Windows &lt;br /&gt;95 and Windows NT. It does not run on&lt;br&gt;&lt;br /&gt;Windows 3.x.&lt;br&gt;&lt;br /&gt;To install the JDK or the sample files on Windows, run the Setup program on the &lt;br /&gt;CD-ROM&lt;br&gt;&lt;br /&gt;(double-clicking the CD icon will do this automatically). By default, the &lt;br /&gt;package will be installed into&lt;br&gt;&lt;br /&gt;C:\Java; you can install it anywhere on your hard disk that you'd like. You'll &lt;br /&gt;be given options to install&lt;br&gt;&lt;br /&gt;the JDK, the sample files, and various other extra files; choose the options you &lt;br /&gt;want and those files will&lt;br&gt;&lt;br /&gt;be installed.&lt;br&gt;&lt;br /&gt;If you've installed the JDK, note that in the directory JDK\lib there is a file &lt;br /&gt;called classes.zip. Do&lt;br&gt;&lt;br /&gt;not unzip this file; it needs to remain in zip form for it to work correctly. &lt;br /&gt;The file JDK\src.zip&lt;br&gt;&lt;br /&gt;contains the source code for many of the JDK libraries; you can unzip this one &lt;br /&gt;if you like. Make sure if&lt;br&gt;&lt;br /&gt;you do that you have a zip program that supports long filenames, or it will not &lt;br /&gt;work correctly!&lt;br&gt;&lt;br /&gt;Macintosh&lt;br&gt;&lt;br /&gt;Sun's JDK for Macintosh runs on System 7 (MacOS) for 68KB or&lt;br&gt;&lt;br /&gt;Power Mac.&lt;br&gt;&lt;br /&gt;To install the JDK or the sample files on the Macintosh, double-click the &lt;br /&gt;installation program on the&lt;br&gt;&lt;br /&gt;CD-ROM. By default, the package will be installed into the folder Java on your &lt;br /&gt;hard disk; you can&lt;br&gt;&lt;br /&gt;install it anywhere on your disk that you'd like. You'll be given options to &lt;br /&gt;install the JDK, the sample&lt;br&gt;&lt;br /&gt;files, and various other extra files; choose the options you want and those &lt;br /&gt;files will be installed.&lt;br&gt;&lt;br /&gt;Solaris&lt;br&gt;&lt;br /&gt;Sun's JDK for Solaris runs on Solaris 2.3, 2.4, and 2.5, as well as the&lt;br&gt;&lt;br /&gt;x86 version of Solaris.&lt;br&gt;&lt;br /&gt;The CD-ROM for this book contains the tarred and zipped JDK in the directory&lt;br&gt;&lt;br /&gt;jdk/solaris/jdk1.02.tgz. Using the utilities gunzip and tar, you can extract the &lt;br /&gt;contents of&lt;br&gt;&lt;br /&gt;that file anywhere on the file system you would like. For example, if you copy &lt;br /&gt;the .tgz file to your&lt;br&gt;&lt;br /&gt;home directory and use the following commands to extract it, you'll end up with &lt;br /&gt;a java directory that&lt;br&gt;&lt;br /&gt;contains the full JDK:&lt;br&gt;&lt;br /&gt;gunzip ./jdk1.02.tgz&lt;br&gt;&lt;br /&gt;tar xvf ./jdk1.02.tar&lt;br&gt;&lt;br /&gt;Note that in the directory java\lib there is a file called classes.zip. Do not &lt;br /&gt;unzip this file; it&lt;br&gt;&lt;br /&gt;needs to remain in zip form for it to work correctly. The file java\src.zip &lt;br /&gt;contains the source code&lt;br&gt;&lt;br /&gt;for many of the JDK libraries; you can unzip this one if you're interested in &lt;br /&gt;the source code.&lt;br&gt;&lt;br /&gt;The sample files are also contained on the CD-ROM in authors/authors.tar. Create &lt;br /&gt;a directory&lt;br&gt;&lt;br /&gt;where the sample files will live (for example, a directory called javasamples in &lt;br /&gt;your home directory),&lt;br&gt;&lt;br /&gt;copy the authors.tar file there, and then use the tar command to extract it, &lt;br /&gt;like this:&lt;br&gt;&lt;br /&gt;mkdir ~/javasamples&lt;br&gt;&lt;br /&gt;cp /cdrom/authors/authors.tar&lt;br&gt;&lt;br /&gt;tar xvf authors.tar&lt;br&gt;&lt;br /&gt;Configuring the JDK&lt;br&gt;&lt;br /&gt;If you've installed the JDK using the setup programs from the CD-ROM, chances &lt;br /&gt;are good that it has&lt;br&gt;&lt;br /&gt;been correctly configured for you. However, because most common problems with &lt;br /&gt;Java result from&lt;br&gt;&lt;br /&gt;configuration errors, I recommend that you double-check your configuration to &lt;br /&gt;make sure everything is&lt;br&gt;&lt;br /&gt;right. And if you've installed the JDK from a source other than the CD-ROM, &lt;br /&gt;you'll definitely want to&lt;br&gt;&lt;br /&gt;read this section to make sure you're all set up.&lt;br&gt;&lt;br /&gt;Windows&lt;br&gt;&lt;br /&gt;The JDK needs two important modifications to yourautoexec.bat file in order to &lt;br /&gt;work correctly: The JDK\bin&lt;br&gt;&lt;br /&gt;directory must be in your execution path, and you must have the CLASSPATH &lt;br /&gt;variable set up.&lt;br&gt;&lt;br /&gt;Edit your autoexec.bat file using your favorite editor (Notepad will do just &lt;br /&gt;fine). Look for a line&lt;br&gt;&lt;br /&gt;that looks something like this:&lt;br&gt;&lt;br /&gt;PATH C:\WINDOWS;C:\WINDOWS\COMMAND;C:\DOS; ...&lt;br&gt;&lt;br /&gt;Somewhere in that line you should see an entry for the JDK; if you installed the &lt;br /&gt;JDK from CD-ROM, it'll&lt;br&gt;&lt;br /&gt;look something like this (the dots are there to indicate that there may be other &lt;br /&gt;stuff on this line):&lt;br&gt;&lt;br /&gt;PATH C:\WINDOWS; ... C:\TEAchY~1\JDK\BIN; ...&lt;br&gt;&lt;br /&gt;If you cannot find any reference to JDK\BIN or JAVA\BIN in your PATH, you'll &lt;br /&gt;need to add it. Simply&lt;br&gt;&lt;br /&gt;include the full pathname to your JDK installation to the end of that line, &lt;br /&gt;starting with C: and ending&lt;br&gt;&lt;br /&gt;with BIN; for example, C:\JAVA\BIN or C:\Java\JDK\BIN.&lt;br&gt;&lt;br /&gt;Note&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;The directories Teach Yourself Java and TEAchY~1 are&lt;br&gt;&lt;br /&gt;actually the same thing; the former is how the directory appears in&lt;br&gt;&lt;br /&gt;Windows 95, and the latter is how it appears in DOS. Either one will&lt;br&gt;&lt;br /&gt;work fine; there's no need to change it if one or the other appears.&lt;br&gt;&lt;br /&gt;Note, however, that if the pathname contains spaces, it must be in&lt;br&gt;&lt;br /&gt;quotes.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;The second thing you'll need to add to the autoexec.bat file (if it isn't &lt;br /&gt;already there) is a&lt;br&gt;&lt;br /&gt;CLASSPATH variable. Look for a line that looks something like this:&lt;br&gt;&lt;br /&gt;SET CLASSPATH=C:\TEAchY~1\JDK\lib\classes.zip;.;The CLASSPATH variable may also &lt;br /&gt;have other entries in it for Netscape or Internet Explorer, but the one&lt;br&gt;&lt;br /&gt;you're most interested in is a reference to the classes.zip file in the JDK, and &lt;br /&gt;to the current&lt;br&gt;&lt;br /&gt;directory (.). If your autoexec.bat file does not include either of these &lt;br /&gt;locations, add a line to the&lt;br&gt;&lt;br /&gt;file that contains both these things (the line shown above will work just fine).&lt;br&gt;&lt;br /&gt;After saving your autoexec.bat file, you'll need to restart Windows for the &lt;br /&gt;changes to take effect.&lt;br&gt;&lt;br /&gt;Macintosh&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;The JDK for Macintosh should need no further configuration after&lt;br&gt;&lt;br /&gt;installation.&lt;br&gt;&lt;br /&gt;Solaris&lt;br&gt;&lt;br /&gt;To configure the JDK for Solaris, all you need to do is add the&lt;br&gt;&lt;br /&gt;java/bin or jdk/bin directory to your execution path. Usually a&lt;br&gt;&lt;br /&gt;line something like this in your .cshrc, .login, or .profile&lt;br&gt;&lt;br /&gt;files will work:&lt;br&gt;&lt;br /&gt;set path= (~/java/bin/ $path)&lt;br&gt;&lt;br /&gt;This line assumes that you've installed the JDK (as the directory java) into &lt;br /&gt;your home directory; if&lt;br&gt;&lt;br /&gt;you've installed it somewhere else, you'll want to substitute that pathname.&lt;br&gt;&lt;br /&gt;Make sure you use the source command with the name of the appropriate file to &lt;br /&gt;make sure the changes&lt;br&gt;&lt;br /&gt;take effect (or log out and log back in again):&lt;br&gt;&lt;br /&gt;source ~/.login&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;Creating a Java Application&lt;br&gt;&lt;br /&gt;Now let's actually get to work. We'll start by creating a simple Java &lt;br /&gt;application: the classic Hello World&lt;br&gt;&lt;br /&gt;example that many programming language books use to begin.&lt;br&gt;&lt;br /&gt;Java applications are different from Java applets. Applets, as you have learned, &lt;br /&gt;are Java programs that&lt;br&gt;&lt;br /&gt;are downloaded over the World Wide Web and executed by a Web browser on the &lt;br /&gt;reader's machine.&lt;br&gt;&lt;br /&gt;Applets depend on a Java-enabled browser in order to run.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;New Term&lt;br&gt;&lt;br /&gt;Java applications, however, are more general programs written in the&lt;br&gt;&lt;br /&gt;Java language. Java applications don't require a browser to run; in&lt;br&gt;&lt;br /&gt;fact, Java can be used to create all the kinds of applications that you&lt;br&gt;&lt;br /&gt;would normally use a more conventional programming language tocreate. &lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;Java applications are standalone Java programs that do not require a Web browser &lt;br /&gt;to run. Java&lt;br&gt;&lt;br /&gt;applications are more general-purpose programs such as you'd find on any &lt;br /&gt;computer.&lt;br&gt;&lt;br /&gt;A single Java program can be an applet or an application, or both, depending on &lt;br /&gt;how you write that&lt;br&gt;&lt;br /&gt;program and the capabilities that program uses. Throughout this first week as &lt;br /&gt;you learn the Java&lt;br&gt;&lt;br /&gt;language, you'll be writing mostly applications; then you'll apply what you've &lt;br /&gt;learned to write applets in&lt;br&gt;&lt;br /&gt;Week 2. If you're eager to get started with applets, be patient. Everything that &lt;br /&gt;you learn while you're&lt;br&gt;&lt;br /&gt;creating simple Java applications will apply to creating applets, and it's &lt;br /&gt;easier to start with the basics&lt;br&gt;&lt;br /&gt;before moving onto the hard stuff. You'll be creating plenty of applets in Week &lt;br /&gt;2.Creating the Source File&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;As with all programming languages, your Java source files are created in a plain &lt;br /&gt;text editor, or in an&lt;br&gt;&lt;br /&gt;editor that can save files in plain ASCII without any formatting characters. On &lt;br /&gt;UNIX, emacs, pico,&lt;br&gt;&lt;br /&gt;and vi will work; on Windows, Notepad or DOS Edit are both text editors that &lt;br /&gt;will work (although I&lt;br&gt;&lt;br /&gt;prefer to use the shareware TextPad). On the Macintosh, SimpleText (which came &lt;br /&gt;with your Mac) or the&lt;br&gt;&lt;br /&gt;shareware BBedit will work. If you're using a development environment like Café &lt;br /&gt;or Roaster, it'll have&lt;br&gt;&lt;br /&gt;its own built-in text editor you can use.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;Note&lt;br&gt;&lt;br /&gt;If you're using Windows to do your Java development, you may have&lt;br&gt;&lt;br /&gt;to make sure Windows understands the .java file extension before&lt;br&gt;&lt;br /&gt;you start; otherwise, your text editor may insist on giving all your&lt;br&gt;&lt;br /&gt;files a .txt extension. The easiest way to do this is to go to any&lt;br&gt;&lt;br /&gt;Windows Explorer window, choose View|Options|File Types, choose&lt;br&gt;&lt;br /&gt;New Type, and add Java Source File and .java to the&lt;br&gt;&lt;br /&gt;Description of Type and Associated Extension boxes, respectively.&lt;br&gt;&lt;br /&gt;Fire up your editor of choice and enter the Java program shown in Listing 1.1. &lt;br /&gt;Type this program, as&lt;br&gt;&lt;br /&gt;shown, in your text editor. Be careful that all the parentheses, braces, and &lt;br /&gt;quotes are there, and that&lt;br&gt;&lt;br /&gt;you've used all the correct upper- and lowercase letters.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;Note&lt;br&gt;&lt;br /&gt;You can also find the code for these examples on the CD-ROM as&lt;br&gt;&lt;br /&gt;part of the sample code. However, it's a good idea to actually type&lt;br&gt;&lt;br /&gt;these first few short examples in so that you get a feel for what Java&lt;br&gt;&lt;br /&gt;code actually looks like.&lt;br&gt;&lt;br /&gt;Listing 1.1. Your first Java application.&lt;br&gt;&lt;br /&gt;1: class HelloWorld {&lt;br&gt;&lt;br /&gt;2: public static void main (String args[]) {&lt;br&gt;&lt;br /&gt;3: System.out.println(&amp;quot;Hello World!&amp;quot;);&lt;br&gt;&lt;br /&gt;4: }&lt;br&gt;&lt;br /&gt;5: }&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;Warning&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;The number before each line is part of the listing and not part of the&lt;br&gt;&lt;br /&gt;program; the numbers are there so I can refer to specific line numbers&lt;br&gt;&lt;br /&gt;when I explain what's going on in the program. Do not include them&lt;br&gt;&lt;br /&gt;in your own file.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;After you've finished typing in the program, save the file somewhere on your &lt;br /&gt;disk with the name&lt;br&gt;&lt;br /&gt;HelloWorld.java. This is very important. Java source files must have the same &lt;br /&gt;name as the class&lt;br&gt;&lt;br /&gt;they define (including the same upper- and lowercase letters), and they must &lt;br /&gt;have the extension .java.&lt;br&gt;&lt;br /&gt;Here, the class definition has the name HelloWorld, so the filename must be &lt;br /&gt;HelloWorld.java. If&lt;br&gt;&lt;br /&gt;you name your file something else (even something like helloworld.java or&lt;br&gt;&lt;br /&gt;Helloworld.java), you won't be able to compile it. Make absolutely certain the &lt;br /&gt;name isHelloWorld.java.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;You can save your Java files anywhere you like on your disk, but I like to have &lt;br /&gt;a central directory or&lt;br&gt;&lt;br /&gt;folder to keep them all in. For the examples in this chapter, I've put my files &lt;br /&gt;into a directory called&lt;br&gt;&lt;br /&gt;TYJtests (short for Teach Yourself Java Tests).&lt;br&gt;&lt;br /&gt;Compiling and Running the Source File&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;Now it's time to compile the file. If you're using the JDK, you can use the &lt;br /&gt;instructions for your computer&lt;br&gt;&lt;br /&gt;system contained in the next few pages. If you're using a graphical development &lt;br /&gt;environment, there will&lt;br&gt;&lt;br /&gt;most likely be a button or option to compile the file (check with the &lt;br /&gt;documentation that came with your&lt;br&gt;&lt;br /&gt;program).&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;Windows&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;To compile the Java source file, you'll use the command-line Java&lt;br&gt;&lt;br /&gt;compiler that comes with the JDK. To run the compiler, you'll need to&lt;br&gt;&lt;br /&gt;first start up a DOS shell. In Windows 95, the DOS shell is under the&lt;br&gt;&lt;br /&gt;Programs menu (it's called MS-DOS Prompt).&lt;br&gt;&lt;br /&gt;From inside DOS, change directories to the l&lt;br&gt;&lt;br /&gt;ocation where you've saved your HelloWorld.java file.&lt;br&gt;&lt;br /&gt;I put mine into the directory TYJtests, so to change directories I'd use this &lt;br /&gt;command:&lt;br&gt;&lt;br /&gt;CD C:\TYJtests&lt;br&gt;&lt;br /&gt;Once you've changed to the right directory, use the javac command as follows, &lt;br /&gt;with the name of the&lt;br&gt;&lt;br /&gt;file as you saved it in Windows (javac stands for Java compiler). Note that you &lt;br /&gt;have to make sure you&lt;br&gt;&lt;br /&gt;type all the same upper- and lowercase here as well:&lt;br&gt;&lt;br /&gt;javac HelloWorld.java&lt;br&gt;&lt;br /&gt;Note&lt;br&gt;&lt;br /&gt;The reason that I've emphasized using the original filename is that&lt;br&gt;&lt;br /&gt;once you're inside the DOS shell, you might notice that your nice&lt;br&gt;&lt;br /&gt;long filenames have been truncated to old-style 8.3 names and that, in&lt;br&gt;&lt;br /&gt;fact, HelloWorld.java actually shows up as HELLOW~1.jav.&lt;br&gt;&lt;br /&gt;Don't panic; this is simply a side effect of Windows 95 and how it&lt;br&gt;&lt;br /&gt;manages long filenames. Ignore the fact that the file appears to be&lt;br&gt;&lt;br /&gt;HELLOW~1.jav and just use the filename you originally used when you saved the &lt;br /&gt;file.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;Figure 1.4 shows what I've done in the DOS shell so you can make sure you're &lt;br /&gt;following along.&lt;br&gt;&lt;br /&gt;Figure 1.4 : Compiling Java in the DOS shell.&lt;br&gt;&lt;br /&gt;If all goes well, you'll end up with a file called HelloWorld.class (or at least &lt;br /&gt;that's what it'll be&lt;br&gt;&lt;br /&gt;called if you look at it outside the DOS shell; from inside DOS its called &lt;br /&gt;HELLOW~1.cla). That's your&lt;br&gt;&lt;br /&gt;Java bytecode file. If you get any errors, go back to your original source file &lt;br /&gt;and make sure you typed it&lt;br&gt;&lt;br /&gt;exactly as it appears in Listing 1.1 with the same upper- and lowercase. Also &lt;br /&gt;make sure the filename has&lt;br&gt;&lt;br /&gt;exactly the same upper- and lowercase as the name of the class (that is, both &lt;br /&gt;should be HelloWorld).&lt;br&gt;&lt;br /&gt;Once you have a class file, you can run that file using the Java bytecode &lt;br /&gt;interpreter. The Java interpreter&lt;br&gt;&lt;br /&gt;is called simply java, and you run it from the DOS shell as you did javac. Run &lt;br /&gt;your Hello World&lt;br&gt;&lt;br /&gt;program like this from the command line, with all the same upper- and lowercase &lt;br /&gt;(and note that the&lt;br&gt;&lt;br /&gt;argument to the java program does not have a .class extension):&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;java HelloWorld&lt;br&gt;&lt;br /&gt;If your program was typed and compiled correctly, you should get the phrase &lt;br /&gt;Hello World! printed&lt;br&gt;&lt;br /&gt;to your screen as a response. Figure 1.5 shows how I did it.&lt;br&gt;&lt;br /&gt;Figure 1.5 : Running Java applications in the DOS shell.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;Note&lt;br&gt;&lt;br /&gt;Remember, the Java compiler and the Java interpreter are different&lt;br&gt;&lt;br /&gt;things. You use the Java compiler (javac) for your Java source files&lt;br&gt;&lt;br /&gt;to create .class files, and you use the Java interpreter (java) to&lt;br&gt;&lt;br /&gt;actually run your class files.&lt;br&gt;&lt;br /&gt;Macintosh&lt;br&gt;&lt;br /&gt;The JDK for the Mac comes with an application called Java&lt;br&gt;&lt;br /&gt;Compiler. To compile your Java source file, simply drag and drop it&lt;br&gt;&lt;br /&gt;on top of the Java Compiler icon. The program will compile your&lt;br&gt;&lt;br /&gt;Java file and, if there are no errors, create a file called&lt;br&gt;&lt;br /&gt;HelloWorld.class in the same folder as your original source&lt;br&gt;&lt;br /&gt;file.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;Tip&lt;br&gt;&lt;br /&gt;Putting an alias for Java Compiler on the desktop makes it easy to&lt;br&gt;&lt;br /&gt;drag and drop Java source files.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;If you get any errors, go back to your original source file and make sure you &lt;br /&gt;typed it exactly as it appears&lt;br&gt;&lt;br /&gt;in Listing 1.1, with the same upper- and lowercase. Also make sure the filename &lt;br /&gt;has exactly the same&lt;br&gt;&lt;br /&gt;upper- and lowercase as the name of the class (that is, both should be &lt;br /&gt;HelloWorld).&lt;br&gt;&lt;br /&gt;Once you've successfully generated a HelloWorld.class file, simply double-click &lt;br /&gt;it to run it. The&lt;br&gt;&lt;br /&gt;application Java Runner, part of the Mac JDK, will start, and the program will &lt;br /&gt;ask you for command-line&lt;br&gt;&lt;br /&gt;arguments. Leave that screen blank and click OK. A window labeled stdout will &lt;br /&gt;appear with the&lt;br&gt;&lt;br /&gt;message Hello World!. Figure 1.6 shows that window.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;Figure 1.6 : Running Java applications on the Mac using Java Runner.&lt;br&gt;&lt;br /&gt;That's it! Keep in mind as you work that you use the Java Compiler application &lt;br /&gt;to compile your .java&lt;br&gt;&lt;br /&gt;files into .class files, which you can then run using Java Runner.&lt;br&gt;&lt;br /&gt;To compile the Java source file in Solaris, you'll use the command-line Java &lt;br /&gt;compiler that comes with the&lt;br&gt;&lt;br /&gt;JDK. From a UNIX command line, cd to the directory that contains your Java &lt;br /&gt;source file. I put mine in&lt;br&gt;&lt;br /&gt;the directory TYJtests, so to change directories I'd use this command:&lt;br&gt;&lt;br /&gt;cd ~/TYJtests&lt;br&gt;&lt;br /&gt;Once you're in the right directory, use the javac command with the name of the &lt;br /&gt;file, like this:&lt;br&gt;&lt;br /&gt;javac HelloWorld.java&lt;br&gt;&lt;br /&gt;If all goes well, you'll end up with a file called HelloWorld.class in the same &lt;br /&gt;directory as your&lt;br&gt;&lt;br /&gt;source file. That's your Java bytecode file. If you get any errors, go back to &lt;br /&gt;your original source file and&lt;br&gt;&lt;br /&gt;make sure you typed it exactly as it appears in Listing 1.1, with the same &lt;br /&gt;upper- and lowercase letters.&lt;br&gt;&lt;br /&gt;Also make sure the filename has exactly the same upper- and lowercase letters as &lt;br /&gt;the name of the class&lt;br&gt;&lt;br /&gt;(that is, both should be HelloWorld).&lt;br&gt;&lt;br /&gt;Once you have a class file, you can run that file using the Java bytecode &lt;br /&gt;interpreter. The Java interpreter&lt;br&gt;&lt;br /&gt;is called simply java, and you run it from the command line as you did javac, &lt;br /&gt;like this (and note that&lt;br&gt;&lt;br /&gt;the argument to the java program does not have a .class extension):&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;java HelloWorld&lt;br&gt;&lt;br /&gt;If your program was typed and compiled correctly, you should get the phrase &lt;br /&gt;Hello World! printed&lt;br&gt;&lt;br /&gt;to your screen as a response. Figure 1.7 shows a listing of all the commands I &lt;br /&gt;used to get to this point&lt;br&gt;&lt;br /&gt;(the part with [desire]~[1] is my system prompt).&lt;br&gt;&lt;br /&gt;Figure 1.7 : Compiling and running a Java application on Solaris.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;Note&lt;br&gt;&lt;br /&gt;Remember that the Java compiler and the Java interpreter are&lt;br&gt;&lt;br /&gt;different things. You use the Java compiler (javac) for your Java&lt;br&gt;&lt;br /&gt;source files to create .class files, and you use the Java interpreter&lt;br&gt;&lt;br /&gt;(java) to actually run your class files.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;Creating a Java Applet&lt;br&gt;&lt;br /&gt;Creating applets is different from creating a simple application. Java applets &lt;br /&gt;run and are displayed inside&lt;br&gt;&lt;br /&gt;a Web page with other page elements, and therefore have special rules for how &lt;br /&gt;they behave. Because of&lt;br&gt;&lt;br /&gt;these special rules for applets, creating an applet may in many cases be more &lt;br /&gt;complex than creating an&lt;br&gt;&lt;br /&gt;application.&lt;br&gt;&lt;br /&gt;For example, to create a simple Hello World applet, instead of merely being able &lt;br /&gt;to print a message as a&lt;br&gt;&lt;br /&gt;set of characters, you have to make space for your message on the Web pages and &lt;br /&gt;then use special font&lt;br&gt;&lt;br /&gt;and graphics operations to paint the message to the screen.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;Note&lt;br&gt;&lt;br /&gt;Actually, you can run a plain Java application as an applet, but the&lt;br&gt;&lt;br /&gt;Hello World message will print to a special window or to a log&lt;br&gt;&lt;br /&gt;file, depending on how the browser has its output set up. You'll learn&lt;br&gt;&lt;br /&gt;more about this next week.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;Creating the Source File&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;In this example, you'll create a simple Hello World applet, place it inside a &lt;br /&gt;Web page, and view the&lt;br&gt;&lt;br /&gt;result. As with the Hello World application, you'll first create the source file &lt;br /&gt;in a plain text editor. Listing&lt;br&gt;&lt;br /&gt;1.2 shows the code for the example.&lt;br&gt;&lt;br /&gt;Listing 1.2. The Hello World applet.&lt;br&gt;&lt;br /&gt;1: import java.awt.Graphics;&lt;br&gt;&lt;br /&gt;2:&lt;br&gt;&lt;br /&gt;3: public class HelloWorldApplet extends java.applet.Applet {&lt;br&gt;&lt;br /&gt;4:&lt;br&gt;&lt;br /&gt;5: public void paint(Graphics g) {&lt;br&gt;&lt;br /&gt;6: g.drawString(&amp;quot;Hello world!&amp;quot;, 5, 25);&lt;br&gt;&lt;br /&gt;7: }&lt;br&gt;&lt;br /&gt;8:}&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;Save that file just as you did the Hello World application, with the filename &lt;br /&gt;exactly the same as the name&lt;br&gt;&lt;br /&gt;of the class. In this case the class name is HelloWorldApplet, so the filename &lt;br /&gt;you save it to would&lt;br&gt;&lt;br /&gt;be HelloWorldApplet.java. As with the application, I put the file in a directory &lt;br /&gt;called TYJch01,&lt;br&gt;&lt;br /&gt;but you can save it anywhere you like.&lt;br&gt;&lt;br /&gt;Compiling the Source File&lt;br&gt;&lt;br /&gt;The next step is to compile the Java applet file. Despite the fact that this is &lt;br /&gt;an applet, you compile the file&lt;br&gt;&lt;br /&gt;exactly the same way you did the Java application, using one of the following &lt;br /&gt;procedures:&lt;br&gt;&lt;br /&gt;javac HelloWorldApplet.java&lt;br&gt;&lt;br /&gt;javac HelloWorldApplet.java&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;Windows&lt;br&gt;&lt;br /&gt;From inside a DOS shell, cd to the directory containing your applet&lt;br&gt;&lt;br /&gt;source file, and use the javac command to compile it (watch those&lt;br&gt;&lt;br /&gt;upper- and lowercase letters):&lt;br&gt;&lt;br /&gt;Macintosh&lt;br&gt;&lt;br /&gt;Drag and drop the HelloWorldApplet.java file onto the Java&lt;br&gt;&lt;br /&gt;Compiler icon.&lt;br&gt;&lt;br /&gt;Salaris&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;From a command line, cd to the directory containing your applet&lt;br&gt;&lt;br /&gt;source file and use the javac command to compile it:&lt;br&gt;&lt;br /&gt;Including the Applet in a Web Page&lt;br&gt;&lt;br /&gt;If you've typed the file correctly, you should end up with a file called &lt;br /&gt;HelloWorldApplet.class in&lt;br&gt;&lt;br /&gt;the same directory as your source file. That's your Java applet file; to have &lt;br /&gt;the applet run inside a Web&lt;br&gt;&lt;br /&gt;page you must refer to that class file inside the HTML code for that page using &lt;br /&gt;the &amp;lt;APPLET&amp;gt; tag.&lt;br&gt;&lt;br /&gt;Listing 1.3 shows a simple HTML file you can use.&lt;br&gt;&lt;br /&gt;Listing 1.3. The HTML with the applet in it.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;1: &amp;lt;HTML&amp;gt;&lt;br&gt;&lt;br /&gt;2: &amp;lt;HEAD&amp;gt;&lt;br&gt;&lt;br /&gt;3: &amp;lt;TITLE&amp;gt;Hello to Everyone!&amp;lt;/TITLE&amp;gt;&lt;br&gt;&lt;br /&gt;4: &amp;lt;/HEAD&amp;gt;&amp;lt;BODY&amp;gt;&lt;br&gt;&lt;br /&gt;5: &amp;lt;P&amp;gt;My Java applet says:&lt;br&gt;&lt;br /&gt;6: &amp;lt;APPLET CODE=&amp;quot;HelloWorldApplet.class&amp;quot; WIDTH=150 HEIGHT=25&amp;gt;&lt;br&gt;&lt;br /&gt;7: &amp;lt;/APPLET&amp;gt;&lt;br&gt;&lt;br /&gt;8: &amp;lt;/BODY&amp;gt;&lt;br&gt;&lt;br /&gt;9: &amp;lt;/HTML&amp;gt;&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;You'll learn more about &amp;lt;APPLET&amp;gt; later in this book, but here are two things to &lt;br /&gt;note about it:&lt;br&gt;&lt;br /&gt;Use the CODE attribute to indicate the name of the class that contains your &lt;br /&gt;applet, here&lt;br&gt;&lt;br /&gt;HelloWorldApplet.Class.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;Use the WIDTH and HEIGHT attributes to indicate the size of the applet on the &lt;br /&gt;page. The browser&lt;br&gt;&lt;br /&gt;uses these values to know how big a chunk of space to leave for the applet on &lt;br /&gt;the page. Here, a&lt;br&gt;&lt;br /&gt;box 150 pixels wide and 25 pixels high is created.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;Save the HTML file in the same directory as your class file, with a descriptive &lt;br /&gt;name and an .html&lt;br&gt;&lt;br /&gt;extension (for example, you might name your HTML file the same name as your&lt;br&gt;&lt;br /&gt;applet-HelloWorldApplet.html).&lt;br&gt;&lt;br /&gt;Note&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;As mentioned earlier with the Java source files, your text editor may&lt;br&gt;&lt;br /&gt;insist on naming your HTML files with a .txt extension if Windows&lt;br&gt;&lt;br /&gt;does not understand what the .html extension is used for. Select&lt;br&gt;&lt;br /&gt;View|Options|File Types from any Windows Explorer window to add&lt;br&gt;&lt;br /&gt;a new file type for HTML files to solve this problem.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;Now you're ready for the final test-actually viewing the result of running your &lt;br /&gt;applet. To view the applet,&lt;br&gt;&lt;br /&gt;you need one of the following:&lt;br&gt;&lt;br /&gt;A browser that supports Java applets, such as Netscape 2.0 or Internet Explorer &lt;br /&gt;3.0. If you're&lt;br&gt;&lt;br /&gt;running on the Macintosh, you'll need Netscape 3.0 or later. If you're running &lt;br /&gt;on Windows 95 or&lt;br&gt;&lt;br /&gt;NT, you'll need the 32-bit version of Netscape. And if you're using Internet &lt;br /&gt;Explorer, you'll need&lt;br&gt;&lt;br /&gt;the 3.0 beta 5 or later (the final version will do just fine).&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;The appletviewer application, which is part of the JDK. The appletviewer is not &lt;br /&gt;a Web&lt;br&gt;&lt;br /&gt;browser and won't let you to see the entire Web page, but it's acceptable for &lt;br /&gt;testing to see how an&lt;br&gt;&lt;br /&gt;applet will look and behave if there is nothing else available.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;An applet viewer or runner tool that comes with your development environment.&lt;br&gt;&lt;br /&gt;If you're using a Java-enabled browser such as Netscape to view your applet &lt;br /&gt;files, you can use the Open&lt;br&gt;&lt;br /&gt;File... item under the File menu to navigate to the HTML file containing the &lt;br /&gt;applet (make sure you open&lt;br&gt;&lt;br /&gt;the HTML file and not the class file). In Internet Explorer, select File|Open &lt;br /&gt;and then Browse to find the&lt;br&gt;&lt;br /&gt;file on your disk. You don't need to install anything on a Web server yet; all &lt;br /&gt;this works on your local&lt;br&gt;&lt;br /&gt;system. Note that the Java applet may take a while to start up after the page &lt;br /&gt;appears to be done loading;&lt;br&gt;&lt;br /&gt;be patient. Figure 1.8 shows the result of running the applet in Netscape.&lt;br&gt;&lt;br /&gt;Figure 1.8 : The applet running in Netscape.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;If you don't have a Web browser with Java capabilities built into it, you can &lt;br /&gt;use the JDK's&lt;br&gt;&lt;br /&gt;appletviewer program to view your Java applet.&lt;br&gt;&lt;br /&gt;appletviewer HTML/HelloWorldApplet.html&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;Windows or Solaris&lt;br&gt;&lt;br /&gt;To run the appletviewer in Windows or Solaris versions of the JDK, cd to the &lt;br /&gt;directory where your HTML and class files are contained and use the appletviewer &lt;br /&gt;command with the name ofthe HTML file you just created:&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;The appletviewer will show you only the applet itself, not the HTML text around &lt;br /&gt;the applet.&lt;br&gt;&lt;br /&gt;Although the appletviewer is a good way to do simple tests of Java applets, it's &lt;br /&gt;a better idea to get a&lt;br&gt;&lt;br /&gt;Java-enabled browser so that you can see your applet on its page in its full &lt;br /&gt;glory.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;Troubleshooting&lt;br&gt;&lt;br /&gt;If you've run into any problems with the previous examples, this section can &lt;br /&gt;help. Here are some of the&lt;br&gt;&lt;br /&gt;most common problems and how to fix them:&lt;br&gt;&lt;br /&gt;Bad command or filename or Command not found&lt;br&gt;&lt;br /&gt;These errors result when you do not have the JDK's bin directory in your &lt;br /&gt;execution path, or the&lt;br&gt;&lt;br /&gt;path to that directory is wrong. On Windows, double-check your autoexec.bat &lt;br /&gt;file; on UNIX,&lt;br&gt;&lt;br /&gt;check the system file with your path commands in it (.cshrc, .login, .profile, &lt;br /&gt;or some&lt;br&gt;&lt;br /&gt;similar file).&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;javac: invalid argument&lt;br&gt;&lt;br /&gt;Make sure the name of the file you're giving to the javac command is exactly the &lt;br /&gt;same name as&lt;br&gt;&lt;br /&gt;the file. In particular, in the DOS shell you want to use the Windows filename &lt;br /&gt;with a .java&lt;br&gt;&lt;br /&gt;extension, not the DOS equivalent (HELLOW~1.jav, for example).&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;Warning: public class HelloWorldApplet must be defined in a file&lt;br&gt;&lt;br /&gt;called HelloWorldApplet.java&lt;br&gt;&lt;br /&gt;This error most often happens if there is a mismatch between the name of the &lt;br /&gt;class as defined in&lt;br&gt;&lt;br /&gt;the Java file itself (the name following the word class) and the name of the &lt;br /&gt;java source file. Both&lt;br&gt;&lt;br /&gt;the filenames must match, including upper- and lowercase letters (this &lt;br /&gt;particular error implies that&lt;br&gt;&lt;br /&gt;the filename had lowercase letters). Rename either the filename or the class &lt;br /&gt;name, and this error&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;will go away.&lt;br&gt;&lt;br /&gt;Insufficient-memory errors&lt;br&gt;&lt;br /&gt;The JDK is not the most efficient user of memory. If you're getting errors about &lt;br /&gt;memory, consider&lt;br&gt;&lt;br /&gt;closing larger programs before running Java compiles, turn on virtual memory, or &lt;br /&gt;install more RAM.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;Other code errors&lt;br&gt;&lt;br /&gt;If you're unable to compile the Java source files because of other errors I &lt;br /&gt;haven't mentioned here,&lt;br&gt;&lt;br /&gt;be sure that you've typed them in exactly as they appear, including all upper- &lt;br /&gt;and lowercase letters.&lt;br&gt;&lt;br /&gt;Java is case sensitive, meaning that upper- and lowercase letters are treated &lt;br /&gt;differently, so you will&lt;br&gt;&lt;br /&gt;need to make sure that everything is capitalized correctly. If all else fails, &lt;br /&gt;try comparing your&lt;br&gt;&lt;br /&gt;source files to the files on the CD-ROM.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;Summary&lt;br&gt;&lt;br /&gt;Today you've gotten a basic introduction to the Java language and its goals and &lt;br /&gt;features. Java is a&lt;br&gt;&lt;br /&gt;programming language, similar to C or C++, in which you can develop a wide range &lt;br /&gt;of programs. The&lt;br&gt;&lt;br /&gt;most common use of Java at the moment is in creating applets for HotJava, an &lt;br /&gt;advanced World Wide&lt;br&gt;&lt;br /&gt;Web browser also written in Java. Applets are Java programs that are downloaded &lt;br /&gt;and run as part of a&lt;br&gt;&lt;br /&gt;Web page. Applets can create animation, games, interactive programs, and other &lt;br /&gt;multimedia effects on&lt;br&gt;&lt;br /&gt;Web pages.&lt;br&gt;&lt;br /&gt;Java's strengths lie in its portability-both at the source and at the binary &lt;br /&gt;level, in its object-oriented&lt;br&gt;&lt;br /&gt;design-and in its simplicity. Each of these features helps make applets &lt;br /&gt;possible, but they also make Java&lt;br&gt;&lt;br /&gt;an excellent language for writing more general-purpose programs that do not &lt;br /&gt;require a Java-enabled&lt;br&gt;&lt;br /&gt;browser to run. These general-purpose Java programs are called applications.&lt;br&gt;&lt;br /&gt;To end this day, you experimented with an example of an applet and an example of &lt;br /&gt;an application,&lt;br&gt;&lt;br /&gt;getting a feel for the differences between the two and how to create, compile, &lt;br /&gt;and run Java programs-or,&lt;br&gt;&lt;br /&gt;in the case of applets, how to include them in Web pages. From here, you now &lt;br /&gt;have the foundation to&lt;br&gt;&lt;br /&gt;create more complex applications and applets. Onward to Day 2, &amp;quot;Object-Oriented &lt;br /&gt;Programming and&lt;br&gt;&lt;br /&gt;Java&amp;quot;!&lt;br&gt;&lt;br /&gt;Q&amp;amp;A&lt;br&gt;&lt;br /&gt;Q: I know a lot about HTML, but not much about computer programming. Can I still &lt;br /&gt;write&lt;br&gt;&lt;br /&gt;Java programs?&lt;br&gt;&lt;br /&gt;A: If you have no programming experience whatsoever, you most likely will find &lt;br /&gt;programming&lt;br&gt;&lt;br /&gt;Java significantly more difficult than HTML. However, Java is an excellent &lt;br /&gt;language to learn&lt;br&gt;&lt;br /&gt;programming with, and if you patiently work through the examples and the &lt;br /&gt;exercises in this&lt;br&gt;&lt;br /&gt;book, you should be able to learn enough to get started with Java.&lt;br&gt;&lt;br /&gt;Q: What's the relationship between JavaScript and Java?&lt;br&gt;&lt;br /&gt;A: They have the same first four letters.&lt;br&gt;&lt;br /&gt;A common misconception in the Web world today is that Java and JavaScript have &lt;br /&gt;more in&lt;br&gt;&lt;br /&gt;common than they actually do. Java is the general-purpose programming language &lt;br /&gt;that you'll&lt;br&gt;&lt;br /&gt;learn about in this book; you use it to create applets. JavaScript is a &lt;br /&gt;Netscape-invented scripting&lt;br&gt;&lt;br /&gt;language that looks sort of like Java; with it you can do various nifty things &lt;br /&gt;in Web pages. They&lt;br&gt;&lt;br /&gt;are independent languages, used for different purposes. If you're interested in &lt;br /&gt;JavaScript&lt;br&gt;&lt;br /&gt;programming, you'll want to pick up another book, such as Teach Yourself &lt;br /&gt;JavaScript in a Week&lt;br&gt;&lt;br /&gt;or Laura Lemay's Web Workshop: JavaScript, both also available from Sams.net &lt;br /&gt;Publishing.&lt;br&gt;&lt;br /&gt;Q: According to today's lesson, Java applets are downloaded via a Java-enabled &lt;br /&gt;browser such&lt;br&gt;&lt;br /&gt;as Netscape and run on the reader's system. Isn't that an enormous security &lt;br /&gt;hole? What&lt;br&gt;&lt;br /&gt;stops someone from writing an applet that compromises the security of my &lt;br /&gt;system-or&lt;br&gt;&lt;br /&gt;worse, that damages my system?&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;A: Sun's Java team has thought a great deal about the security of applets within &lt;br /&gt;Java-enabled&lt;br&gt;&lt;br /&gt;browsers and has implemented several checks to make sure applets cannot do nasty &lt;br /&gt;things:&lt;br&gt;&lt;br /&gt;Java applets cannot read or write to the disk on the local system.&lt;br&gt;&lt;br /&gt;Java applets cannot execute any programs on the local system.&lt;br&gt;&lt;br /&gt;Java applets cannot connect to any machines on the Web except for the server &lt;br /&gt;from which&lt;br&gt;&lt;br /&gt;they are originally downloaded.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;Note that some of these restrictions may be allowed in some browsers or may be &lt;br /&gt;turned on in the&lt;br&gt;&lt;br /&gt;browser configuration. However, you cannot expect any of these capabilities to &lt;br /&gt;be available.&lt;br&gt;&lt;br /&gt;In addition, the Java compiler and interpreter check both the Java source code &lt;br /&gt;and the Java&lt;br&gt;&lt;br /&gt;bytecodes to make sure that the Java programmer has not tried any sneaky tricks &lt;br /&gt;(for example,&lt;br&gt;&lt;br /&gt;overrunning buffers or stack frames).&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;These checks obviously cannot stop every potential security hole (no system can &lt;br /&gt;promise that!),&lt;br&gt;&lt;br /&gt;but they can significantly reduce the potential for hostile applets. You'll &lt;br /&gt;learn more about&lt;br&gt;&lt;br /&gt;security issues for applets on Day 8, &amp;quot;Java Applet Basics,&amp;quot; and in greater &lt;br /&gt;detail on Day 21,&lt;br&gt;&lt;br /&gt;&amp;quot;Under the Hood.&amp;quot;&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;Q: I followed all the directions you gave for creating a Java applet. I loaded &lt;br /&gt;it into HotJava,but Hello World didn't show up. What did I do wrong?&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;A: Don't use HotJava to view applets you've created in this book; get a more &lt;br /&gt;up-to-date browsersuch as Netscape or Internet Explorer. HotJava was an &lt;br /&gt;experimental browser and has not beenupdated since soon after its original &lt;br /&gt;release. The steps you take to define and write an applethave changed since &lt;br /&gt;then, and the applets you write now will not run on HotJava.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;Q: You've mentioned Solaris, Windows, and Macintosh in this chapter. What about &lt;br /&gt;otheroperating systems?&lt;br&gt;&lt;br /&gt;A: If you use a flavor of UNIX other than Solaris, chances are good that the JDK &lt;br /&gt;has been ported to your system. Here are some examples:&lt;br&gt;&lt;br /&gt;SGI's version of the JDK can be found at&lt;br&gt;&lt;br /&gt;http://www.sgi.com/Products/cosmo/cosmo_instructions.html.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;Information about Java for Linux can be found at&lt;br&gt;&lt;br /&gt;http://www.blackdown.org/java-linux/.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;IBM has ported the JDK to OS/2 and AIX. Find out more from&lt;br&gt;&lt;br /&gt;http://www.ncc.hurley.ibm.com/javainfo/.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;OSF is porting the JDK to HP/UX, Unixware, Sony NEWS, and Digital UNIX. See&lt;br&gt;&lt;br /&gt;http://www.osf.org/mall/web/javaport.htm.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;(Thanks to Elliote Rusty Harold's Java FAQ at &lt;br /&gt;http://www.sunsite.unc.edu/javafaq/javafaq/html for this information.)&lt;br&gt;&lt;br /&gt;Q: Why doesn't Java run on Windows 3.1?&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;A: Technical limitations in Windows 3.1 make porting Java to Windows 3.1 &lt;br /&gt;particularly difficult.Rumor has it that both IBM and Microsoft are working on &lt;br /&gt;ports, but no real information is forthcoming.&lt;br&gt;&lt;br /&gt;Q: I'm using Notepad on Windows to edit my Java files. The program insists on &lt;br /&gt;adding a.txt extension to all my files, regardless of what I name them (so I &lt;br /&gt;always end up with files like HelloWorld.java.txt). Short of renaming them &lt;br /&gt;before I compile them, what else can I do to fix this?&lt;br&gt;&lt;br /&gt;A: Although you can rename the files just before you compile them, that can get &lt;br /&gt;to be a pain, particularly when you have a lot of files. The problem here is &lt;br /&gt;that Windows doesn't understand the .java extension (you may also have this &lt;br /&gt;problem with HTML's .html extension as well). To fix this, go into any Windows &lt;br /&gt;Explorer window and select View|Options|File Types. Fromthat panel, select New &lt;br /&gt;Type. Enter Java Source Files in the Description of Type box and .java into the &lt;br /&gt;Associated Extension box. Then click OK. Do the same with HTML files if you need &lt;br /&gt;to, and click OK again. You should now be able to use Notepad (or any other text &lt;br /&gt;editor) to create and save Java and HTML files.&lt;br&gt;&lt;br /&gt;Q: Where can I learn more about Java and find applets and applications to play &lt;br /&gt;with?&lt;br&gt;&lt;br /&gt;A: You can read the rest of this book! Here are some other places to look for &lt;br /&gt;Java information and Java applets:&lt;br&gt;&lt;br /&gt;The Java home page at http://www.java.sun.com/ is the official source for Java &lt;br /&gt;information, including information about the JDK, about the upcoming 1.1 &lt;br /&gt;release, and about developer tools such as the Java Workshop, as well as &lt;br /&gt;extensive documentation. &lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;Gamelan, at http://www.gamelan.com/, is a repository of applets and Java&lt;br&gt;&lt;br /&gt;information, organized into categories. If you want to play with applets or &lt;br /&gt;applications, this is the place to look.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;For Java discussion, check out the comp.lang.java newsgroups, including&lt;br&gt;&lt;br /&gt;comp.lang.java.programmer, comp.lang.java.tech,&lt;br&gt;&lt;br /&gt;comp.lang.java.advocacy, and so on. (You'll need a Usenet newsreader to access&lt;br&gt;&lt;br /&gt;&amp;nbsp;&lt;/p&gt;</description><thr:total xmlns:thr="http://purl.org/syndication/thread/1.0">1</thr:total></item><item><title>C Language  Help, C Language Tutorials, C Language Programming, C Language Tricks  { The C# Language }</title><link>http://swebsolution.blogspot.com/2007/12/c-language-xml-document-c-language.html</link><author>noreply@blogger.com (Unknown)</author><pubDate>Wed, 26 Dec 2007 04:09:00 -0800</pubDate><guid isPermaLink="false">tag:blogger.com,1999:blog-7227425366482465228.post-1641704964408031362</guid><description>&lt;p align="justify"&gt;&lt;b&gt;C Language  Help, C Language Tutorials, C Language Programming, C Language Tricks &lt;/b&gt; &lt;br&gt;XML Documentation&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;&lt;br /&gt;XML is used extensively to document program code. Apart from its status of a &lt;br /&gt;mere acronym for eXtensible Markup Language, XML has a bounden duty to achieve &lt;br /&gt;miracles. However, in this chapter, we would be utilizing XML for the temporal &lt;br /&gt;task of documenting our code. &lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;a.cs&lt;br&gt;&lt;br /&gt;class zzz&lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;public static void Main() {&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;Run the compiler as &lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;&amp;gt;csc a.cs /doc:a.xml&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;a.xml&lt;br&gt;&lt;br /&gt;&amp;lt;?xml version=&amp;quot;1.0&amp;quot;?&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;doc&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;assembly&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;name&amp;gt;a&amp;lt;/name&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;/assembly&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;members&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;/members&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;/doc&amp;gt;&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;This is the smallest possible XML file that can be effectively generated by the &lt;br /&gt;compiler. The compiler with the /doc option, followed by a colon and filename, &lt;br /&gt;creates the XML file, which documents the C# program. &lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;Anything placed within the &amp;lt;&amp;gt; brackets is called a tag. At the expense of &lt;br /&gt;sounding tediously repetitive, we once again reiterate that, each and every XML &lt;br /&gt;file must necessarily start with a tag beginning with &amp;lt;?xml&amp;gt;. This has to be &lt;br /&gt;followed by an attribute called version. Presently, the only permissible value &lt;br /&gt;that can be assigned to version is 1.0. There may never be a version 1.1 or 2.0.&lt;br /&gt;&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;Subsequent to this, a root tag called 'doc' is specified, which envelopes all &lt;br /&gt;other tags within the file. We have a tag called 'assembly' which owns another &lt;br /&gt;tag called 'name' that encloses our assembly name 'a'. The name of our program &lt;br /&gt;file is 'a'. There are no members to be documented, therefore, no names are &lt;br /&gt;noticeable within the member tags. This is the basic structure of our XML file.&lt;br /&gt;&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;It is the bare minimum, no frills structure, created by the compiler. Now, let &lt;br /&gt;us refurbish and garnish it through our own contribution.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;a.cs&lt;br&gt;&lt;br /&gt;class zzz&lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;/// &amp;lt;vijay&amp;gt;&lt;br&gt;&lt;br /&gt;public static void Main() &lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;We have added an XML tag in the .cs file. The documentation clearly states that &lt;br /&gt;an XML tag in a .cs file cannot begin with two slashes, since they are reserved &lt;br /&gt;for comments. It should instead have three slashes. The use of the two slashes &lt;br /&gt;generates the following warning: &lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;Output&lt;br&gt;&lt;br /&gt;a.cs(4,3): warning CS1570: XML comment on 'zzz.Main()' has badly formed XML -- &lt;br /&gt;'End tag 'member' does not match the start tag 'vijay'.' &lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;From now on, we shall only display the pertinent portions of the XML file, while &lt;br /&gt;ignoring the portion that is invariant, such as, the doc and the /doc tags. So &lt;br /&gt;in the file created as: &lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;a.xml&lt;br&gt;&lt;br /&gt;&amp;lt;?xml version=&amp;quot;1.0&amp;quot;?&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;doc&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;assembly&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;name&amp;gt;a&amp;lt;/name&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;/assembly&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;members&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;!-- Badly formed XML comment ignored for member &amp;quot;M:zzz.Main&amp;quot; --&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;/members&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;/doc&amp;gt;&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;we will display only &lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;xml fragment:&lt;br&gt;&lt;br /&gt;&amp;lt;members&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;!-- Badly formed XML comment ignored for member &amp;quot;M:zzz.Main&amp;quot; --&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;/members&amp;gt;&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;In the next program, a closing tag of 'vijay' is specified.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;a.cs&lt;br&gt;&lt;br /&gt;class zzz&lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;/// &amp;lt;vijay&amp;gt;&lt;br&gt;&lt;br /&gt;/// Function Main gets called first&lt;br&gt;&lt;br /&gt;/// &amp;lt;/vijay&amp;gt;&lt;br&gt;&lt;br /&gt;public static void Main() &lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;xml &lt;br&gt;&lt;br /&gt;&amp;lt;members&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;member name=&amp;quot;M:zzz.Main&amp;quot;&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;vijay&amp;gt;&lt;br&gt;&lt;br /&gt;Function Main gets called first&lt;br&gt;&lt;br /&gt;&amp;lt;/vijay&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;/member&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;/members&amp;gt;&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;The earlier program generated an error, since there was no corresponding end tag &lt;br /&gt;to the tag 'vijay'. &lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;Every tag in XML must necessarily have a start tag and an end tag. We are at &lt;br /&gt;liberty to choose any name for the tag. Therefore, we have chosen the name &lt;br /&gt;'vijay'. &lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;The compiler creates a tag called 'member' under the tag members, and adds an &lt;br /&gt;attribute called 'name'. This attribute is initialized to the name of the C# &lt;br /&gt;entity, which is the position where the tag is placed. The value assigned to &lt;br /&gt;name is, as follows:&lt;br&gt;&lt;br /&gt;• The letter M, &lt;br&gt;&lt;br /&gt;• Followed by a colon separator, &lt;br&gt;&lt;br /&gt;• Followed by the name of the class zzz, &lt;br&gt;&lt;br /&gt;• Followed by a dot separator &lt;br&gt;&lt;br /&gt;• Finally, followed by the name of the function i.e. Main. &lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;Thus, it displays M:zzz.Main. &lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;The tag 'vijay' and the text enclosed between the start and end tags of 'vijay' &lt;br /&gt;subsequently, get enclosed within the members tag. &lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;a.cs&lt;br&gt;&lt;br /&gt;/// &amp;lt;mukhi&amp;gt;&lt;br&gt;&lt;br /&gt;/// Our first class&lt;br&gt;&lt;br /&gt;/// &amp;lt;/mukhi&amp;gt;&lt;br&gt;&lt;br /&gt;class zzz&lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;/// &amp;lt;vijay&amp;gt;&lt;br&gt;&lt;br /&gt;/// Function Main gets called first&lt;br&gt;&lt;br /&gt;/// &amp;lt;/vijay&amp;gt;&lt;br&gt;&lt;br /&gt;public static void Main() &lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;xml &lt;br&gt;&lt;br /&gt;&amp;lt;members&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;member name=&amp;quot;T:zzz&amp;quot;&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;mukhi&amp;gt;&lt;br&gt;&lt;br /&gt;Our first class&lt;br&gt;&lt;br /&gt;&amp;lt;/mukhi&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;/member&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;member name=&amp;quot;M:zzz.Main&amp;quot;&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;vijay&amp;gt;&lt;br&gt;&lt;br /&gt;Function Main gets called first&lt;br&gt;&lt;br /&gt;&amp;lt;/vijay&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;/member&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;/members&amp;gt;&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;Now, we have added a tag called 'mukhi' to the class. It is however, not added &lt;br /&gt;to the function. So presently, there are two member tags, and each has been &lt;br /&gt;assigned a name within the members tag. The value assigned to the member name, &lt;br /&gt;which encloses 'mukhi' is, T:zzz. This is created in a manner akin to that of a &lt;br /&gt;function, with the exception of the prefix M, which has now been replaced by T.&lt;br /&gt;&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;The rationale behind introducing XML tags is that, a program known as an XML &lt;br /&gt;Parser can interpret the above XML file, and subsequently, create legible &lt;br /&gt;output. But, the prime impediment that we have to confront and surmount is, the &lt;br /&gt;reluctance of programmers to document code in the first place. &lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;a.cs&lt;br&gt;&lt;br /&gt;class zzz&lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;/// &amp;lt;mukhi&amp;gt;&lt;br&gt;&lt;br /&gt;/// Our Instance Variable&lt;br&gt;&lt;br /&gt;/// &amp;lt;/mukhi&amp;gt;&lt;br&gt;&lt;br /&gt;public int i;&lt;br&gt;&lt;br /&gt;public static void Main() &lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;/// &amp;lt;vijay&amp;gt;&lt;br&gt;&lt;br /&gt;/// Our first variable ii&lt;br&gt;&lt;br /&gt;/// &amp;lt;/vijay&amp;gt;&lt;br&gt;&lt;br /&gt;int ii;&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;Output&lt;br&gt;&lt;br /&gt;a.cs(12,5): warning CS0168: The variable 'ii' is declared but never used&lt;br&gt;&lt;br /&gt;a.cs(9,1): warning CS1587: XML comment is not placed on a valid language element&lt;br&gt;&lt;br /&gt;a.cs(6,12): warning CS0649: Field 'zzz.i' is never assigned to, and will always &lt;br /&gt;have its default value 0&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;xml &lt;br&gt;&lt;br /&gt;&amp;lt;members&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;member name=&amp;quot;F:zzz.i&amp;quot;&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;mukhi&amp;gt;&lt;br&gt;&lt;br /&gt;Our Instance Variable&lt;br&gt;&lt;br /&gt;&amp;lt;/mukhi&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;/member&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;/members&amp;gt;&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;Oops! The tags that we give within a function have not been reflected in the XML &lt;br /&gt;file. The instance variable i is flagged with an F, but the local variable ii is &lt;br /&gt;not reflected in the XML file at all. &lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;We have carried this out on purpose, with the intention of demonstrating that it &lt;br /&gt;is not permissible to write XML tags within functions. Even though our preceding &lt;br /&gt;action would not produce any error, the compiler shall, in any case, ignore our &lt;br /&gt;work of art.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;We will now dwell upon the names or string ids, which the compiler generates for &lt;br /&gt;names of entities. They are as follows:&lt;br&gt;&lt;br /&gt;• N stands for a Namespace.&lt;br&gt;&lt;br /&gt;• T represents a type, class, interface, struct, enum and delegate. &lt;br&gt;&lt;br /&gt;• F denotes a field. &lt;br&gt;&lt;br /&gt;• P is indicative of either properties or indexers. &lt;br&gt;&lt;br /&gt;• M indicates all methods &lt;br&gt;&lt;br /&gt;• E represents an event. &lt;br&gt;&lt;br /&gt;• ! is indicative of an error. &lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;a.cs&lt;br&gt;&lt;br /&gt;/// &amp;lt;vijay&amp;gt;&lt;br&gt;&lt;br /&gt;/// Our first variable ii&lt;br&gt;&lt;br /&gt;/// &amp;lt;/vijay&amp;gt;&lt;br&gt;&lt;br /&gt;namespace aaa&lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;class zzz&lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;public static void Main() &lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;Output&lt;br&gt;&lt;br /&gt;a.cs(1,1): warning CS1587: XML comment is not placed on a valid language element&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;We do not enjoy the license to place tags wherever we desire. Thus, a warning is &lt;br /&gt;issued when a tag is placed on a namespace, although no error is generated. In &lt;br /&gt;the earlier case too, a similar warning was issued, when we had placed the tags &lt;br /&gt;in a function. &lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;a.cs&lt;br&gt;&lt;br /&gt;class zzz&lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;/// &amp;lt;vijay&amp;gt;&lt;br&gt;&lt;br /&gt;/// Our function &amp;lt;c&amp;gt;Main&amp;lt;/c&amp;gt; is the starting point &lt;br&gt;&lt;br /&gt;/// &amp;lt;/vijay&amp;gt;&lt;br&gt;&lt;br /&gt;public static void Main() &lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;xml &lt;br&gt;&lt;br /&gt;&amp;lt;member name=&amp;quot;M:zzz.Main&amp;quot;&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;vijay&amp;gt;&lt;br&gt;&lt;br /&gt;Our function &amp;lt;c&amp;gt;Main&amp;lt;/c&amp;gt; is the starting point &lt;br&gt;&lt;br /&gt;&amp;lt;/vijay&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;/member&amp;gt;&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;If we place the tag &amp;lt;c&amp;gt; and &amp;lt;/c&amp;gt; around some text in a tag, the tag gets copied &lt;br /&gt;verbatim, into the XML file. It is indicative of the fact that the text &lt;br /&gt;represents some code. To mark multiple lines of code, the code tag may be used &lt;br /&gt;instead. The example tag also works in a similar manner. &lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;a.cs&lt;br&gt;&lt;br /&gt;/// &amp;lt;exception cref=&amp;quot;System.Exception&amp;quot;&amp;gt; Our exception class &amp;lt;/exception&amp;gt;&lt;br&gt;&lt;br /&gt;class zzz&lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;public static void Main() &lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;xml &lt;br&gt;&lt;br /&gt;&amp;lt;member name=&amp;quot;T:zzz&amp;quot;&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;exception cref=&amp;quot;T:System.Exception&amp;quot;&amp;gt; Our exception class &amp;lt;/exception&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;/member&amp;gt; &lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;The exception tag works as demonstrated above, with a single difference in that, &lt;br /&gt;the attribute cref is inspected for accuracy. This attribute should represent a &lt;br /&gt;valid C# entity, otherwise, the following warning is displayed: &lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;/// &amp;lt;exception cref=&amp;quot;System.Exceptions&amp;quot;&amp;gt; Our exception class &amp;lt;/exception&amp;gt;&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;Output &lt;br&gt;&lt;br /&gt;a.cs(2,22): warning CS1574: XML comment on 'zzz' has cref attribute &lt;br /&gt;'System.Exceptions' that could not be found.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;xml &lt;br&gt;&lt;br /&gt;&amp;lt;member name=&amp;quot;T:zzz&amp;quot;&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;exception cref=&amp;quot;!:System.Exceptions&amp;quot;&amp;gt; Our exception class &amp;lt;/exception&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;/member&amp;gt;&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;All the tags expounded by us so far, have a similar functionality. This surely &lt;br /&gt;must have kindled your curiosity to discover the rationale behind employing so &lt;br /&gt;many tags. &lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;This is so, because each of these tags, documents different features of the &lt;br /&gt;language. Let us assume that we yearn for a bulleted list in our final &lt;br /&gt;documentation. We would have to use the following tags to accomplish it:&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;a.cs&lt;br&gt;&lt;br /&gt;/// &amp;lt;list type=&amp;quot;bullet&amp;quot;&amp;gt;&lt;br&gt;&lt;br /&gt;/// &amp;lt;item&amp;gt;&lt;br&gt;&lt;br /&gt;/// &amp;lt;description&amp;gt;One&amp;lt;/description&amp;gt;&lt;br&gt;&lt;br /&gt;/// &amp;lt;/item&amp;gt;&lt;br&gt;&lt;br /&gt;/// &amp;lt;item&amp;gt;&lt;br&gt;&lt;br /&gt;/// &amp;lt;description&amp;gt;Two&amp;lt;/description&amp;gt;&lt;br&gt;&lt;br /&gt;/// &amp;lt;/item&amp;gt;&lt;br&gt;&lt;br /&gt;/// &amp;lt;/list&amp;gt;&lt;br&gt;&lt;br /&gt;class zzz&lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;public static void Main() {&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;xml fragment&lt;br&gt;&lt;br /&gt;&amp;lt;member name=&amp;quot;T:zzz&amp;quot;&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;list type=&amp;quot;bullet&amp;quot;&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;item&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;description&amp;gt;One&amp;lt;/description&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;/item&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;item&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;description&amp;gt;Two&amp;lt;/description&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;/item&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;/list&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;/member&amp;gt;&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;The attribute type can also be assigned the values of bullet, number or table.&lt;br&gt;&lt;br /&gt;Bear in mind, that an external program will interpret the XML tags. Thus, the &lt;br /&gt;designers of the language standardized tags for describing different things. If &lt;br /&gt;the same tag had been utilized for documenting every feature, a single program &lt;br /&gt;would have sufficed to document the entire C# code. We have employed tags such &lt;br /&gt;as 'vijay' and 'mukhi' to document our classes. Chaos would reign supreme, if &lt;br /&gt;everyone decided to use his or her own names. Appended below is a brief summary &lt;br /&gt;of the various tags that can be employed in the XML file:&lt;br&gt;&lt;br /&gt;• The param tag is used to document the parameters to a function.&lt;br&gt;&lt;br /&gt;• The paramref tag is used to document a parameter name. &lt;br&gt;&lt;br /&gt;• The permission tag is used to document the access allowed to a member. &lt;br&gt;&lt;br /&gt;• The remarks tag is used to specify the overall information for a class. &lt;br&gt;&lt;br /&gt;• The return tag has been introduced to document the return values of functions.&lt;br /&gt;&lt;br&gt;&lt;br /&gt;• The seealso tag helps in implementing the 'see also' section at the bottom of &lt;br /&gt;help sections. &lt;br&gt;&lt;br /&gt;• The summary tag documents all members of a type. &lt;br&gt;&lt;br /&gt;• The value tag documents all the properties. &lt;/p&gt;</description><thr:total xmlns:thr="http://purl.org/syndication/thread/1.0">0</thr:total></item><item><title>C Language  Help, C Language Tutorials, C Language Programming, C Language Tricks  { The C# Language }</title><link>http://swebsolution.blogspot.com/2007/12/c-language-xml-data-document-c-language.html</link><author>noreply@blogger.com (Unknown)</author><pubDate>Wed, 26 Dec 2007 04:07:00 -0800</pubDate><guid isPermaLink="false">tag:blogger.com,1999:blog-7227425366482465228.post-7446779991260619398</guid><description>&lt;p align="justify"&gt;&lt;b&gt;C Language  Help, C Language Tutorials, C Language Programming, C Language Tricks &lt;/b&gt;&lt;br&gt;XML Data Document&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;a.cs&lt;br&gt;&lt;br /&gt;using System;&lt;br&gt;&lt;br /&gt;using System.IO;&lt;br&gt;&lt;br /&gt;using System.Xml;&lt;br&gt;&lt;br /&gt;public class zzz &lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;public static void Main() &lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;XmlDocument d = new XmlDocument();&lt;br&gt;&lt;br /&gt;XmlTextReader r = new XmlTextReader(&amp;quot;books.xml&amp;quot;);&lt;br&gt;&lt;br /&gt;r.WhitespaceHandling = WhitespaceHandling.None;&lt;br&gt;&lt;br /&gt;//r.MoveToContent();&lt;br&gt;&lt;br /&gt;d.Load(r);&lt;br&gt;&lt;br /&gt;d.Save(Console.Out);&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;books.xml&lt;br&gt;&lt;br /&gt;&amp;lt;?xml version='1.0'?&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;!-- This file represents a fragment of a book store inventory database --&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;bookstore&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;book genre=&amp;quot;autobiography&amp;quot; publicationdate=&amp;quot;1981&amp;quot; ISBN=&amp;quot;1-861003-11-0&amp;quot;&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;title&amp;gt;The Autobiography of Benjamin Franklin&amp;lt;/title&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;author&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;first-name&amp;gt;Benjamin&amp;lt;/first-name&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;last-name&amp;gt;Franklin&amp;lt;/last-name&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;/author&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;price&amp;gt;8.99&amp;lt;/price&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;/book&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;book genre=&amp;quot;novel&amp;quot; publicationdate=&amp;quot;1967&amp;quot; ISBN=&amp;quot;0-201-63361-2&amp;quot;&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;title&amp;gt;The Confidence Man&amp;lt;/title&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;author&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;first-name&amp;gt;Herman&amp;lt;/first-name&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;last-name&amp;gt;Melville&amp;lt;/last-name&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;/author&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;price&amp;gt;11.99&amp;lt;/price&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;/book&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;book genre=&amp;quot;philosophy&amp;quot; publicationdate=&amp;quot;1991&amp;quot; ISBN=&amp;quot;1-861001-57-6&amp;quot;&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;title&amp;gt;The Gorgias&amp;lt;/title&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;author&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;name&amp;gt;Plato&amp;lt;/name&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;/author&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;price&amp;gt;9.99&amp;lt;/price&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;/book&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;/bookstore&amp;gt;&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;Output&lt;br&gt;&lt;br /&gt;&amp;lt;?xml version=&amp;quot;1.0&amp;quot;?&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;!-- This file represents a fragment of a book store inventory database --&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;bookstore&amp;gt;&lt;br&gt;&lt;br /&gt;..&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;The XmlDocument class represents an XML document. The W3C has two standards, one &lt;br /&gt;called the Document Object Model DOM Level 1, and the other called the Core DOM &lt;br /&gt;Level 2. The XmlDocument class implements these standards. This class is derived &lt;br /&gt;from an abstract class called XmlNode.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;The XML Document is loaded into memory as a tree structure, thereby facilitating &lt;br /&gt;the navigation within and the editing of the document. The representation of the &lt;br /&gt;document in memory is known as a DOM. &lt;br&gt;&lt;br /&gt;The XML file books.xml represents a bookstore , which stores a large number of &lt;br /&gt;books. This file shall not be displayed in future since it is pretty lengthy. &lt;br /&gt;This file is utilised by most of the XML samples supplied by Microsoft. &lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;The tag 'books' represents a book, and has attributes such as, type of book, &lt;br /&gt;genre, the year of publication or publication date etc; and finally, a unique &lt;br /&gt;number, ISBN, assigned to the book. Each book has a title as well as an author. &lt;br /&gt;The attribute author contains a few tags that disclose details such as, the &lt;br /&gt;first name and the last name of the author. The price of the book is the last &lt;br /&gt;element. It is not imperative for these elements of a book to be present in a &lt;br /&gt;specific order, i.e. price can come before the title, and so on. &lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;The object r of type XmlTextReader class represents the XML file books.xml. As &lt;br /&gt;always, the WhiteSpaceHandling is set to None. D is an object that looks like &lt;br /&gt;XmlDocument. The Load function of this class can handle four overloads. One of &lt;br /&gt;the overloads that the function can take is an XmlReader. As we are supplying an &lt;br /&gt;XmlTextReader to this function, it eventually scales down to an XmlReader.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;The Load function is responsible for loading the XML file into the XmlDocument &lt;br /&gt;object. If the property PreserveWhitespace is set to True, then and only then, &lt;br /&gt;are the Whitespace nodes created. In this case, since we have set Whitespaces to &lt;br /&gt;None in the XML file, no white space nodes can ever be created, regardless of &lt;br /&gt;property PreserveWhitespace being set to True. The Load function cannot change &lt;br /&gt;the Whitespace Handling present in the reader. The Load method does not perform &lt;br /&gt;any validations. In case validations are imperative, the XmlValidatingReader &lt;br /&gt;class must be utilised in place of the plain Jane XmlTextReader class. The same &lt;br /&gt;argument holds good when entities have to be resolved. &lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;The Save function writes the XML document associated with the XmlDocument, &lt;br /&gt;either to disk or to the console. We have displayed only a section of the &lt;br /&gt;document, since the entire books.xml shall occupy space needlessly. We acquire &lt;br /&gt;the entire file because the ReadState is set to Initial. This is so because the &lt;br /&gt;state has not been changed yet. Let us now position the reader and see what &lt;br /&gt;happens. &lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;We remove the comment marks associated with MoveToContent, thereby, making it &lt;br /&gt;available. As a consequence, both, the XmlDeclaration and the content are &lt;br /&gt;skipped, and the reader is positioned at the first node 'Bookstore'. The output &lt;br /&gt;is as displayed below:&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;Output&lt;br&gt;&lt;br /&gt;&amp;lt;bookstore&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;book genre=&amp;quot;autobiography&amp;quot; publicationdate=&amp;quot;1981&amp;quot; ISBN=&amp;quot;1-861003-11-0&amp;quot;&amp;gt;&lt;br&gt;&lt;br /&gt;…&lt;br&gt;&lt;br /&gt;&amp;lt;/bookstore&amp;gt;&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;a.cs&lt;br&gt;&lt;br /&gt;using System;&lt;br&gt;&lt;br /&gt;using System.IO;&lt;br&gt;&lt;br /&gt;using System.Xml;&lt;br&gt;&lt;br /&gt;public class zzz&lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;public static void Main()&lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;XmlDocument d = new XmlDocument();&lt;br&gt;&lt;br /&gt;XmlTextReader r = new XmlTextReader(&amp;quot;books.xml&amp;quot;);&lt;br&gt;&lt;br /&gt;r.WhitespaceHandling = WhitespaceHandling.None;&lt;br&gt;&lt;br /&gt;r.MoveToContent();&lt;br&gt;&lt;br /&gt;r.Read();&lt;br&gt;&lt;br /&gt;r.Skip();&lt;br&gt;&lt;br /&gt;r.Skip();&lt;br&gt;&lt;br /&gt;d.Load(r);&lt;br&gt;&lt;br /&gt;d.Save(Console.Out);&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;Output&lt;br&gt;&lt;br /&gt;&amp;lt;book genre=&amp;quot;philosophy&amp;quot; publicationdate=&amp;quot;1991&amp;quot; ISBN=&amp;quot;1-861001-57-6&amp;quot;&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;title&amp;gt;The Gorgias&amp;lt;/title&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;author&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;name&amp;gt;Plato&amp;lt;/name&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;/author&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;price&amp;gt;9.99&amp;lt;/price&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;/book&amp;gt;&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;The Read function takes the liberty to position itself at the book tag, the &lt;br /&gt;first Skip function skips over the first book, and the second skip function &lt;br /&gt;skips over the second book. The Load function of the XmlDocument now loads the &lt;br /&gt;node from the reader's current position, i.e. from the third book onwards. Thus, &lt;br /&gt;the XmlDocument contains the third book, which is about Philosophy, and not the &lt;br /&gt;other two books. The skip function skips over nodes that are at the same level.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;a.cs&lt;br&gt;&lt;br /&gt;using System;&lt;br&gt;&lt;br /&gt;using System.IO;&lt;br&gt;&lt;br /&gt;using System.Xml;&lt;br&gt;&lt;br /&gt;public class zzz&lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;public static void Main()&lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;XmlDocument d = new XmlDocument();&lt;br&gt;&lt;br /&gt;d.LoadXml(&amp;quot;&amp;lt;aa bb='hi'&amp;gt; bad &amp;lt;/aa&amp;gt;&amp;quot;);&lt;br&gt;&lt;br /&gt;d.Save(&amp;quot;d.xml&amp;quot;);&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;d.xml&lt;br&gt;&lt;br /&gt;&amp;lt;aa bb=&amp;quot;hi&amp;quot;&amp;gt; bad &amp;lt;/aa&amp;gt;&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;There are different ways of associating XML with an XmlDocument object. The &lt;br /&gt;XmlDocument class has a function called LoadXml that converts a string &lt;br /&gt;representing XML into an XmlDocument object. The Save function writes the &lt;br /&gt;contents to a file on disk named d.xml. &lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;a.cs&lt;br&gt;&lt;br /&gt;using System;&lt;br&gt;&lt;br /&gt;using System.IO;&lt;br&gt;&lt;br /&gt;using System.Xml;&lt;br&gt;&lt;br /&gt;public class zzz&lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;public static void Main()&lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;XmlDocument d = new XmlDocument();&lt;br&gt;&lt;br /&gt;d.CreateElement(&amp;quot;vijay&amp;quot;);&lt;br&gt;&lt;br /&gt;d.Save(&amp;quot;d.xml&amp;quot;);&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;Output&lt;br&gt;&lt;br /&gt;Unhandled Exception: System.Xml.XmlException: This an invalid XML document. The &lt;br /&gt;document does not have a root element.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;The CreateElement function, as the name suggests, creates an element with the &lt;br /&gt;name passed as a parameter. We encounter an impediment when we attempt to write &lt;br /&gt;it to disk. An exception is thrown, since we have not added the element to the &lt;br /&gt;XmlDocument. Had we used the Save overload that writes to Console.Out, no &lt;br /&gt;exceptions would have been thrown, but neither would any output have been &lt;br /&gt;displayed on the screen.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;a.cs&lt;br&gt;&lt;br /&gt;using System;&lt;br&gt;&lt;br /&gt;using System.IO;&lt;br&gt;&lt;br /&gt;using System.Xml;&lt;br&gt;&lt;br /&gt;public class zzz&lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;public static void Main() {&lt;br&gt;&lt;br /&gt;XmlDocument d = new XmlDocument();&lt;br&gt;&lt;br /&gt;XmlElement e;&lt;br&gt;&lt;br /&gt;e = d.CreateElement(&amp;quot;vijay&amp;quot;);&lt;br&gt;&lt;br /&gt;d.AppendChild(e);&lt;br&gt;&lt;br /&gt;d.Save(&amp;quot;d.xml&amp;quot;);&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;d.xml&lt;br&gt;&lt;br /&gt;&amp;lt;vijay /&amp;gt;&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;The CreateElement function returns an XmlElement object, which is stored in e. &lt;br /&gt;This object is then passed on to the AppendChild function, which creates a child &lt;br /&gt;node. Thus, when we write the XmlDocument to disk, d.xml will display a single &lt;br /&gt;tag of 'vijay'. &lt;br&gt;&lt;br /&gt;We shall now examine a series of programs, which shall create each and every &lt;br /&gt;type of node and shall write them all to disk.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;a.cs&lt;br&gt;&lt;br /&gt;using System;&lt;br&gt;&lt;br /&gt;using System.IO;&lt;br&gt;&lt;br /&gt;using System.Xml;&lt;br&gt;&lt;br /&gt;public class zzz&lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;public static void Main()&lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;XmlDocument d = new XmlDocument();&lt;br&gt;&lt;br /&gt;XmlElement e;&lt;br&gt;&lt;br /&gt;e = d.CreateElement(&amp;quot;vijay&amp;quot;);&lt;br&gt;&lt;br /&gt;XmlAttribute a = d.CreateAttribute(&amp;quot;a1&amp;quot;);&lt;br&gt;&lt;br /&gt;e.SetAttributeNode(a);&lt;br&gt;&lt;br /&gt;e.SetAttribute(&amp;quot;a1&amp;quot;, &amp;quot;howdy&amp;quot;);&lt;br&gt;&lt;br /&gt;d.AppendChild(e);&lt;br&gt;&lt;br /&gt;d.Save(&amp;quot;d.xml&amp;quot;);&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;d.xml&lt;br&gt;&lt;br /&gt;&amp;lt;vijay a1=&amp;quot;howdy&amp;quot; /&amp;gt;&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;CreateElement creates an element called 'vijay'. Thereafter, a function called &lt;br /&gt;CreateAttribute creates an attribute called a1. This function returns an &lt;br /&gt;XmlAttribute object that is associated with the element e, using the function &lt;br /&gt;SetAttributeNode, off the XmlElement class. If we comment the line &lt;br /&gt;e.SetAttribute(&amp;quot;a1&amp;quot;, &amp;quot;howdy&amp;quot;); we shall see the file d.xml containing the &lt;br /&gt;following: &lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;d.xml&lt;br&gt;&lt;br /&gt;&amp;lt;vijay a1=&amp;quot;&amp;quot; /&amp;gt;&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;The attribute gets created, but it is devoid of any value at this stage. It is &lt;br /&gt;the SetAttribute function from the XmlElement class that will assign the value &lt;br /&gt;to the attribute. Hence, it accepts two parameters. &lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;The first parameter is a1, which is the name of the attribute, and the second &lt;br /&gt;parameter 'howdy' is the value of the attribute.&lt;br&gt;&lt;br /&gt;a.cs&lt;br&gt;&lt;br /&gt;using System;&lt;br&gt;&lt;br /&gt;using System.IO;&lt;br&gt;&lt;br /&gt;using System.Xml;&lt;br&gt;&lt;br /&gt;public class zzz&lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;public static void Main()&lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;XmlDocument d = new XmlDocument();&lt;br&gt;&lt;br /&gt;XmlElement e;&lt;br&gt;&lt;br /&gt;e = d.CreateElement(&amp;quot;vijay&amp;quot;);&lt;br&gt;&lt;br /&gt;d.AppendChild(e);&lt;br&gt;&lt;br /&gt;XmlCDataSection c;&lt;br&gt;&lt;br /&gt;c = d.CreateCDataSection(&amp;quot;sonal mukhi&amp;quot;); &lt;br&gt;&lt;br /&gt;d.AppendChild(c); &lt;br&gt;&lt;br /&gt;d.Save(&amp;quot;d.xml&amp;quot;);&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;Output&lt;br&gt;&lt;br /&gt;Unhandled Exception: System.InvalidOperationException: The specified node cannot &lt;br /&gt;be inserted as the valid child of this node, because the specified node is the &lt;br /&gt;wrong type.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;We first create an element 'vijay' by using the CreateElement function. &lt;br /&gt;Thereafter, using the Append Child function, we add the element to the &lt;br /&gt;XmlDocument. No errors are generated upto this point. &lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;Now, we wish to add a CDATA section to the file. To achieve this, the &lt;br /&gt;CreateCDataSection is used with a string parameter 'sonal mukhi', which returns &lt;br /&gt;an XmlCDataSection object. Employing the familiar AppendChild function, the &lt;br /&gt;section is added to the file.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;Since an XML document cannot have a CDATA section by itself, the above exception &lt;br /&gt;is generated.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;a.cs&lt;br&gt;&lt;br /&gt;using System;&lt;br&gt;&lt;br /&gt;using System.IO;&lt;br&gt;&lt;br /&gt;using System.Xml;&lt;br&gt;&lt;br /&gt;public class zzz&lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;public static void Main()&lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;XmlDocument d = new XmlDocument();&lt;br&gt;&lt;br /&gt;XmlElement e;&lt;br&gt;&lt;br /&gt;e = d.CreateElement(&amp;quot;vijay&amp;quot;);&lt;br&gt;&lt;br /&gt;d.AppendChild(e);&lt;br&gt;&lt;br /&gt;XmlCDataSection c;&lt;br&gt;&lt;br /&gt;c = d.CreateCDataSection(&amp;quot;sonal mukhi&amp;quot;); &lt;br&gt;&lt;br /&gt;XmlElement r = d.DocumentElement;&lt;br&gt;&lt;br /&gt;r.AppendChild(c); &lt;br&gt;&lt;br /&gt;d.Save(&amp;quot;d.xml&amp;quot;);&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;d.xml&lt;br&gt;&lt;br /&gt;&amp;lt;vijay&amp;gt;&amp;lt;![CDATA[sonal mukhi]]&amp;gt;&amp;lt;/vijay&amp;gt;&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;The error now disappears, as the AppendChild function is called from the &lt;br /&gt;XmlElement class, and not from the XmlDocument class. &lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;The DocumentElement property represents the root or the first element of &lt;br /&gt;theXMLdocument. The CDATA section now gets added to the element 'vijay', since &lt;br /&gt;the root element is 'vijay'. &lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;a.cs&lt;br&gt;&lt;br /&gt;using System;&lt;br&gt;&lt;br /&gt;using System.IO;&lt;br&gt;&lt;br /&gt;using System.Xml;&lt;br&gt;&lt;br /&gt;public class zzz&lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;public static void Main() {&lt;br&gt;&lt;br /&gt;XmlDocument d = new XmlDocument();&lt;br&gt;&lt;br /&gt;d.LoadXml(&amp;quot;&amp;lt;vijay&amp;gt; hi &amp;lt;/vijay&amp;gt;&amp;quot;);&lt;br&gt;&lt;br /&gt;XmlCDataSection c;&lt;br&gt;&lt;br /&gt;c = d.CreateCDataSection(&amp;quot;sonal mukhi&amp;quot;); &lt;br&gt;&lt;br /&gt;XmlElement r = d.DocumentElement;&lt;br&gt;&lt;br /&gt;r.AppendChild(c); &lt;br&gt;&lt;br /&gt;d.Save(&amp;quot;d.xml&amp;quot;);&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;d.xml&lt;br&gt;&lt;br /&gt;&amp;lt;vijay&amp;gt; hi &amp;lt;![CDATA[sonal mukhi]]&amp;gt;&amp;lt;/vijay&amp;gt;&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;An alternative approach would be to use the trustworthy LoadXml function, in &lt;br /&gt;lieu of CreateElement. Any of the two approaches may be employed to add a root &lt;br /&gt;node, thereby, displaying the d.xml file. As we have been harping repeatedly, &lt;br /&gt;there are many ways to skin a cat.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;a.cs&lt;br&gt;&lt;br /&gt;using System.Xml;&lt;br&gt;&lt;br /&gt;public class Sample&lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;public static void Main()&lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;XmlDocument d = new XmlDocument();&lt;br&gt;&lt;br /&gt;d.LoadXml(&amp;quot;&amp;lt;!DOCTYPE book &amp;gt; &amp;lt;book /&amp;gt; &amp;quot;);&lt;br&gt;&lt;br /&gt;XmlDocumentType t;&lt;br&gt;&lt;br /&gt;t = d.DocumentType;&lt;br&gt;&lt;br /&gt;System.Console.WriteLine(t.OuterXml);&lt;br&gt;&lt;br /&gt;System.Console.WriteLine(t.Name);&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;Output&lt;br&gt;&lt;br /&gt;&amp;lt;!DOCTYPE book[]&amp;gt;&lt;br&gt;&lt;br /&gt;book&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;This program demonstrates our ability to access a specific node while processing &lt;br /&gt;an XML file. &lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;The DocumentType property returns an XmlDocumentType t, which represents the &lt;br /&gt;singular DOCTYPE node present in an XML file. This object t has a large number &lt;br /&gt;of properties that facilitate access to all the details regarding the node. In &lt;br /&gt;the program, only two properties are displayed:&lt;br&gt;&lt;br /&gt;• The first property OuterXml represents the entire node as a string. Since no &lt;br /&gt;external DTD file is present, by default, the internal DTD, which does not have &lt;br /&gt;any contents, is displayed. &lt;br&gt;&lt;br /&gt;• The second property Name presents the name of the root node.&lt;br&gt;&lt;br /&gt;a.cs&lt;br&gt;&lt;br /&gt;using System;&lt;br&gt;&lt;br /&gt;using System.Xml;&lt;br&gt;&lt;br /&gt;public class Sample&lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;public static void Main()&lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;XmlDocument d = new XmlDocument();&lt;br&gt;&lt;br /&gt;d.LoadXml(&amp;quot;&amp;lt;book&amp;gt; &amp;lt;title&amp;gt;None&amp;lt;/title&amp;gt; &amp;lt;author&amp;gt;Me&amp;lt;/author&amp;gt; &amp;lt;/book&amp;gt;&amp;quot;);&lt;br&gt;&lt;br /&gt;XmlNode r = d.FirstChild;&lt;br&gt;&lt;br /&gt;XmlNode n = r.FirstChild;&lt;br&gt;&lt;br /&gt;Console.WriteLine(r);&lt;br&gt;&lt;br /&gt;Console.WriteLine(n.OuterXml);&lt;br&gt;&lt;br /&gt;Console.WriteLine(r.OuterXml);&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;Output&lt;br&gt;&lt;br /&gt;System.Xml.XmlElement&lt;br&gt;&lt;br /&gt;&amp;lt;title&amp;gt;None&amp;lt;/title&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;book&amp;gt;&amp;lt;title&amp;gt;None&amp;lt;/title&amp;gt;&amp;lt;author&amp;gt;Me&amp;lt;/author&amp;gt;&amp;lt;/book&amp;gt;&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;The FirstChild property of the XmlDocument class or XmlNode class retrieves the &lt;br /&gt;first child of the current node in the document i.e. XmlElement. The value is &lt;br /&gt;stored in both r and n. It can be displayed using the WriteLine function. The &lt;br /&gt;OuterXml property contains the tags, including the child nodes. &lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;The FirstChild property of r is an XmlNode, whose OuterXml is the first child &lt;br /&gt;node or tag title within the tag 'book'. If no child node exists, the value is &lt;br /&gt;null.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;a.cs&lt;br&gt;&lt;br /&gt;using System;&lt;br&gt;&lt;br /&gt;using System.Xml;&lt;br&gt;&lt;br /&gt;public class Sample&lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;public static void Main()&lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;XmlDocument d = new XmlDocument();&lt;br&gt;&lt;br /&gt;d.LoadXml(&amp;quot;&amp;lt;book&amp;gt; &amp;lt;title&amp;gt;None&amp;lt;/title&amp;gt; &amp;lt;author&amp;gt;Me&amp;lt;/author&amp;gt; &amp;lt;/book&amp;gt;&amp;quot;);&lt;br&gt;&lt;br /&gt;XmlNode r = d.LastChild;&lt;br&gt;&lt;br /&gt;XmlNode n = r.LastChild;&lt;br&gt;&lt;br /&gt;Console.WriteLine(r);&lt;br&gt;&lt;br /&gt;Console.WriteLine(n.OuterXml);&lt;br&gt;&lt;br /&gt;Console.WriteLine(r.OuterXml);&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;Output&lt;br&gt;&lt;br /&gt;System.Xml.XmlElement&lt;br&gt;&lt;br /&gt;&amp;lt;author&amp;gt;Me&amp;lt;/author&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;book&amp;gt;&amp;lt;title&amp;gt;None&amp;lt;/title&amp;gt;&amp;lt;author&amp;gt;Me&amp;lt;/author&amp;gt;&amp;lt;/book&amp;gt;&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;Comparable to the FirstChild property is the LastChild property, which merely &lt;br /&gt;returns the last child in the node. Therefore, as the 'author' tag is the last &lt;br /&gt;tag, the LastChild property returns a reference to 'Me'. Since title is the only &lt;br /&gt;tag in the file, the use of FirstChild or LastChild achieves the same outcome, &lt;br /&gt;in this case. &lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;a.cs&lt;br&gt;&lt;br /&gt;using System;&lt;br&gt;&lt;br /&gt;using System.Xml;&lt;br&gt;&lt;br /&gt;public class Sample&lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;public static void Main()&lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;XmlDocument d = new XmlDocument();&lt;br&gt;&lt;br /&gt;d.LoadXml(&amp;quot;&amp;lt;aa&amp;gt;&amp;lt;book&amp;gt; hi&amp;lt;title&amp;gt;None&amp;lt;/title&amp;gt; &amp;lt;/book&amp;gt; &amp;lt;/aa&amp;gt;&amp;quot;);&lt;br&gt;&lt;br /&gt;XmlDocument de = (XmlDocument) d.CloneNode(true);&lt;br&gt;&lt;br /&gt;Console.WriteLine(de.ChildNodes.Count);&lt;br&gt;&lt;br /&gt;for (int i=0; i&amp;lt;de.ChildNodes.Count; i++)&lt;br&gt;&lt;br /&gt;Console.WriteLine(de.ChildNodes[i].OuterXml);&lt;br&gt;&lt;br /&gt;Console.WriteLine(de.Name + de.OuterXml);&lt;br&gt;&lt;br /&gt;Console.WriteLine(&amp;quot;Shallow&amp;quot;);&lt;br&gt;&lt;br /&gt;XmlDocument sh = (XmlDocument) d.CloneNode(false);&lt;br&gt;&lt;br /&gt;Console.WriteLine(sh.ChildNodes.Count);&lt;br&gt;&lt;br /&gt;for (int i=0; i&amp;lt;sh.ChildNodes.Count; i++)&lt;br&gt;&lt;br /&gt;Console.WriteLine(sh.ChildNodes[i].InnerXml);&lt;br&gt;&lt;br /&gt;Console.WriteLine(sh.Name + sh.OuterXml);&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;Output&lt;br&gt;&lt;br /&gt;1&lt;br&gt;&lt;br /&gt;&amp;lt;aa&amp;gt;&amp;lt;book&amp;gt; hi&amp;lt;title&amp;gt;None&amp;lt;/title&amp;gt;&amp;lt;/book&amp;gt;&amp;lt;/aa&amp;gt;&lt;br&gt;&lt;br /&gt;#document&amp;lt;aa&amp;gt;&amp;lt;book&amp;gt; hi&amp;lt;title&amp;gt;None&amp;lt;/title&amp;gt;&amp;lt;/book&amp;gt;&amp;lt;/aa&amp;gt;&lt;br&gt;&lt;br /&gt;Shallow&lt;br&gt;&lt;br /&gt;0&lt;br&gt;&lt;br /&gt;#document&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;We start with an XML fragment that has a root node aa, which has a child node of &lt;br /&gt;book, containing another child node called title. The CloneNode function in &lt;br /&gt;XmlDocument takes a boolean parameter, where True refers to cloning the nodes &lt;br /&gt;within the node aa, which includes the nodes 'book' and 'title'. It behaves akin &lt;br /&gt;to a copy constructor for nodes. &lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;The cloned node does not have a parent. Therefore, when we exploit the property &lt;br /&gt;ParentNode to ascertain the value of the parent node, it returns a value of &lt;br /&gt;null. The ParentNode property returns a handle to the Parent node of the node.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;The return value of the CloneNode function is an XmlNode class. As the &lt;br /&gt;XmlDocument derives from XmlNode, the 'cast' operator is used. The XmlNode class &lt;br /&gt;has a property called ChildNodes, which returns an XmlNodeList object that &lt;br /&gt;refers to all the child nodes. This object is of a Collection data type. The &lt;br /&gt;Count property in the collection can be used to render a count of the number of &lt;br /&gt;entities present in the clone.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;The Name property returns the qualified name of #document and the OuterXml &lt;br /&gt;property provides the entire element. This function at variance with the &lt;br /&gt;LocalName function, which returns the name of an attribute, #cdata-section for &lt;br /&gt;the CDATA section, and so on. Thus, with the help of this function, we can &lt;br /&gt;identify the different names for different node types.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;The CloneNode function is called again, but with a value of false. Thus, a &lt;br /&gt;shallow node with no ChildNodes is returned. This is confirmed using the count &lt;br /&gt;property, which shows a count of zero. This proves that only the node has been &lt;br /&gt;cloned and not the content.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;Using a 'for' statement, we iterate through all the child nodes, and display the &lt;br /&gt;OuterXml property using the indexer. The value returned is the content of the &lt;br /&gt;node, which includes text such as 'hi'. If you read about the CloneNode function &lt;br /&gt;in the documentation, it would apprise you of the impending eventuality, if an &lt;br /&gt;attempt is made to clone nodes of different types.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;a.cs&lt;br&gt;&lt;br /&gt;using System;&lt;br&gt;&lt;br /&gt;using System.IO;&lt;br&gt;&lt;br /&gt;using System.Xml;&lt;br&gt;&lt;br /&gt;public class Sample&lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;public static void Main()&lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;XmlDocument d = new XmlDocument();&lt;br&gt;&lt;br /&gt;d.LoadXml(&amp;quot;&amp;lt;!DOCTYPE book [&amp;lt;!ENTITY bb 'vijay'&amp;gt;]&amp;gt; &amp;lt;book&amp;gt; &amp;lt;misc /&amp;gt; &amp;lt;/book&amp;gt;&amp;quot;); &lt;br&gt;&lt;br /&gt;XmlEntityReference e = d.CreateEntityReference(&amp;quot;bb&amp;quot;);&lt;br&gt;&lt;br /&gt;Console.WriteLine(e.ChildNodes.Count);&lt;br&gt;&lt;br /&gt;d.DocumentElement.LastChild.AppendChild(e);&lt;br&gt;&lt;br /&gt;Console.WriteLine(e.FirstChild.InnerText + &amp;quot;. &amp;quot; + e.ChildNodes.Count + &amp;quot; &amp;quot; + &lt;br /&gt;e.Name);&lt;br&gt;&lt;br /&gt;d.Save(Console.Out);&lt;br&gt;&lt;br /&gt;XmlEntityReference e1 = d.CreateEntityReference(&amp;quot;bbb&amp;quot;);&lt;br&gt;&lt;br /&gt;Console.WriteLine(e1.ChildNodes.Count);&lt;br&gt;&lt;br /&gt;d.DocumentElement.LastChild.AppendChild(e1);&lt;br&gt;&lt;br /&gt;Console.WriteLine(e1.FirstChild.InnerText + &amp;quot;. &amp;quot; + e1.ChildNodes.Count + &amp;quot; &amp;quot; + &lt;br /&gt;e1.Name);&lt;br&gt;&lt;br /&gt;d.Save(Console.Out);&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;Output&lt;br&gt;&lt;br /&gt;0&lt;br&gt;&lt;br /&gt;vijay. 1 bb&lt;br&gt;&lt;br /&gt;&amp;lt;!DOCTYPE book[&amp;lt;!ENTITY bb 'vijay'&amp;gt;]&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;book&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;misc&amp;gt;&amp;amp;bb;&amp;lt;/misc&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;/book&amp;gt;0&lt;br&gt;&lt;br /&gt;. 1 bbb&lt;br&gt;&lt;br /&gt;&amp;lt;!DOCTYPE book[&amp;lt;!ENTITY bb 'vijay'&amp;gt;]&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;book&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;misc&amp;gt;&amp;amp;bb;&amp;amp;bbb;&amp;lt;/misc&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;/book&amp;gt;&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;The LoadXml function has a DOCTYPE declaration, and an entity reference called &lt;br /&gt;bb having a value of 'vijay'. Now, using the function CreateEntityReference, we &lt;br /&gt;create an entity reference called bb. This function returns an &lt;br /&gt;XmlEntityReference object, which is a class derived from XmlLinkedNode, which, &lt;br /&gt;in turn, is derived from XmlNode. The number of child nodes is zero at this &lt;br /&gt;stage, because the node has not yet been expanded. &lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;The function AppendChild is then used to append the child to the node, returned &lt;br /&gt;by the LastChild property.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;Since the node has been appended to the document, its parent node is set and the &lt;br /&gt;entity reference bb is expanded to 'vijay'. Thus, the file has a child node that &lt;br /&gt;contains 'vijay', the entity reference text. The InnerText property of the &lt;br /&gt;FirstChild gives the replacement text. The Count of the child nodes is one, &lt;br /&gt;since we have added one entity, and the Name property of the Entity Reference is &lt;br /&gt;the name bb, which we have created.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;Finally, the Save function, which prints out the XML fragment, displays the &lt;br /&gt;entity reference bb, starting with an ampersand symbol &amp;amp; and ending with a &lt;br /&gt;semi-colon, with the LastChild node named misc.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;We now add another entity reference bbb, but lay it aside, undefined. The rules, &lt;br /&gt;which applied earlier to the entity reference, are also relevant now. The only &lt;br /&gt;significant difference is that the InnerText property does not have any value &lt;br /&gt;when the reference node is expanded. Thus, the child is an empty text node. The &lt;br /&gt;entity refs of bb and bbb are placed one after the other.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;a.cs&lt;br&gt;&lt;br /&gt;using System;&lt;br&gt;&lt;br /&gt;using System.IO;&lt;br&gt;&lt;br /&gt;using System.Xml;&lt;br&gt;&lt;br /&gt;public class zzz&lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;public static void Main() {&lt;br&gt;&lt;br /&gt;XmlDocument d = new XmlDocument();&lt;br&gt;&lt;br /&gt;d.LoadXml(&amp;quot;&amp;lt;book&amp;gt;&amp;lt;/book&amp;gt;&amp;quot;); &lt;br&gt;&lt;br /&gt;XmlNode n=d.CreateNode(&amp;quot;element&amp;quot;, &amp;quot;vijay&amp;quot;, &amp;quot;&amp;quot;); &lt;br&gt;&lt;br /&gt;n.InnerText = &amp;quot;hi&amp;quot;;&lt;br&gt;&lt;br /&gt;XmlElement r = d.DocumentElement;&lt;br&gt;&lt;br /&gt;r.AppendChild(n);&lt;br&gt;&lt;br /&gt;//d.LastChild.AppendChild(n);&lt;br&gt;&lt;br /&gt;//d.AppendChild(n);&lt;br&gt;&lt;br /&gt;Console.WriteLine(d.OuterXml);&lt;br&gt;&lt;br /&gt;d.Save(Console.Out);&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;Output&lt;br&gt;&lt;br /&gt;&amp;lt;book&amp;gt;&amp;lt;vijay&amp;gt;hi&amp;lt;/vijay&amp;gt;&amp;lt;/book&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;book&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;vijay&amp;gt;hi&amp;lt;/vijay&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;/book&amp;gt;&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;The XML fragment shows the root tag as 'book' because of the LoadXml function. &lt;br /&gt;The CreateNode function creates a new node or element called 'vijay'. This &lt;br /&gt;function returns an object of type XmlNode and accepts the following parameters:&lt;br /&gt;&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;The first parameter decides on the type of node. Exactly 10 types of nodes can &lt;br /&gt;be created with this function: element, attribute, text, cdatasection, &lt;br /&gt;entityreference, processinginstruction, comment, document, documenttype and &lt;br /&gt;documentfragmnet. This parameter is case sensitive. &lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;The second parameter is the name of the new node. If there is a colon in the &lt;br /&gt;name, it is parsed into a prefix and a LocalName. &lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;The third parameter refers to the namespace URI. The InnerText property is &lt;br /&gt;another mechanism for adding content between tags. The tag 'vijay' now embodies &lt;br /&gt;the word 'hi'. This node in memory is added to the XML fragment with the help of &lt;br /&gt;the AppendChild function, which is called off the XmlElement, returned by the &lt;br /&gt;DocumentElement property. &lt;br&gt;&lt;br /&gt;Calling AppendChild off the XmlDocument class, throws an exception, since a &lt;br /&gt;DocumentElement node already exists within the document. If we uncomment the &lt;br /&gt;line d.AppendChild(n), it will result in the following exception:&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;Output&lt;br&gt;&lt;br /&gt;Unhandled Exception: System.InvalidOperationException: This document already has &lt;br /&gt;a DocumentElement node.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;If we uncomment the line d.LastChild.AppendChild(n), it would be similar to &lt;br /&gt;first gaining a handle to the last child and then adding the node. In this case, &lt;br /&gt;whether the node is the first child or the last child, it does not make any &lt;br /&gt;difference at all. The documentation for the CreateNode function, offers a table &lt;br /&gt;that very distinctly specifies as to which nodes can be placed within other &lt;br /&gt;nodes. &lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;a.cs&lt;br&gt;&lt;br /&gt;using System;&lt;br&gt;&lt;br /&gt;using System.Xml;&lt;br&gt;&lt;br /&gt;public class zzz {&lt;br&gt;&lt;br /&gt;public static void Main()&lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;XmlDocument d = new XmlDocument();&lt;br&gt;&lt;br /&gt;d.Load(&amp;quot;b.xml&amp;quot;);&lt;br&gt;&lt;br /&gt;XmlImplementation i;&lt;br&gt;&lt;br /&gt;i = d.Implementation;&lt;br&gt;&lt;br /&gt;XmlDocument d1 = i.CreateDocument();&lt;br&gt;&lt;br /&gt;d.Save(Console.Out);&lt;br&gt;&lt;br /&gt;d1.Save(Console.Out);&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;b.xml&lt;br&gt;&lt;br /&gt;&amp;lt;?xml version=&amp;quot;1.0&amp;quot;?&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;!DOCTYPE vijay&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;vijay /&amp;gt;&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;Output&lt;br&gt;&lt;br /&gt;&amp;lt;?xml version=&amp;quot;1.0&amp;quot;?&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;!DOCTYPE vijay[]&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;vijay /&amp;gt;&lt;br&gt;&lt;br /&gt;Every XmlDocument has an XmlImplementation object associated with it, which can &lt;br /&gt;be accessed using the Implementation property. XmlDocument objects created from &lt;br /&gt;the same XmlImplementation, share the same name table. Thus, it is permissible &lt;br /&gt;to compare attribute and element names as objects, instead of strings. &lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;We create an empty XmlDocument d by using the constructor as before, and &lt;br /&gt;thereafter, use the Load function to associate anXMLfile named b.xml with the &lt;br /&gt;XmlDocument. Earlier we had used the Reader object. &lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;To create an XmlDocument object that shares the same XmlNameTable, we use the &lt;br /&gt;function CreateDocument from the XmlImplementation object. Then, the Save &lt;br /&gt;function is used to write out both the XmlDocument objects to the Console. The &lt;br /&gt;first XmlDocument object d, displays a replica of the file b.xml, while the &lt;br /&gt;second one is empty because we have not associated any XML content with it. &lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;Thus, the CreateDocument function creates an empty XML document, but it does not &lt;br /&gt;copy any content to it. It ensures that the names are shared, and not &lt;br /&gt;duplicated.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;a.cs&lt;br&gt;&lt;br /&gt;using System;&lt;br&gt;&lt;br /&gt;using System.IO;&lt;br&gt;&lt;br /&gt;using System.Xml;&lt;br&gt;&lt;br /&gt;public class zzz&lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;public static void Main()&lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;XmlTextReader r = new XmlTextReader (&amp;quot;books.xml&amp;quot;);&lt;br&gt;&lt;br /&gt;r.WhitespaceHandling = WhitespaceHandling.None;&lt;br&gt;&lt;br /&gt;XmlDocument d = new XmlDocument();&lt;br&gt;&lt;br /&gt;d.Load (r);&lt;br&gt;&lt;br /&gt;XmlNode n;&lt;br&gt;&lt;br /&gt;n = d.DocumentElement;&lt;br&gt;&lt;br /&gt;Console.WriteLine(n.Name + &amp;quot;&amp;lt;&amp;quot; + n.Value + &amp;quot;&amp;gt;&amp;quot;);&lt;br&gt;&lt;br /&gt;n = n.FirstChild;&lt;br&gt;&lt;br /&gt;Console.WriteLine(n.Name + &amp;quot;&amp;lt;&amp;quot; + n.Value + &amp;quot;&amp;gt;&amp;quot;);&lt;br&gt;&lt;br /&gt;while (n != null)&lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;XmlNamedNodeMap m = n.Attributes;&lt;br&gt;&lt;br /&gt;foreach (XmlNode a in m)&lt;br&gt;&lt;br /&gt;Console.Write(&amp;quot; &amp;quot; + a.Name + &amp;quot;&amp;lt;&amp;quot; + a.Value + &amp;quot;&amp;gt; &amp;quot;);&lt;br&gt;&lt;br /&gt;n = n.NextSibling;&lt;br&gt;&lt;br /&gt;Console.WriteLine();&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;Output&lt;br&gt;&lt;br /&gt;bookstore&amp;lt;&amp;gt;&lt;br&gt;&lt;br /&gt;book&amp;lt;&amp;gt;&lt;br&gt;&lt;br /&gt;genre&amp;lt;autobiography&amp;gt; publicationdate&amp;lt;1981&amp;gt; ISBN&amp;lt;1-861003-11-0&amp;gt; &lt;br&gt;&lt;br /&gt;genre&amp;lt;novel&amp;gt; publicationdate&amp;lt;1967&amp;gt; ISBN&amp;lt;0-201-63361-2&amp;gt; &lt;br&gt;&lt;br /&gt;genre&amp;lt;philosophy&amp;gt; publicationdate&amp;lt;1991&amp;gt; ISBN&amp;lt;1-861001-57-6&amp;gt; &lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;First, we start by loading the XML file books.xml, and then, positioning the &lt;br /&gt;DocumentElement at the root tag bookstore. Then, using the FirstChild property, &lt;br /&gt;we make the first child node 'book' as the active node and display it. As we &lt;br /&gt;have no clue about the number of child book nodes that are present in the file, &lt;br /&gt;we use a while loop that repeats itself until the XmlNode object n returns a &lt;br /&gt;null. &lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;The Attributes property returns an XmlNamedNodeMap object that represents a &lt;br /&gt;collection of nodes. These nodes can be represented by a name or an index. So, &lt;br /&gt;using the foreach loop, we fetch one attribute at a time in an XmlNode object &lt;br /&gt;and display the Name and the Value contained in it. The &amp;lt;&amp;gt; signs are used as a &lt;br /&gt;talisman. &lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;Once this is accomplished, we move to the next book at the same level. Tags at &lt;br /&gt;the same level are called siblings. The property NextSibling moves to the next &lt;br /&gt;book or tag at the same level, thereby, displaying one book tag after another.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;a.cs&lt;br&gt;&lt;br /&gt;using System;&lt;br&gt;&lt;br /&gt;using System.Xml;&lt;br&gt;&lt;br /&gt;public class Sample&lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;public static void Main()&lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;XmlTextReader r = new XmlTextReader (&amp;quot;books.xml&amp;quot;);&lt;br&gt;&lt;br /&gt;r.WhitespaceHandling = WhitespaceHandling.None;&lt;br&gt;&lt;br /&gt;XmlDocument d = new XmlDocument();&lt;br&gt;&lt;br /&gt;d.Load (r);&lt;br&gt;&lt;br /&gt;XmlNode n = d.DocumentElement;&lt;br&gt;&lt;br /&gt;Console.WriteLine(n.Name);&lt;br&gt;&lt;br /&gt;XmlNode b = n.LastChild;&lt;br&gt;&lt;br /&gt;Console.WriteLine(b.Name);&lt;br&gt;&lt;br /&gt;Console.WriteLine(b.OuterXml + &amp;quot;\n&amp;quot;);&lt;br&gt;&lt;br /&gt;b = n.LastChild.PreviousSibling;&lt;br&gt;&lt;br /&gt;Console.WriteLine(b.OuterXml); &lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;Output&lt;br&gt;&lt;br /&gt;bookstore&lt;br&gt;&lt;br /&gt;book&lt;br&gt;&lt;br /&gt;&amp;lt;book genre=&amp;quot;philosophy&amp;quot; publicationdate=&amp;quot;1991&amp;quot; ISBN=&amp;quot;1-861001-57-6&amp;quot;&amp;gt;&amp;lt;title&amp;gt;The &lt;br /&gt;Gorgias&amp;lt;/title&amp;gt;&amp;lt;author&amp;gt;&amp;lt;name&amp;gt;Plato &amp;lt;/name&amp;gt;&amp;lt;/author&amp;gt;&amp;lt;price&amp;gt;9.99&amp;lt;/price&amp;gt;&amp;lt;/book&amp;gt;&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;In the previous example, we first displayed the root node bookstore, followed by &lt;br /&gt;the LastChild and not the FirstChild node. Both are expected to return the same &lt;br /&gt;name, since the first and last child tags refer to the same node book. The XML &lt;br /&gt;associated with this node was displayed using the OuterXml function, which &lt;br /&gt;displayed the last book, which deals with Philosophy. You may recollect that the &lt;br /&gt;OuterXml function displays the child tags also. &lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;In this example, we proceed backwards. Hence, we use the PreviousSibling &lt;br /&gt;function to display the second last book. Thus, we can either proceed in the &lt;br /&gt;forward direction, like we did in the last example, or in the backward &lt;br /&gt;direction, as shown in the current example. &lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;a.cs&lt;br&gt;&lt;br /&gt;using System;&lt;br&gt;&lt;br /&gt;using System.IO;&lt;br&gt;&lt;br /&gt;using System.Xml;&lt;br&gt;&lt;br /&gt;public class Sample&lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;public static void Main()&lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;XmlDocument d = new XmlDocument();&lt;br&gt;&lt;br /&gt;d.LoadXml(&amp;quot;&amp;lt;book&amp;gt; hi &amp;lt;/book&amp;gt;&amp;quot;);&lt;br&gt;&lt;br /&gt;XmlComment c;&lt;br&gt;&lt;br /&gt;c = d.CreateComment(&amp;quot;Comment 1&amp;quot;);&lt;br&gt;&lt;br /&gt;XmlElement r = d.DocumentElement;&lt;br&gt;&lt;br /&gt;d.InsertBefore(c, r);&lt;br&gt;&lt;br /&gt;d.Save(Console.Out);&lt;br&gt;&lt;br /&gt;Console.WriteLine(&amp;quot;\n---&amp;quot;);&lt;br&gt;&lt;br /&gt;XmlComment c1;&lt;br&gt;&lt;br /&gt;c1 = d.CreateComment(&amp;quot;Comment 2&amp;quot;);&lt;br&gt;&lt;br /&gt;d.InsertAfter(c1, r);&lt;br&gt;&lt;br /&gt;d.Save(Console.Out);&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;Output&lt;br&gt;&lt;br /&gt;&amp;lt;!--Comment 1--&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;book&amp;gt; hi &amp;lt;/book&amp;gt;&lt;br&gt;&lt;br /&gt;---&lt;br&gt;&lt;br /&gt;&amp;lt;!--Comment 1--&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;book&amp;gt; hi &amp;lt;/book&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;!--Comment 2--&amp;gt;&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;LoadXml creates a node called book, which encompasses 'hi'. Thereafter, a &lt;br /&gt;Comment object is created by calling the CreateComment function. The function &lt;br /&gt;merely requires the string to be displayed as a comment. The extra comment &lt;br /&gt;characters are placed by the function in the XML file. &lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;The next dilemma is with regard to the position of the comment i.e. should the &lt;br /&gt;comment be placed before or after the node book. The InsertBefore function &lt;br /&gt;inserts the required node. It takes two parameters, i.e. a comment, followed by &lt;br /&gt;the node before which the comment is to be inserted. As we want it to be &lt;br /&gt;inserted before the book node, we use the handle returned by DocumentElement &lt;br /&gt;property, which contains the handle to the book node. The Save function then &lt;br /&gt;displays the comment before the book node. &lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;The InsertAfter function inserts the comment node after the reference node, &lt;br /&gt;which has been passed as the second parameter. Thus, the second comment comes &lt;br /&gt;after the book node.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;a.cs&lt;br&gt;&lt;br /&gt;using System;&lt;br&gt;&lt;br /&gt;using System.IO;&lt;br&gt;&lt;br /&gt;using System.Xml;&lt;br&gt;&lt;br /&gt;public class zzz {&lt;br&gt;&lt;br /&gt;public static void Main()&lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;XmlDocument d = new XmlDocument();&lt;br&gt;&lt;br /&gt;d.LoadXml(&amp;quot;&amp;lt;?xml version='1.0' ?&amp;gt;&amp;lt;book&amp;gt; &amp;lt;title&amp;gt;Vijay&amp;lt;/title&amp;gt; &amp;lt;/book&amp;gt;&amp;quot;);&lt;br&gt;&lt;br /&gt;XmlNode n = d.DocumentElement;&lt;br&gt;&lt;br /&gt;n.RemoveChild(n.FirstChild);&lt;br&gt;&lt;br /&gt;d.Save(Console.Out);&lt;br&gt;&lt;br /&gt;d.RemoveAll();&lt;br&gt;&lt;br /&gt;Console.WriteLine(&amp;quot;\n=========&amp;quot;);&lt;br&gt;&lt;br /&gt;d.Save(Console.Out);&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;Output&lt;br&gt;&lt;br /&gt;&amp;lt;?xml version=&amp;quot;1.0&amp;quot;?&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;book&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;/book&amp;gt;&lt;br&gt;&lt;br /&gt;=========&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;The XML fragment that is loaded using the LoadXml function has an XML &lt;br /&gt;Declaration, which is a tag book, containing an element of title enclosing &lt;br /&gt;'Vijay'. &lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;The DocumentElement property returns a handle to the node book, which is stored &lt;br /&gt;in n. Then, the RemoveChild function of node n is called with a single &lt;br /&gt;parameter, which is the node title, since the FirstChild returns this value. &lt;br /&gt;This function removes the child from the tag.&lt;br&gt;&lt;br /&gt;Saving to the Console displays the remaining XML fragment. Thus, we can see the &lt;br /&gt;XML declaration and the tag book, but without any content, since the tag title &lt;br /&gt;has been removed. &lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;The next function that is implemented is, RemoveAll from the XmlDocument class. &lt;br /&gt;On displaying the XML file, we witness no output since the RemoveAll function &lt;br /&gt;erases everything from the Document.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;a.cs&lt;br&gt;&lt;br /&gt;using System;&lt;br&gt;&lt;br /&gt;using System.IO;&lt;br&gt;&lt;br /&gt;using System.Xml;&lt;br&gt;&lt;br /&gt;public class zzz {&lt;br&gt;&lt;br /&gt;public static void Main()&lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;XmlNode n;&lt;br&gt;&lt;br /&gt;XmlDocument d = new XmlDocument();&lt;br&gt;&lt;br /&gt;d.LoadXml(&amp;quot;&amp;lt;zz xml:space=\&amp;quot;preserve\&amp;quot;&amp;gt;&amp;lt;aa&amp;gt;Vijay&amp;lt;/aa&amp;gt;&amp;lt;bb&amp;gt;Mukhi&amp;lt;/bb&amp;gt;&amp;lt;/zz&amp;gt;&amp;quot;); &lt;br&gt;&lt;br /&gt;Console.WriteLine(d.DocumentElement.InnerText);&lt;br&gt;&lt;br /&gt;n=d.DocumentElement;&lt;br&gt;&lt;br /&gt;XmlSignificantWhitespace s=d.CreateSignificantWhitespace(&amp;quot; &amp;quot;);&lt;br&gt;&lt;br /&gt;d.Save(Console.Out);&lt;br&gt;&lt;br /&gt;Console.WriteLine();&lt;br&gt;&lt;br /&gt;n.InsertAfter(s, n.FirstChild);&lt;br&gt;&lt;br /&gt;Console.WriteLine(d.DocumentElement.InnerText);&lt;br&gt;&lt;br /&gt;d.Save(Console.Out);&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;Output&lt;br&gt;&lt;br /&gt;VijayMukhi&lt;br&gt;&lt;br /&gt;&amp;lt;zz xml:space=&amp;quot;preserve&amp;quot;&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;aa&amp;gt;Vijay&amp;lt;/aa&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;bb&amp;gt;Mukhi&amp;lt;/bb&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;/zz&amp;gt;&lt;br&gt;&lt;br /&gt;Vijay Mukhi&lt;br&gt;&lt;br /&gt;&amp;lt;zz xml:space=&amp;quot;preserve&amp;quot;&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;aa&amp;gt;Vijay&amp;lt;/aa&amp;gt; &amp;lt;bb&amp;gt;Mukhi&amp;lt;/bb&amp;gt;&amp;lt;/zz&amp;gt;&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;The DocumentElement property returns a handle to the zz tag, whereas, the &lt;br /&gt;InnerText property in the DocumentElement, refers to the Text present within the &lt;br /&gt;tags aa and bb. Thus, we see 'VijayMukhi' displayed without any spaces displayed &lt;br /&gt;between the two words. &lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;We also create significant whitespace, using the function &lt;br /&gt;CreateSignificantWhitespace, after initializing the node n to tag zz. This &lt;br /&gt;function accepts only one string parameter, which could be any one of the &lt;br /&gt;following four: &amp;amp;#20; &amp;amp;#10; &amp;amp;#13; and &amp;amp;#9. It does not augment theXMLfragment. &lt;br /&gt;Finally, we write the XML fragment using the Save function. The attribute &lt;br /&gt;space=preserve is visible. This attribute is optional. &lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;Using the InsertAfter function, we add significant whitespace after the &lt;br /&gt;FirstChild node. Thus, we see significant space between the nodes aa and bb. The &lt;br /&gt;InnerText also displays the spaces between the words 'Vijay' and ' Mukhi'.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;Output&lt;br&gt;&lt;br /&gt;VijayMukhi&lt;br&gt;&lt;br /&gt;&amp;lt;zz xml:space=&amp;quot;preserve&amp;quot;&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;aa&amp;gt;Vijay&amp;lt;/aa&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;bb&amp;gt;Mukhi&amp;lt;/bb&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;/zz&amp;gt;&lt;br&gt;&lt;br /&gt;VijayMukhi&lt;br&gt;&lt;br /&gt;&amp;lt;zz xml:space=&amp;quot;preserve&amp;quot;&amp;gt; &amp;lt;aa&amp;gt;Vijay&amp;lt;/aa&amp;gt;&amp;lt;bb&amp;gt;Mukhi&amp;lt;/bb&amp;gt;&amp;lt;/zz&amp;gt;&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;The above output is obtained on adding the significant space before the first &lt;br /&gt;child, using the InsertBefore function, in lieu of the InsertAfter function. The &lt;br /&gt;spaces are inserted before the FirstChild aa, i.e. before the text 'Vijay' and &lt;br /&gt;not after.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;a.cs&lt;br&gt;&lt;br /&gt;using System;&lt;br&gt;&lt;br /&gt;using System.Collections;&lt;br&gt;&lt;br /&gt;using System.Xml;&lt;br&gt;&lt;br /&gt;public class zzz&lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;public static void Main()&lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;XmlDocument d = new XmlDocument();&lt;br&gt;&lt;br /&gt;d.Load(&amp;quot;books.xml&amp;quot;);&lt;br&gt;&lt;br /&gt;XmlNode n = d.DocumentElement;&lt;br&gt;&lt;br /&gt;IEnumerator i = n.GetEnumerator();&lt;br&gt;&lt;br /&gt;XmlNode b;&lt;br&gt;&lt;br /&gt;while (i.MoveNext()) &lt;br&gt;&lt;br /&gt;{ &lt;br&gt;&lt;br /&gt;b = (XmlNode)i.Current;&lt;br&gt;&lt;br /&gt;Console.WriteLine(b.OuterXml);&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;Output&lt;br&gt;&lt;br /&gt;&amp;lt;book genre=&amp;quot;autobiography&amp;quot; publicationdate=&amp;quot;1981&amp;quot; &lt;br /&gt;ISBN=&amp;quot;1-861003-11-0&amp;quot;&amp;gt;&amp;lt;title&amp;gt;The Autobiography of Benjamin &lt;br /&gt;Franklin&amp;lt;/title&amp;gt;&amp;lt;author&amp;gt;&amp;lt;first-name&amp;gt;Benjamin&amp;lt;/first-name&amp;gt;&amp;lt;last-name&amp;gt; &lt;br /&gt;Franklin&amp;lt;/last-name&amp;gt;&amp;lt;/author&amp;gt;&amp;lt;price&amp;gt;8.99&amp;lt;/price&amp;gt;&amp;lt;/book&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;book genre=&amp;quot;novel&amp;quot; publicationdate=&amp;quot;1967&amp;quot; ISBN=&amp;quot;0-201-63361-2&amp;quot;&amp;gt;&amp;lt;title&amp;gt;The &lt;br /&gt;Confidence Man&amp;lt;/title&amp;gt;&amp;lt;author&amp;gt;&amp;lt;first-name&amp;gt; &lt;br /&gt;Herman&amp;lt;/first-name&amp;gt;&amp;lt;last-name&amp;gt;Melville&amp;lt;/last-name&amp;gt;&amp;lt;/author&amp;gt; &lt;br /&gt;&amp;lt;price&amp;gt;11.99&amp;lt;/price&amp;gt;&amp;lt;/book&amp;gt;&amp;lt;book genre=&amp;quot;philosophy&amp;quot; publicationdate=&amp;quot;1991&amp;quot; &lt;br /&gt;ISBN=&amp;quot;1-861001-57-6&amp;quot;&amp;gt;&amp;lt;title&amp;gt;The&lt;br&gt;&lt;br /&gt;Gorgias&amp;lt;/title&amp;gt;&amp;lt;author&amp;gt;&amp;lt;name&amp;gt;Plato&amp;lt;/name&amp;gt;&amp;lt;/author&amp;gt;&amp;lt;price&amp;gt; 9.99&amp;lt;/price&amp;gt;&amp;lt;/book&amp;gt;&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;The last example in the series, displays various parts of an XML file in a &lt;br /&gt;different manner.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;Every XML node object has a function called GetEnumerator, which returns an &lt;br /&gt;IEnumerator interface object. This interface has a MoveNext function that moves &lt;br /&gt;from one node to the next. In our program, we move from one book to another, &lt;br /&gt;since the XmlNode is on a tag book. &lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;The Current property accesses the current book node, since it happens to be the &lt;br /&gt;first one. Then, we use the OuterXml property to display the entire contents of &lt;br /&gt;this node. &lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;The MoveNext function then activates the next node. If no more nodes exist, it &lt;br /&gt;returns False, or else it returns True. In this manner, we can iterate through &lt;br /&gt;all the nodes. The foreach instruction has a similar functionality. Whenever we &lt;br /&gt;want to navigate through a collection, the IEnumerator interface is used. This &lt;br /&gt;is the conventional way of moving sequentially through a list of objects or a &lt;br /&gt;collection in C#.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;&amp;nbsp;&lt;/p&gt;</description><thr:total xmlns:thr="http://purl.org/syndication/thread/1.0">0</thr:total></item><item><title>C Language  Help, C Language Tutorials, C Language Programming, C Language Tricks  { The C# Language }</title><link>http://swebsolution.blogspot.com/2007/12/c-language-dtd-c-language.html</link><author>noreply@blogger.com (Unknown)</author><pubDate>Wed, 26 Dec 2007 04:06:00 -0800</pubDate><guid isPermaLink="false">tag:blogger.com,1999:blog-7227425366482465228.post-1408925094216043457</guid><description>&lt;p align="justify"&gt;&lt;b&gt;C Language  Help, C Language Tutorials, C Language Programming, C Language Tricks &lt;/b&gt;&lt;br&gt;The DTD&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;Validations in XML&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;So far, we have only read an XML file, without catering to special cases, &lt;br /&gt;wherein, either an entity has been used, or data has to be validated as per the &lt;br /&gt;element. The XmlTextReader class is the most optimum choice for reading an XML &lt;br /&gt;file, barring the cases where data has to be validated, or in cases where an &lt;br /&gt;entity has to be replaced with a value. For such purposes, the &lt;br /&gt;XmlValidatingReader class is more suited. This class is derived from XmlReader, &lt;br /&gt;and it conducts three types of validations- DTD, XDR and XSD schema validations.&lt;br /&gt;&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;This class is used when the primary task is either to conduct data validations &lt;br /&gt;or to resolve general entities or to provide support for default entities. &lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;a.cs&lt;br&gt;&lt;br /&gt;using System;&lt;br&gt;&lt;br /&gt;using System.IO;&lt;br&gt;&lt;br /&gt;using System.Xml;&lt;br&gt;&lt;br /&gt;public class zzz &lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;public static void Main()&lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;XmlValidatingReader r = null;&lt;br&gt;&lt;br /&gt;XmlParserContext p;&lt;br&gt;&lt;br /&gt;p = new XmlParserContext(null, null, &amp;quot;vijay&amp;quot;, null, null, &amp;quot;&amp;lt;!ENTITY pr &lt;br /&gt;'100'&amp;gt;&amp;quot;,&amp;quot;&amp;quot;,&amp;quot;&amp;quot;, XmlSpace.None);&lt;br&gt;&lt;br /&gt;r = new XmlValidatingReader (&amp;quot;&amp;lt;vijay mukhi='great' price='Rs &amp;amp;pr;'&amp;gt;&amp;lt;/vijay&amp;gt;&amp;quot;, &lt;br /&gt;XmlNodeType.Element, p);&lt;br&gt;&lt;br /&gt;r.ValidationType = ValidationType.None;&lt;br&gt;&lt;br /&gt;r.MoveToContent();&lt;br&gt;&lt;br /&gt;while (r.MoveToNextAttribute())&lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;Console.WriteLine(&amp;quot;{0} = {1}&amp;quot;, r.Name, r.Value);&lt;br&gt;&lt;br /&gt;} &lt;br&gt;&lt;br /&gt;r.Close();&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;Output&lt;br&gt;&lt;br /&gt;mukhi = great&lt;br&gt;&lt;br /&gt;price = Rs 100&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;To create the object p of type XmlParserContext, the constructor with nine &lt;br /&gt;parameters of XmlParserContext class is called. The nine parameters are as &lt;br /&gt;follows:&lt;br&gt;&lt;br /&gt;• The first parameter refers to the NameTable type. It has a value of null.&lt;br&gt;&lt;br /&gt;• The second parameter refers to NamespaceManager type. It also has a value of &lt;br /&gt;null.&lt;br&gt;&lt;br /&gt;• The third Parameter is the DocType, i.e. the root tag 'vijay'.&lt;br&gt;&lt;br /&gt;• The fourth parameter is the pubid for the external DTD file. &lt;br&gt;&lt;br /&gt;• The fifth parameter is the sysid for the external DTD file. &lt;br&gt;&lt;br /&gt;• The sixth parameter is the internal DTD, where an ENTITY declaration &amp;lt;!ENTITY &lt;br /&gt;pr '100'&amp;gt; has been created. This simply states that the word 'pr' is preceded by &lt;br /&gt;a '&amp;amp;' and followed by a semi-colon must be replaced with the string '100'. &lt;br&gt;&lt;br /&gt;• The seventh parameter in sequence is the location from where the fragment is &lt;br /&gt;to be loaded, i.e. the base URI.&lt;br&gt;&lt;br /&gt;• The eighth parameter stands for the xml:lang scope.&lt;br&gt;&lt;br /&gt;• The ninth parameter stands for the xml:space scope. &lt;br&gt;&lt;br /&gt;The parameters to the constructor of XmlValidatingReader class are similar to &lt;br /&gt;those of the XmlTextReader, which we had encountered earlier. This class is &lt;br /&gt;derived from the XmlTextReader as well as the IXmlLineInfo interface.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;There are five different values that a Validationtype can be initialized to: &lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;1. The first is Auto, which validates only when the DTD or schema information is &lt;br /&gt;found. &lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;2. The second is DTD, which validates based on the instructions found in the DTD.&lt;br /&gt;&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;3. The third option, which creates an XML 1.0 non-validation parser, validates &lt;br /&gt;the default attributes and resolves entities without using the DOCTYPE. Thus, if &lt;br /&gt;the root tag is changed from 'vijay' to 'vijay1', no errors will be generated. &lt;br /&gt;Placing the ValidationType statement within comments will generate the following &lt;br /&gt;exception: &lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;&amp;quot;Unhandled Exception: System.Xml.Schema.XmlSchemaException: The root element &lt;br /&gt;name must match the DocType name. An error occurred at (1, 2).&amp;quot;&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;4. The fourth option is XSD, which validates as per the XSD schemas. &lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;5. The fifth option is XDR, which validates as per the XDR schemas. In our &lt;br /&gt;program we have set this property to a value of None. &lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;Once the required properties are set, the MoveToContent function is used to move &lt;br /&gt;to the first element, 'vijay'. The next function, MoveToNextAttribute returns a &lt;br /&gt;value of True when there are attributes remaining to be read. Otherwise, it &lt;br /&gt;returns a value of False. In our case, it is similar to the MoveToFirstElement &lt;br /&gt;function. &lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;The while loop repeats twice, since there are two attributes. The Name and Value &lt;br /&gt;properties for the first attribute are displayed as 'mukhi' and 'great'. This is &lt;br /&gt;very similar to what we have observed in the earlier program. The name for the &lt;br /&gt;second attribute is displayed as 'price'. However, its value is not the same, &lt;br /&gt;because it has an entity &amp;amp;pr;. The XmlValidatingReader replaces the entity pr &lt;br /&gt;with the string '100', prior to displaying the value. Therefore, the output is &lt;br /&gt;displayed as 'price' and 'Rs. 100'.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;a.cs&lt;br&gt;&lt;br /&gt;using System;&lt;br&gt;&lt;br /&gt;using System.IO;&lt;br&gt;&lt;br /&gt;using System.Xml;&lt;br&gt;&lt;br /&gt;using System.Xml.Schema;&lt;br&gt;&lt;br /&gt;class zzz&lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;public static void Main()&lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;XmlTextReader r = new XmlTextReader(&amp;quot;b.xml&amp;quot;);&lt;br&gt;&lt;br /&gt;XmlValidatingReader v = new XmlValidatingReader(r);&lt;br&gt;&lt;br /&gt;v.ValidationType = ValidationType.DTD;&lt;br&gt;&lt;br /&gt;v.ValidationEventHandler += new ValidationEventHandler (abc);&lt;br&gt;&lt;br /&gt;while(v.Read());&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;public static void abc(object s, ValidationEventArgs a)&lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;Console.WriteLine(&amp;quot;Severity:{0}&amp;quot;, a.Severity);&lt;br&gt;&lt;br /&gt;Console.WriteLine(&amp;quot;Message:{0}&amp;quot;, a.Message);&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;b.xml&lt;br&gt;&lt;br /&gt;&amp;lt;?xml version=&amp;quot;1.0&amp;quot; ?&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;!DOCTYPE vijay1 &amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;vijay&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;/vijay&amp;gt;&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;Output&lt;br&gt;&lt;br /&gt;Severity:Error&lt;br&gt;&lt;br /&gt;Message:The root element name must match the DocType name. An error occurred at &lt;br /&gt;file:///c:/csharp/b.xml(3, 2).&lt;br&gt;&lt;br /&gt;Severity:Error&lt;br&gt;&lt;br /&gt;Message:The 'vijay' element is not declared. An error occurred at file:///c:/csharp/b.xml(3, &lt;br /&gt;2).&lt;br&gt;&lt;br /&gt;In the above program, to begin with, an object r that looks like XmlTextReader &lt;br /&gt;is created, and then, it is passed to the constructor of XmlValidatingReader, &lt;br /&gt;while object v is being created. The ValidationType of the object v is modified &lt;br /&gt;to DTD. The ValidationEventHandler event is set to the function abc, which gets &lt;br /&gt;called whenever an error occurs. Under the aegis of the Read function, the &lt;br /&gt;entire XML file is validated, using the while loop, and the function abc is &lt;br /&gt;notified whenever an error is chanced upon. &lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;In the function abc, the values contained in the properties - Severity and &lt;br /&gt;Message, of the ValidationEventArgs parameter 'a', are printed. The Severity &lt;br /&gt;property reveals whether it is an error or warning, whereas, the Message &lt;br /&gt;property contains the precise text of the error or warning. &lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;In the above case, an error is generated because the DOCTYPE expects the root &lt;br /&gt;element to be 'vijay1', whereas, it has been specified as 'vijay'. When no error &lt;br /&gt;message is displayed, it may be inferred that no errors have been found. &lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;The DTD&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;Using the above C# program, we shall now create our own DTD file. Therefore, we &lt;br /&gt;shall modify only the b.xml and b.dtd files. &lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;b.xml&lt;br&gt;&lt;br /&gt;&amp;lt;?xml version=&amp;quot;1.0&amp;quot; ?&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;!DOCTYPE vijay SYSTEM &amp;quot;b.dtd&amp;quot; &amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;vijay /&amp;gt;&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;b.dtd&lt;br&gt;&lt;br /&gt;&amp;lt;!ELEMENT vijay &amp;gt;&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;A DTD is generally very protracted. So, an internal DTD is rarely used. If it is &lt;br /&gt;used, its contents have to be placed within [] brackets. To use an external DTD, &lt;br /&gt;we use the words SYSTEM followed by the name of the DTD file, which is b.dtd, in &lt;br /&gt;this case. &lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;In b.dtd, an element 'vijay' is created by inserting the reserved characters &lt;br /&gt;'&amp;lt;!', followed by ELEMENT, and finally by the element name 'vijay'. When we run &lt;br /&gt;the C# program 'a', the following error is generated:&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;Output&lt;br&gt;&lt;br /&gt;Unhandled Exception: System.Xml.XmlException: This is an invalid content model. &lt;br /&gt;Line 1, position 17.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;An error in the DTD file has resulted in the generation of an un-handled &lt;br /&gt;exception. The error occurred due to an incomplete ELEMENT statement.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;b.dtd&lt;br&gt;&lt;br /&gt;&amp;lt;!ELEMENT vijay EMPTY&amp;gt;&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;The addition of the word EMPTY salvages the situation. By specifying the word &lt;br /&gt;EMPTY, it is amply clear that the element named 'vijay' is an empty element.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;b.xml&lt;br&gt;&lt;br /&gt;&amp;lt;?xml version=&amp;quot;1.0&amp;quot; ?&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;!DOCTYPE vijay SYSTEM &amp;quot;b.dtd&amp;quot; &amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;vijay&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;/vijay&amp;gt;&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;Output&lt;br&gt;&lt;br /&gt;Severity:Error&lt;br&gt;&lt;br /&gt;Message:Element 'vijay' has invalid child element '#PCDATA'. An error occurred &lt;br /&gt;at file:///c:/csharp/b.xml(3, 8).&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;The DTD file states, with absolute clarity, that the ELEMENT 'vijay' is EMPTY. &lt;br /&gt;However, an open tag &amp;lt;vijay&amp;gt; and a close tag &amp;lt;/vijay&amp;gt;have been added to the XML &lt;br /&gt;file. Therefore, an error message is generated, which, as usual, is &lt;br /&gt;unintelligible. &lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;Instead of using tags such as 'vijay', let us consider a DTD that has been &lt;br /&gt;implemented in real life. This one is used for the WML, or the Wireless Markup &lt;br /&gt;Language. The rules or syntax of WML are available as a DTD. &lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;In our book titled 'WML and WMLScript', we have endeavoured to elucidate the &lt;br /&gt;concept of a DTD. You are at liberty to refer to the book. However, we must &lt;br /&gt;caution you that, the approach and the explanation used here is entirely at &lt;br /&gt;variance with the one used in the earlier book.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;b.xml&lt;br&gt;&lt;br /&gt;&amp;lt;?xml version=&amp;quot;1.0&amp;quot; ?&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;!DOCTYPE wml SYSTEM &amp;quot;b.dtd&amp;quot; &amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;wml&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;/wml&amp;gt;&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;b.dtd&lt;br&gt;&lt;br /&gt;&amp;lt;!ELEMENT wml EMPTY&amp;gt;&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;Output&lt;br&gt;&lt;br /&gt;Severity:Error&lt;br&gt;&lt;br /&gt;Message:Element 'wml' has invalid child element '#PCDATA'. An error occurred at &lt;br /&gt;file:///C:/csharp/b.xml(3, 6).&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;The word 'vijay' has merely been replaced by the word 'wml'. The error generated &lt;br /&gt;is akin to the earlier one. At this juncture, we introduce a 'card' into the DTD &lt;br /&gt;file.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;b.dtd&lt;br&gt;&lt;br /&gt;&amp;lt;!ELEMENT wml (card)&amp;gt;&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;Output&lt;br&gt;&lt;br /&gt;Severity:Error&lt;br&gt;&lt;br /&gt;Message:Element 'wml' has incomplete content. Expected 'card'. An error occurred &lt;br /&gt;at file:///c:/csharp/b.xml(4, 3).&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;Every WML document must commence with the root tag 'wml'. In the DTD file, we &lt;br /&gt;have placed the word 'card' within round brackets, along with wml. This &lt;br /&gt;signifies that the wml tag must contain a tag or an element called 'card'. Since &lt;br /&gt;there is no card in the XML file, an error is reported, stating that a card is &lt;br /&gt;expected, and on account of its unavailability, the wml element is incomplete.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;b.xml&lt;br&gt;&lt;br /&gt;&amp;lt;?xml version=&amp;quot;1.0&amp;quot; ?&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;!DOCTYPE wml SYSTEM &amp;quot;b.dtd&amp;quot; &amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;wml&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;card /&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;/wml&amp;gt;&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;Output&lt;br&gt;&lt;br /&gt;Severity:Error&lt;br&gt;&lt;br /&gt;Message:The 'card' element is not declared. An error occurred at file:///c:/csharp/b.xml(4, &lt;br /&gt;2)&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;We add the card tag as a single tag to our XML file, in an endeavour to &lt;br /&gt;eliminate the error. But, as we have not specified 'card' as a valid element in &lt;br /&gt;the DTD file, yet another error message is displayed. Unless 'card' appears as &lt;br /&gt;an ELEMENT in the DTD file, it is not possible to use it in the XML file. &lt;br /&gt;Therefore, we now include 'card' as an EMPTY element in b.dtd&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;b.dtd&lt;br&gt;&lt;br /&gt;&amp;lt;!ELEMENT wml (card)&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;!ELEMENT card EMPTY&amp;gt;&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;Now, all the errors just vanish. In the DTD file, we had affirmed that the &lt;br /&gt;element 'card' shall be empty i.e. it will not have any content. &lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;The XML file depicted below displays an error, because the 'card' tag is not a &lt;br /&gt;single tag any longer.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;b.xml&lt;br&gt;&lt;br /&gt;&amp;lt;?xml version=&amp;quot;1.0&amp;quot; ?&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;!DOCTYPE wml SYSTEM &amp;quot;b.dtd&amp;quot; &amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;wml&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;card&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;/card&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;/wml&amp;gt;&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;Output&lt;br&gt;&lt;br /&gt;Severity:Error&lt;br&gt;&lt;br /&gt;Message:Element 'card' has invalid child element '#PCDATA'. An error occurred at &lt;br /&gt;file:///C:/csharp/b.xml(4, 7).&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;The error message displayed here is very similar to the one seen with the wml &lt;br /&gt;tag.&lt;br&gt;&lt;br /&gt;The element 'wml' has an invalid child element '#PCDATA'&lt;br&gt;&lt;br /&gt;A slight modification to the XML file is desirable, before we endeavour to &lt;br /&gt;eliminate the error. &lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;b.xml&lt;br&gt;&lt;br /&gt;&amp;lt;?xml version=&amp;quot;1.0&amp;quot; ?&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;!DOCTYPE wml SYSTEM &amp;quot;b.dtd&amp;quot; &amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;wml&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;card&amp;gt;&lt;br&gt;&lt;br /&gt;hi&lt;br&gt;&lt;br /&gt;&amp;lt;/card&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;/wml&amp;gt;&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;Output&lt;br&gt;&lt;br /&gt;Severity:Error&lt;br&gt;&lt;br /&gt;Message:Element 'card' has invalid child element 'Text'. An error occurred at &lt;br /&gt;file:///c:/csharp/b.xml(4, 7).&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;Inserting the word 'hi' between the card tags results in a slightly altered &lt;br /&gt;error messages. In place of PCDATA, we get to see Text. Resorting to the &lt;br /&gt;following modifications to the DTD file, both the error messages can be &lt;br /&gt;eliminated.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;b.dtd&lt;br&gt;&lt;br /&gt;&amp;lt;!ELEMENT wml (card)&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;!ELEMENT card (#PCDATA)&amp;gt;&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;To eradicate the errors, the EMPTY word is replaced with #PCDATA, enclosed &lt;br /&gt;within round brackets. The word PCDATA is an acronym for Parseable Character &lt;br /&gt;Data. In plain English, it represents text that can be entered from the &lt;br /&gt;keyboard. Thus, we are at liberty to write as many lines of text as we want, &lt;br /&gt;within the card tag. Even if the word 'hi' is removed from within the tags, no &lt;br /&gt;error is generated. &lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;Our DTD expects a root tag or starting tag of wml. Only a card tag can be &lt;br /&gt;inserted amidst within this tag, which is capable of containing limitless &lt;br /&gt;content. Insertion of anything else in this tag is a sure recipe for disaster.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;b.xml&lt;br&gt;&lt;br /&gt;&amp;lt;?xml version=&amp;quot;1.0&amp;quot; ?&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;!DOCTYPE wml SYSTEM &amp;quot;b.dtd&amp;quot; &amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;wml&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;card&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;/card&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;card&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;/card&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;/wml&amp;gt;&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;Output&lt;br&gt;&lt;br /&gt;Severity:Error&lt;br&gt;&lt;br /&gt;Message:Element 'wml' has invalid content. Expected ''. An error occurred at &lt;br /&gt;file:///c:/csharp/b.xml(6, 2).&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;The above error has occurred because, the DTD clearly specifies that the root &lt;br /&gt;tag wml must have one, and only one, occurrence of the tag called 'card' within &lt;br /&gt;it. Here, we have created two tags, thereby, causing the error.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;b.dtd&lt;br&gt;&lt;br /&gt;&amp;lt;!ELEMENT wml (card)*&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;!ELEMENT card (#PCDATA)&amp;gt;&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;The * symbol, placed after the round brackets, is indicative of the fact that, &lt;br /&gt;it can be replaced with zero to infinite values. Thus, the XML file can now &lt;br /&gt;either have zero or countless card elements. If you do not give credence to this &lt;br /&gt;statement of ours, you may either delete all the card elements from the XML &lt;br /&gt;file, or add numerous cards. Either way, no error will be generated.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;b.dtd&lt;br&gt;&lt;br /&gt;&amp;lt;!ELEMENT wml (card)+&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;!ELEMENT card (#PCDATA)&amp;gt;&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;Replacing the symbol * with a + transforms the meaning from 'zero to infinity' &lt;br /&gt;to 'one to infinity'. The only difference between the * symbol and the + symbol &lt;br /&gt;is that, the + sign mandates at least one occurrence of the element whereas, the &lt;br /&gt;* signs makes it optional. Thus, in the aboveXMLfile, at least a single card &lt;br /&gt;element is required.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;b.dtd&lt;br&gt;&lt;br /&gt;&amp;lt;!ELEMENT wml (card)?&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;!ELEMENT card (#PCDATA)&amp;gt;&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;The last of the special characters is the symbol ? that specifies the number of &lt;br /&gt;elements to be from 'zero or one'. Thus in the XML file, we may have either one &lt;br /&gt;card element or none at all. The presence of two or more cards will generate an &lt;br /&gt;error. You should try out various possible combinations for each of the symbols &lt;br /&gt;*, + and?.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;b.xml&lt;br&gt;&lt;br /&gt;&amp;lt;?xml version=&amp;quot;1.0&amp;quot; ?&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;!DOCTYPE wml SYSTEM &amp;quot;b.dtd&amp;quot; &amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;wml&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;card&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;p&amp;gt; hi &amp;lt;/p&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;/card&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;/wml&amp;gt;&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;b.dtd&lt;br&gt;&lt;br /&gt;&amp;lt;!ELEMENT wml (card)*&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;!ELEMENT card (p)&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;!ELEMENT p (#PCDATA)&amp;gt;&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;No error is generated because, in the DTD file, we have now stated that, the &lt;br /&gt;card element can have a tag p, which can contain any text. We have, however, &lt;br /&gt;done away with the provision of placing any text within the card tag. &lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;Add in a new modification to the file.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;b.xml&lt;br&gt;&lt;br /&gt;&amp;lt;?xml version=&amp;quot;1.0&amp;quot; ?&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;!DOCTYPE wml SYSTEM &amp;quot;b.dtd&amp;quot; &amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;wml&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;card&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;p&amp;gt; &amp;lt;b/&amp;gt; &amp;lt;/p&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;/card&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;/wml&amp;gt;&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;b.dtd&lt;br&gt;&lt;br /&gt;&amp;lt;!ELEMENT wml (card)*&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;!ELEMENT card (p)&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;!ELEMENT p (br | b)&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;!ELEMENT br EMPTY&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;!ELEMENT b EMPTY&amp;gt;&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;The DTD appears extensively complicated. The p tag is now competent of &lt;br /&gt;containing only two tags, br and b. Text is not allowed any more. The | sign &lt;br /&gt;signifies the OR condition, which implies that either tag b or tag br is &lt;br /&gt;allowed. The two aforesaid tags are defined as EMPTY tags. To summarise, our DTD &lt;br /&gt;states that the p tag can contain a single tag of either b or br.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;b.xml&lt;br&gt;&lt;br /&gt;&amp;lt;?xml version=&amp;quot;1.0&amp;quot; ?&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;!DOCTYPE wml SYSTEM &amp;quot;b.dtd&amp;quot; &amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;wml&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;card&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;p&amp;gt; &amp;lt;b/&amp;gt; &amp;lt;br/&amp;gt;&amp;lt;/p&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;/card&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;/wml&amp;gt;&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;Output&lt;br&gt;&lt;br /&gt;Severity:Error&lt;br&gt;&lt;br /&gt;Message:Element 'p' has invalid content. Expected ''. An error occurred at &lt;br /&gt;file:///c:/csharp/b.xml(5, 11). &lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;All is not well, because we are allowed to place either a 'b' or a 'br' at a &lt;br /&gt;time, but not both together. To remedy the situation, we place a * symbol after &lt;br /&gt;the p tag.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;b.dtd&lt;br&gt;&lt;br /&gt;&amp;lt;!ELEMENT wml (card)*&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;!ELEMENT card (p)*&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;!ELEMENT p (br | b)*&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;!ELEMENT br EMPTY&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;!ELEMENT b EMPTY&amp;gt;&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;The above DTD provides us the flexibility of having multiple p tags within n &lt;br /&gt;number of cards. These, in turn, may have as many b or br tags as desired. &lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;By replacing the b tag with #PCDATA, a p tag is in a position to accommodate &lt;br /&gt;multiple br tags, as well as an indefinite amount of text. &lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;&amp;lt;!ELEMENT p (br | #PCDATA)*&amp;gt;&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;b.xml&lt;br&gt;&lt;br /&gt;&amp;lt;?xml version=&amp;quot;1.0&amp;quot; ?&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;!DOCTYPE wml SYSTEM &amp;quot;b.dtd&amp;quot; &amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;wml&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;card /&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;head /&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;/wml&amp;gt;&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;b.dtd&lt;br&gt;&lt;br /&gt;&amp;lt;!ELEMENT wml (card,head)&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;!ELEMENT card EMPTY&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;!ELEMENT head EMPTY&amp;gt;&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;The above DTD file permits the wml tag to contain a card tag, which is then to &lt;br /&gt;be strictly followed by a head tag. The comma signifies that one tag is to be &lt;br /&gt;followed by the other. If we refrain from using the head tag in the XML file, &lt;br /&gt;the following error message will be generated:&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;Output&lt;br&gt;&lt;br /&gt;Severity:Error&lt;br&gt;&lt;br /&gt;Message:Element 'wml' has incomplete content. Expected 'head'. An error occurred &lt;br /&gt;at file:///C:/csharp/b.xml(5, 3).&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;b.xml&lt;br&gt;&lt;br /&gt;&amp;lt;?xml version=&amp;quot;1.0&amp;quot; ?&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;!DOCTYPE wml SYSTEM &amp;quot;b.dtd&amp;quot; &amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;wml&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;head /&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;card /&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;/wml&amp;gt;&lt;br&gt;&lt;br /&gt;Output&lt;br&gt;&lt;br /&gt;Severity:Error&lt;br&gt;&lt;br /&gt;Message:Element 'wml' has invalid content. Expected 'card'. An error occurred at &lt;br /&gt;file:///c:/csharp/b.xml(4, 2).&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;If the order of the tags is interchanged, an error is thrown. The card tag must &lt;br /&gt;be followed by the head tag. Besides, there is a restriction imposed that there &lt;br /&gt;can be only one insertion of each tag. If there are multiple insertions, it will &lt;br /&gt;result in an error.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;b.xml&lt;br&gt;&lt;br /&gt;&amp;lt;?xml version=&amp;quot;1.0&amp;quot; ?&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;!DOCTYPE wml SYSTEM &amp;quot;b.dtd&amp;quot; &amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;wml&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;card /&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;card /&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;head /&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;/wml&amp;gt;&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;b.dtd&lt;br&gt;&lt;br /&gt;&amp;lt;!ELEMENT wml (card+,head?)&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;!ELEMENT card EMPTY&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;!ELEMENT head EMPTY&amp;gt;&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;When the plus sign is inserted after the card, it allows the use of more that &lt;br /&gt;one card tag in the file. The ? sign denotes 'zero or one' insertions of the &lt;br /&gt;head tag. Thus, we can have more than one card tag and have either a single head &lt;br /&gt;tag or none at all. If the head tag is present, it must be placed after the card &lt;br /&gt;tag, since the order of the tags is sacrosanct. &lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;b.xml&lt;br&gt;&lt;br /&gt;&amp;lt;?xml version=&amp;quot;1.0&amp;quot; ?&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;!DOCTYPE wml SYSTEM &amp;quot;b.dtd&amp;quot; &amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;wml&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;card /&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;head /&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;card /&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;/wml&amp;gt;&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;Output&lt;br&gt;&lt;br /&gt;Severity:Error&lt;br&gt;&lt;br /&gt;Message:Element 'wml' has invalid content. Expected ''. An error occurred at &lt;br /&gt;file:///c:/csharp/b.xml(6, 2).&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;The Draconian restrictions imposed by the DTD file prohibit us from altering the &lt;br /&gt;sequence of the above tags. The card tag has to come first, followed by the head &lt;br /&gt;tag. We cannot interchange a head tag with a card tag. So, the only solution to &lt;br /&gt;this problem is to abide by the stipulated sequence.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;b.dtd&lt;br&gt;&lt;br /&gt;&amp;lt;!ELEMENT wml (card+,head?,template*)*&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;!ELEMENT card EMPTY&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;!ELEMENT head EMPTY&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;!ELEMENT template EMPTY&amp;gt;&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;In the DTD file, we have added a * symbol to the entire set of tags, which make &lt;br /&gt;up the wml element. The set consists of the following individual elements in a &lt;br /&gt;sequential order:&lt;br&gt;&lt;br /&gt;• More than one card tags.&lt;br&gt;&lt;br /&gt;• Zero or one head tag. &lt;br&gt;&lt;br /&gt;• Zero to many template tags. &lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;This set can constitute of numerous permutations and combinations of the above &lt;br /&gt;conditions, in the specified order. Thus, the card and head can appear together, &lt;br /&gt;or the card can appear by itself without the head tag, or the template tag may &lt;br /&gt;not be present at all, and so on. Every occurrence, however, needs to begin with &lt;br /&gt;a card tag.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;b.xml&lt;br&gt;&lt;br /&gt;&amp;lt;?xml version=&amp;quot;1.0&amp;quot; ?&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;!DOCTYPE wml SYSTEM &amp;quot;b.dtd&amp;quot; &amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;wml&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;card aa=&amp;quot;hi&amp;quot;/&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;/wml&amp;gt;&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;b.dtd&lt;br&gt;&lt;br /&gt;&amp;lt;!ELEMENT wml (card)&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;!ELEMENT card EMPTY&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;!ATTLIST card aa CDATA #IMPLIED&amp;gt;&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;In the above example, the card tag has an attribute called aa initialized to &lt;br /&gt;'hi'. To implement an attribute, we include the word ATTLIST, which is a short &lt;br /&gt;form for 'a list of attributes', in the DTD file. This is followed by the name &lt;br /&gt;of the tag that the attribute is associated with. Then, the actual name of the &lt;br /&gt;attribute aa is specified, followed by the datatype it will hold, which is &lt;br /&gt;character data, in our case. The last parameter, #IMPLIED permits the attribute &lt;br /&gt;aa to be optional. Therefore, even if you remove it, no error will be generated.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;b.xml&lt;br&gt;&lt;br /&gt;&amp;lt;?xml version=&amp;quot;1.0&amp;quot; ?&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;!DOCTYPE wml SYSTEM &amp;quot;b.dtd&amp;quot; &amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;wml&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;card /&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;/wml&amp;gt;&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;b.dtd&lt;br&gt;&lt;br /&gt;&amp;lt;!ELEMENT wml (card)&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;!ELEMENT card EMPTY&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;!ATTLIST card aa CDATA #IMPLIED bb CDATA #REQUIRED&amp;gt;&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;Output&lt;br&gt;&lt;br /&gt;Severity:Error&lt;br&gt;&lt;br /&gt;Message:The required attribute 'bb' is missing. An error occurred at file:///c:/csharp/b.xml(4, &lt;br /&gt;2).&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;The error message clearly mentions that the attribute bb is missing. The &lt;br /&gt;#REQUIRED demands the presence of attribute bb, along with the card, whenever &lt;br /&gt;the card tag is used. Further, the attributes are to be placed one after the &lt;br /&gt;other. However, the order of placement is not significant. &lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;b.xml&lt;br&gt;&lt;br /&gt;&amp;lt;?xml version=&amp;quot;1.0&amp;quot; ?&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;!DOCTYPE wml SYSTEM &amp;quot;b.dtd&amp;quot; &amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;wml&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;card bb=&amp;quot;no&amp;quot;/&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;/wml&amp;gt;&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;No errors are generated since the attribute bb, which is mandatory, has been &lt;br /&gt;specified. You can avoid aa, since it is implied.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;b.xml&lt;br&gt;&lt;br /&gt;&amp;lt;?xml version=&amp;quot;1.0&amp;quot; ?&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;!DOCTYPE wml SYSTEM &amp;quot;b.dtd&amp;quot; &amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;wml&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;card aa=&amp;quot;no&amp;quot;/&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;/wml&amp;gt;&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;b.dtd&lt;br&gt;&lt;br /&gt;&amp;lt;!ELEMENT wml (card)&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;!ELEMENT card EMPTY&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;!ATTLIST card aa (hi | bye ) &amp;quot;bye&amp;quot;&amp;gt;&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;Output&lt;br&gt;&lt;br /&gt;Severity:Error&lt;br&gt;&lt;br /&gt;Message:'no' is not in the enumeration list. An error occurred at file:///c:/csharp/b.xml(4, &lt;br /&gt;7).&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;The values assigned to attributes can be restricted to specific values. This can &lt;br /&gt;be achieved by specifying the values along with ATTLIST in the DTD file and &lt;br /&gt;using the OR sign (|) as the separator. The attribute aa can only be assigned &lt;br /&gt;the value of either 'hi' or 'bye'. Specifying any other value would result in an &lt;br /&gt;error.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;If the attribute is not initialized, it assumes the default value of 'bye'.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;b.xml&lt;br&gt;&lt;br /&gt;&amp;lt;?xml version=&amp;quot;1.0&amp;quot; ?&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;!DOCTYPE wml SYSTEM &amp;quot;b.dtd&amp;quot; &amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;wml&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;card aa=&amp;quot;hi&amp;quot;/&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;/wml&amp;gt;&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;The error disappears because the attribute has been assigned a value of 'hi'.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;b.xml&lt;br&gt;&lt;br /&gt;&amp;lt;?xml version=&amp;quot;1.0&amp;quot; ?&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;!DOCTYPE wml SYSTEM &amp;quot;b.dtd&amp;quot; &amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;wml&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;card aa=&amp;quot;hi&amp;quot;/&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;/wml&amp;gt;&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;b.dtd&lt;br&gt;&lt;br /&gt;&amp;lt;!ELEMENT wml (card)*&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;!ELEMENT card EMPTY&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;!ATTLIST card aa ID #IMPLIED&amp;gt;&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;We have created an attribute aa, with a data type of ID. This does not result in &lt;br /&gt;any error. &lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;b.xml&lt;br&gt;&lt;br /&gt;&amp;lt;?xml version=&amp;quot;1.0&amp;quot; ?&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;!DOCTYPE wml SYSTEM &amp;quot;b.dtd&amp;quot; &amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;wml&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;card aa=&amp;quot;hi&amp;quot;/&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;card aa=&amp;quot;hi&amp;quot;/&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;/wml&amp;gt;&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;Output&lt;br&gt;&lt;br /&gt;Severity:Error&lt;br&gt;&lt;br /&gt;Message:'hi' is already used as an ID. An error occurred at file:///c:/csharp/b.xml(5, &lt;br /&gt;7).&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;The card tag can be used multiple times, due to the presence of the * sign in &lt;br /&gt;the DTD file. By associating the type of ID to the attribute aa, it is &lt;br /&gt;guaranteed that the same value of 'hi' is not assigned to the attribute. The &lt;br /&gt;error message conveys that 'hi' has already been assigned as an ID to the &lt;br /&gt;attribute aa, and hence, it cannot be used again.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;b.xml&lt;br&gt;&lt;br /&gt;&amp;lt;?xml version=&amp;quot;1.0&amp;quot; ?&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;!DOCTYPE wml SYSTEM &amp;quot;b.dtd&amp;quot; &amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;wml&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;card aa=&amp;quot;hi&amp;quot;/&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;card aa=&amp;quot;hi1&amp;quot;/&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;/wml&amp;gt;&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;If we assign a different value to the attribute, the error is dispensed with. &lt;br /&gt;Thus, a data type of ID guarantees that the attribute shall never have a &lt;br /&gt;duplicate value.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;b.xml&lt;br&gt;&lt;br /&gt;&amp;lt;?xml version=&amp;quot;1.0&amp;quot; ?&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;!DOCTYPE wml SYSTEM &amp;quot;b.dtd&amp;quot; &amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;wml&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;card&amp;gt;&lt;br&gt;&lt;br /&gt;Hi &amp;amp;sonal;&lt;br&gt;&lt;br /&gt;&amp;lt;/card&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;/wml&amp;gt;&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;b.dtd&lt;br&gt;&lt;br /&gt;&amp;lt;!ELEMENT wml (card)*&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;!ELEMENT card (#PCDATA)*&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;!ENTITY sonal &amp;quot;hi&amp;quot; &amp;gt;&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;Entities have been touched upon earlier. Here, the word 'sonal' will be replaced &lt;br /&gt;with 'hi'. This is called an Entity Reference. The DTD file requires an ENTITY &lt;br /&gt;word with the variable 'sonal', and the value 'hi'.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;&amp;nbsp;&lt;/p&gt;</description><thr:total xmlns:thr="http://purl.org/syndication/thread/1.0">0</thr:total></item><item><title>C Language  Help, C Language Tutorials, C Language Programming, C Language Tricks { The C# Language }</title><link>http://swebsolution.blogspot.com/2007/12/c-language-xml-classes-c-language.html</link><author>noreply@blogger.com (Unknown)</author><pubDate>Wed, 26 Dec 2007 03:50:00 -0800</pubDate><guid isPermaLink="false">tag:blogger.com,1999:blog-7227425366482465228.post-3297536631759087147</guid><description>&lt;p align="justify"&gt;&amp;nbsp;&lt;br&gt;&lt;b&gt;C Language  Help, C Language Tutorials, C Language Programming, C Language Tricks &lt;/b&gt;&lt;br&gt;&lt;br /&gt;XML Classes&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;eXtensible Markup Language i.e. XML is a subset of the Standard Generalized &lt;br /&gt;Markup Language  &lt;br /&gt;(SGML), which is an ISO standard numbered ISO 8879. SGML was perceived to be &lt;br /&gt;remarkably colossal  &lt;br /&gt;and extremely convoluted to be put to any pragmatic use. Thus, a subset of this &lt;br /&gt;language, XML,  &lt;br /&gt;was developed to work seamlessly with both SGML and HTML. XML may be considered &lt;br /&gt;as a restricted  &lt;br /&gt;form of SGML, since it conforms to the rules of an SGML document. &lt;br /&gt; &lt;br /&gt;XML was created in the year 1996 under the auspices of the World Wide Web &lt;br /&gt;Consortium (W3C),  &lt;br /&gt;under the chairmanship of Jon Bosak. This group spelt out 10 ground rules for &lt;br /&gt;XML, with 'ease of  &lt;br /&gt;use' as its fundamental philosophy. From thereon, the expectations reached a &lt;br /&gt;threshold wherein,  &lt;br /&gt;XML was expected to eradicate world poverty and generally rid the world of all &lt;br /&gt;its tribulations.  &lt;br /&gt;To be precise, XML was overvalued, way beyond realistic levels. There are people &lt;br /&gt;who appear to  &lt;br /&gt;be extremely infatuated by XML, even though they may not have read through a &lt;br /&gt;single rule or  &lt;br /&gt;specification of the language.  &lt;br /&gt;&lt;br&gt;&lt;br /&gt;The specifications of XML laid down by its three primary authors- Tim Bray, Jean &lt;br /&gt;Paoli and C. M.  &lt;br /&gt;Sperberg-McQueen, are accessible at the web site http://www.w3.org/XML.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;XML documents consists entities comprising of Characters or Markups. An XML file &lt;br /&gt;is made up of a  &lt;br /&gt;myriad components, which shall be unravelled one at a time, after we have &lt;br /&gt;discerned the basic concepts of this language. We commence this chapter by introducing a program &lt;br /&gt;that generates an &lt;br&gt;&lt;br /&gt;XML file. &lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;a.cs&lt;br&gt;&lt;br /&gt;using System;&lt;br&gt;&lt;br /&gt;using System.Xml;&lt;br&gt;&lt;br /&gt;public class zzz&lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;public static void Main() {&lt;br&gt;&lt;br /&gt;XmlTextWriter a = new XmlTextWriter (&amp;quot;b.xml&amp;quot;, null);&lt;br&gt;&lt;br /&gt;a.Flush();&lt;br&gt;&lt;br /&gt;a.Close();&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;In this program, we use a class called XmlTextWriter, which comes from the &lt;br /&gt;System.Xml namespace.&lt;br /&gt;An instance 'a' of the XmlTextWriter class is created, by passing two parameters &lt;br /&gt;to the &lt;br&gt;&lt;br /&gt;constructor:&lt;br&gt;&lt;br /&gt;• The first parameter, b.xml, is a string and represents the name of the file to &lt;br /&gt;be&lt;br /&gt;created. If the file exists in the current directory, it gets deleted and then &lt;br /&gt;recreated, but &lt;br /&gt;with zero bytes. &lt;br&gt;&lt;br /&gt;• The second parameter is null. It represents the Encoding type used.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;Unicode is a standard whereby each character is assigned 16 bits. All the &lt;br /&gt;languages in the &lt;br /&gt;world can now be easily represented by this standard. In the .Net world, we are &lt;br /&gt;furnished with &lt;br /&gt;classes whose methods facilitate conversion of arrays and strings made up of &lt;br /&gt;Unicode characters, &lt;br /&gt;to and from arrays made up of bytes alone. &lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;The System.Text namespace has a large number of Encoding implementations, such &lt;br /&gt;as the following: &lt;br&gt;&lt;br /&gt;• The ASCII Encoding encodes the Unicode characters as 7-bit ASCII.&lt;br&gt;&lt;br /&gt;• The UTF8 Encoding class encodes Unicode characters using UTF-8 encoding. &lt;br&gt;&lt;br /&gt;UTF-8 stands for UCS Transformation Format 8 bit. It supports all Unicode &lt;br /&gt;characters. It is &lt;br&gt;&lt;br /&gt;normally accessed as code page 65001. UTF-8 is the default value and represents &lt;br /&gt;all the letters &lt;br /&gt;from the English alphabet. Here, since we have specified the second parameter as &lt;br /&gt;null, the &lt;br /&gt;default value of UTF-8 encoding is taken.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;If we execute the program at this stage, you would be amazed by the fact that no &lt;br /&gt;file by the &lt;br /&gt;name of b.xml will be displayed. To enable this to happen, a function named &lt;br /&gt;Flush needs to be&lt;br /&gt;called. &lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;Each time we ask the class XmlTextWriter to write to a file, it may not oblige &lt;br /&gt;immediately, but&lt;br /&gt;may place the output in a buffer. Only when the buffer becomes full, will it &lt;br /&gt;write to the file. &lt;br /&gt;This approach is pursued to avoid the overhead of accessing the file on the disk &lt;br /&gt;repetitively. &lt;br /&gt;This improves efficiency. The Flush function flushes the buffer to the file &lt;br /&gt;stream, but it does &lt;br /&gt;not close the file. &lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;The Close function has to be employed to execute the twin tasks of flushing the &lt;br /&gt;buffer to the &lt;br /&gt;file, and closing the file. It is sagacious to call Flush, and then call Close, &lt;br /&gt;even though &lt;br /&gt;Close is adequate to carry out both these tasks.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;a.cs&lt;br&gt;&lt;br /&gt;using System;&lt;br&gt;&lt;br /&gt;using System.Xml;&lt;br&gt;&lt;br /&gt;public class zzz&lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;public static void Main() {&lt;br&gt;&lt;br /&gt;XmlTextWriter a = new XmlTextWriter (&amp;quot;b.xml&amp;quot;, null);&lt;br&gt;&lt;br /&gt;a.WriteStartDocument();&lt;br&gt;&lt;br /&gt;a.Flush();&lt;br&gt;&lt;br /&gt;a.Close();&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;b.xml&lt;br&gt;&lt;br /&gt;&amp;lt;?xml version=&amp;quot;1.0&amp;quot;?&amp;gt;&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;Here, we have called a function called WriteStartDocument from the XmlTextWriter &lt;br /&gt;class, which&lt;br /&gt;does not take any parameters. It produces the line &amp;lt;?xml version=&amp;quot;1.0&amp;quot;?&amp;gt;, in the &lt;br /&gt;file b.xml.&lt;br /&gt;Any line that begins with &amp;lt;?xml is called an XML declaration. Every entity in &lt;br /&gt;XML is described &lt;br /&gt;as a node. Every XML file must begin with an XML Declaration node. There can be &lt;br /&gt;only one such &lt;br /&gt;node in our XML file and it must be placed on the first line. Following it is an &lt;br /&gt;attribute &lt;br /&gt;called version, which is initialized to a value of 1.0. &lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;The XML specifications lucidly stipulate that there would be no attribute called &lt;br /&gt;version in the &lt;br /&gt;next version of the software. Even if there is, its value would be &lt;br /&gt;indeterminate. In other &lt;br /&gt;words, in the foreseeable future, the only mandatory attribute would be &lt;br /&gt;version=1.0.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;a.cs&lt;br&gt;&lt;br /&gt;using System;&lt;br&gt;&lt;br /&gt;using System.Xml;&lt;br&gt;&lt;br /&gt;public class zzz&lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;public static void Main()&lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;XmlTextWriter a = new XmlTextWriter (&amp;quot;b.xml&amp;quot;, null);&lt;br&gt;&lt;br /&gt;a.WriteStartDocument();&lt;br&gt;&lt;br /&gt;a.WriteDocType(&amp;quot;vijay&amp;quot;, null, null ,null);&lt;br&gt;&lt;br /&gt;a.Flush();&lt;br&gt;&lt;br /&gt;a.Close();&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;b.xml&lt;br&gt;&lt;br /&gt;&amp;lt;?xml version=&amp;quot;1.0&amp;quot;?&amp;gt;&amp;lt;!DOCTYPE vijay&amp;gt;&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;The next vital declaration is the DOCTYPE declaration. Every XML file must have &lt;br /&gt;one DOCTYPE &lt;br&gt;&lt;br /&gt;declaration, as it specifies the root tag. In our case, the root tag would be 'vijay'.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;An XML file is made up of tags, which are words enclosed within angular &lt;br /&gt;brackets. The file also &lt;br /&gt;contains rules, which bind the tags. The next three parameters of the function &lt;br /&gt;WriteDocType are&lt;br /&gt;presently specified as null. You may refer to the documentation to decipher the &lt;br /&gt;remaining &lt;br&gt;&lt;br /&gt;values, since these may be used in place of null. If this does not appeal to &lt;br /&gt;you, you may have &lt;br /&gt;to hold your horses, till we furnish the explanation at an appropriate time.&lt;br&gt;&lt;br /&gt;a.cs&lt;br&gt;&lt;br /&gt;using System;&lt;br&gt;&lt;br /&gt;using System.Xml;&lt;br&gt;&lt;br /&gt;public class zzz&lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;public static void Main() {&lt;br&gt;&lt;br /&gt;XmlTextWriter a = new XmlTextWriter (&amp;quot;b.xml&amp;quot;, null);&lt;br&gt;&lt;br /&gt;a.WriteStartDocument();&lt;br&gt;&lt;br /&gt;a.Formatting = Formatting.Indented;&lt;br&gt;&lt;br /&gt;a.Indentation = 3;&lt;br&gt;&lt;br /&gt;a.WriteDocType(&amp;quot;vijay&amp;quot;, null, null ,null);&lt;br&gt;&lt;br /&gt;a.Flush();&lt;br&gt;&lt;br /&gt;a.Close();&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;b.xml&lt;br&gt;&lt;br /&gt;&amp;lt;?xml version=&amp;quot;1.0&amp;quot;?&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;!DOCTYPE vijay&amp;gt;&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;In the earlier example, all the nodes were displayed on the same line. We would &lt;br /&gt;indubitably &lt;br /&gt;desire that every node be displayed on a new line. The property Formatting in &lt;br /&gt;XmlTextWriter, is&lt;br /&gt;used to accomplish this task. Formatting can be assigned only one of the &lt;br /&gt;following two values: &lt;br /&gt;Indented or None. By default, the value assigned is None. &lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;The Indented option indents the child elements by 2 spaces. The magnitude of the &lt;br /&gt;indent may be &lt;br /&gt;altered, by stipulating a new value for the Indentation field. In our program, &lt;br /&gt;we want the &lt;br /&gt;indent to be 3 spaces deep. Hence, we stipulate the value as 3. As is evident, &lt;br /&gt;all nodes do not &lt;br /&gt;get indented. For example, the DOCTYPE node does not get indented; instead, it &lt;br /&gt;is placed on a&lt;br /&gt;new line. &lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;The IndentChar property may be supplied with the character that is to be &lt;br /&gt;employed for&lt;br /&gt;indentation. By default, a space character is used for this purpose. &lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;a.cs&lt;br&gt;&lt;br /&gt;using System;&lt;br&gt;&lt;br /&gt;using System.Xml;&lt;br&gt;&lt;br /&gt;public class zzz&lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;public static void Main()&lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;XmlTextWriter a = new XmlTextWriter (&amp;quot;b.xml&amp;quot;, null);&lt;br&gt;&lt;br /&gt;a.WriteStartDocument();&lt;br&gt;&lt;br /&gt;a.Formatting = Formatting.Indented;&lt;br&gt;&lt;br /&gt;a.Indentation = 3;&lt;br&gt;&lt;br /&gt;a.WriteDocType(&amp;quot;vijay&amp;quot;, null, null ,null);&lt;br&gt;&lt;br /&gt;a.WriteStartElement(&amp;quot;vijay&amp;quot;);&lt;br&gt;&lt;br /&gt;a.Flush();&lt;br&gt;&lt;br /&gt;a.Close();&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;b.xml&lt;br&gt;&lt;br /&gt;&amp;lt;?xml version=&amp;quot;1.0&amp;quot;?&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;!DOCTYPE vijay&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;vijay /&amp;gt;&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;The function WriteStartElement accepts a single parameter, which is the tag &lt;br /&gt;name, to be written&lt;br /&gt;to the XML file. This is an oft-repeated instruction, to be iterated in almost &lt;br /&gt;every program, &lt;br /&gt;since an XML file basically comprises of tags. A tag normally has a start point &lt;br /&gt;and an end &lt;br /&gt;point, and it confines entities within these two extremities. However, there are &lt;br /&gt;tags that do &lt;br /&gt;not accept any entities. Such tags end with a / symbol.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;a.cs&lt;br&gt;&lt;br /&gt;using System;&lt;br&gt;&lt;br /&gt;using System.Xml;&lt;br&gt;&lt;br /&gt;public class zzz&lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;public static void Main()&lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;XmlTextWriter a = new XmlTextWriter (&amp;quot;b.xml&amp;quot;, null);&lt;br&gt;&lt;br /&gt;a.WriteStartDocument();&lt;br&gt;&lt;br /&gt;a.Formatting = Formatting.Indented;&lt;br&gt;&lt;br /&gt;a.Indentation = 3;&lt;br&gt;&lt;br /&gt;a.WriteDocType(&amp;quot;vijay&amp;quot;, null, null ,null);&lt;br&gt;&lt;br /&gt;a.WriteStartElement(&amp;quot;vijay&amp;quot;);&lt;br&gt;&lt;br /&gt;a.WriteAttributeString (&amp;quot;wife&amp;quot;,&amp;quot;sonal&amp;quot;);&lt;br&gt;&lt;br /&gt;a.Flush();&lt;br&gt;&lt;br /&gt;a.Close();&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;b.xml&lt;br&gt;&lt;br /&gt;&amp;lt;?xml version=&amp;quot;1.0&amp;quot;?&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;!DOCTYPE vijay&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;vijay wife=&amp;quot;sonal&amp;quot; /&amp;gt;&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;The newly added function WriteAttributeString accepts two parameters, which it &lt;br /&gt;writes in the&lt;br /&gt;form of a name-value pair. Thus, along with 'vijay', we see the attribute named &lt;br /&gt;'wife', having a&lt;br /&gt;value of 'sonal'. An attribute is analogous to an adjective of the English &lt;br /&gt;language, in that, it&lt;br /&gt;describes the object. In our case, it describes the tag 'vijay'. It divulges &lt;br /&gt;additional&lt;br /&gt;information about the properties of a tag. &lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;XML does not interpret the contents of these tags. The word 'wife' or the value &lt;br /&gt;'sonal', have no&lt;br /&gt;special significance for XML, which is absolutely unconcerned about the &lt;br /&gt;information provided &lt;br /&gt;within the tags.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;a.cs&lt;br&gt;&lt;br /&gt;using System;&lt;br&gt;&lt;br /&gt;using System.Xml;&lt;br&gt;&lt;br /&gt;public class zzz&lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;public static void Main() {&lt;br&gt;&lt;br /&gt;XmlTextWriter a = new XmlTextWriter (&amp;quot;b.xml&amp;quot;, null);&lt;br&gt;&lt;br /&gt;a.WriteStartDocument();&lt;br&gt;&lt;br /&gt;a.Formatting = Formatting.Indented;&lt;br&gt;&lt;br /&gt;a.Indentation = 3;&lt;br&gt;&lt;br /&gt;a.WriteDocType(&amp;quot;vijay&amp;quot;, null, null ,null);&lt;br&gt;&lt;br /&gt;a.WriteStartElement(&amp;quot;vijay&amp;quot;);&lt;br&gt;&lt;br /&gt;a.WriteAttributeString (&amp;quot;wife&amp;quot;,&amp;quot;sonal&amp;quot;);&lt;br&gt;&lt;br /&gt;a.WriteElementString(&amp;quot;surname&amp;quot;, &amp;quot;mukhi&amp;quot;);&lt;br&gt;&lt;br /&gt;a.Flush();&lt;br&gt;&lt;br /&gt;a.Close();&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;b.xml&lt;br&gt;&lt;br /&gt;&amp;lt;?xml version=&amp;quot;1.0&amp;quot;?&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;!DOCTYPE vijay&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;vijay wife=&amp;quot;sonal&amp;quot;&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;surname&amp;gt;mukhi&amp;lt;/surname&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;/vijay&amp;gt;&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;An element represents entities within a tag. We have a tag surname containing &lt;br /&gt;the value 'mukhi'.&lt;br /&gt;We can have multiple tags within the root tag. &lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;We have been reiterating the fact that we need to adhere to specific rules. You &lt;br /&gt;may steer clear &lt;br /&gt;of the beaten path and interchange the following two newly added functions as &lt;br /&gt;follows:&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;a.WriteElementString(&amp;quot;surname&amp;quot;, &amp;quot;mukhi&amp;quot;);&lt;br&gt;&lt;br /&gt;a.WriteAttributeString (&amp;quot;wife&amp;quot;,&amp;quot;sonal&amp;quot;);&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;As a fallout of this interchange, the following exception will be thrown:&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;Unhandled Exception: System.InvalidOperationException: Token StartAttribute in &lt;br /&gt;state Content&lt;br /&gt;would result in an invalid XML document.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;This exception is triggered off due to the fact that the attribute must be &lt;br /&gt;specified first. &lt;br /&gt;Then, and only then, should the child tags within the tag, be specified. &lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;a.cs&lt;br&gt;&lt;br /&gt;using System;&lt;br&gt;&lt;br /&gt;using System.Xml;&lt;br&gt;&lt;br /&gt;public class zzz&lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;public static void Main()&lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;XmlTextWriter a = new XmlTextWriter (&amp;quot;b.xml&amp;quot;, null);&lt;br&gt;&lt;br /&gt;a.WriteStartDocument();&lt;br&gt;&lt;br /&gt;a.Formatting = Formatting.Indented;&lt;br&gt;&lt;br /&gt;a.Indentation = 3;&lt;br&gt;&lt;br /&gt;a.WriteDocType(&amp;quot;vijay&amp;quot;, null, null ,null);&lt;br&gt;&lt;br /&gt;a.WriteStartElement(&amp;quot;vijay&amp;quot;);&lt;br&gt;&lt;br /&gt;a.WriteAttributeString (&amp;quot;wife&amp;quot;,&amp;quot;sonal&amp;quot;);&lt;br&gt;&lt;br /&gt;a.WriteAttributeString (&amp;quot;friend&amp;quot;,&amp;quot;two&amp;quot;);&lt;br&gt;&lt;br /&gt;a.WriteElementString(&amp;quot;surname&amp;quot;, &amp;quot;mukhi&amp;quot;);&lt;br&gt;&lt;br /&gt;a.WriteElementString(&amp;quot;books&amp;quot;, &amp;quot;67&amp;quot;);&lt;br&gt;&lt;br /&gt;a.Flush();&lt;br&gt;&lt;br /&gt;a.Close();&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;b.xml&lt;br&gt;&lt;br /&gt;&amp;lt;?xml version=&amp;quot;1.0&amp;quot;?&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;!DOCTYPE vijay&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;vijay wife=&amp;quot;sonal&amp;quot; friend=&amp;quot;two&amp;quot;&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;surname&amp;gt;mukhi&amp;lt;/surname&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;books&amp;gt;67&amp;lt;/books&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;/vijay&amp;gt;&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;To summarize, the WriteDocType function specifies the root tag, the &lt;br /&gt;WriteStartElement the tag,&lt;br /&gt;the WriteAttributeString, the attributes for the active tag and &lt;br /&gt;WriteElementString function, a&lt;br /&gt;tag within a tag. We can enumerate as many attributes as we desire. They will &lt;br /&gt;eventually be &lt;br /&gt;clustered together. The WriteElementString function is also capable of creating &lt;br /&gt;as many tags, as&lt;br /&gt;are needed under a tag. &lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;In the file b.xml, we see two attributes and two tags, under the root tag 'vijay'.&lt;br /&gt;&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;a.cs&lt;br&gt;&lt;br /&gt;using System;&lt;br&gt;&lt;br /&gt;using System.Xml;&lt;br&gt;&lt;br /&gt;public class zzz&lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;public static void Main()&lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;XmlTextWriter a = new XmlTextWriter (&amp;quot;b.xml&amp;quot;, null);&lt;br&gt;&lt;br /&gt;a.WriteStartDocument();&lt;br&gt;&lt;br /&gt;a.Formatting = Formatting.Indented;&lt;br&gt;&lt;br /&gt;a.Indentation = 3;&lt;br&gt;&lt;br /&gt;a.WriteDocType(&amp;quot;vijay&amp;quot;, null, null ,null);&lt;br&gt;&lt;br /&gt;a.WriteStartElement(&amp;quot;vijay&amp;quot;);&lt;br&gt;&lt;br /&gt;a.WriteAttributeString (&amp;quot;friend&amp;quot;,&amp;quot;two&amp;quot;);&lt;br&gt;&lt;br /&gt;a.WriteStartElement(&amp;quot;mukhi&amp;quot;);&lt;br&gt;&lt;br /&gt;a.WriteAttributeString (&amp;quot;wife&amp;quot;,&amp;quot;sonal&amp;quot;);&lt;br&gt;&lt;br /&gt;a.Flush();&lt;br&gt;&lt;br /&gt;a.Close();&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;b.xml&lt;br&gt;&lt;br /&gt;&amp;lt;?xml version=&amp;quot;1.0&amp;quot;?&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;!DOCTYPE vijay&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;vijay friend=&amp;quot;two&amp;quot;&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;mukhi wife=&amp;quot;sonal&amp;quot; /&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;/vijay&amp;gt;&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;In the above example, 'vijay' is the root tag, with the attribute 'friend', &lt;br /&gt;which is assigned a&lt;br /&gt;value of 2. It also has a child tag 'mukhi' having the attribute of 'wife' &lt;br /&gt;initialized to&lt;br /&gt;'sonal'. Both the tags, 'vijay' and 'mukhi', are created using the function &lt;br /&gt;WriteStartElement.&lt;br /&gt;Unlike function WriteElementString, which creates a start and end tag, &lt;br /&gt;WriteStartElement creates&lt;br /&gt;only a start tag. &lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;A tag too can be endowed with attributes. The active tag is the last inserted by &lt;br /&gt;the &lt;br /&gt;WriteStartElement function. Functions such as WriteAttributeString, act on the &lt;br /&gt;active tag. Thus,&lt;br /&gt;we notice that the attribute of 'wife' has the tag 'mukhi' and not 'vijay'. &lt;br /&gt;Finally, since the&lt;br /&gt;tag 'mukhi' is devoid of any contents, it ends with a / symbol on the same line.&lt;br /&gt;&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;a.cs&lt;br&gt;&lt;br /&gt;using System;&lt;br&gt;&lt;br /&gt;using System.Xml;&lt;br&gt;&lt;br /&gt;public class zzz&lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;public static void Main()&lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;XmlTextWriter a = new XmlTextWriter (&amp;quot;b.xml&amp;quot;, null);&lt;br&gt;&lt;br /&gt;a.WriteStartDocument();&lt;br&gt;&lt;br /&gt;a.Formatting = Formatting.Indented;&lt;br&gt;&lt;br /&gt;a.Indentation = 3;&lt;br&gt;&lt;br /&gt;a.WriteDocType(&amp;quot;vijay&amp;quot;, null, null ,null);&lt;br&gt;&lt;br /&gt;a.WriteStartElement(&amp;quot;vijay&amp;quot;);&lt;br&gt;&lt;br /&gt;a.WriteAttributeString (&amp;quot;friend&amp;quot;,&amp;quot;two&amp;quot;);&lt;br&gt;&lt;br /&gt;a.WriteStartElement(&amp;quot;mukhi&amp;quot;);&lt;br&gt;&lt;br /&gt;a.WriteAttributeString (&amp;quot;wife&amp;quot;,&amp;quot;sonal&amp;quot;);&lt;br&gt;&lt;br /&gt;a.WriteFullEndElement();&lt;br&gt;&lt;br /&gt;a.Flush();&lt;br&gt;&lt;br /&gt;a.Close();&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;b.xml&lt;br&gt;&lt;br /&gt;&amp;lt;?xml version=&amp;quot;1.0&amp;quot;?&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;!DOCTYPE vijay&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;vijay friend=&amp;quot;two&amp;quot;&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;mukhi wife=&amp;quot;sonal&amp;quot;&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;/mukhi&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;/vijay&amp;gt;&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;The function WriteFullEndElement marks the end of the active tag. Therefore, the &lt;br /&gt;single tag&lt;br /&gt;'mukhi', does not end with a / symbol on the same line. It has an ending tag &lt;br /&gt;instead. Both these&lt;br /&gt;possibilities are equally valid in this case. But, if the tags embody any &lt;br /&gt;contents, then both  &lt;br /&gt;the start and the end tags are mandatory. In such situations, a single empty tag &lt;br /&gt;would just not suffice.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;a.cs&lt;br&gt;&lt;br /&gt;using System;&lt;br&gt;&lt;br /&gt;using System.Xml;&lt;br&gt;&lt;br /&gt;public class zzz&lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;public static void Main()&lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;XmlTextWriter a = new XmlTextWriter (&amp;quot;b.xml&amp;quot;, null);&lt;br&gt;&lt;br /&gt;//a.WriteComment(&amp;quot;comment 1&amp;quot;);&lt;br&gt;&lt;br /&gt;a.Formatting = Formatting.Indented;&lt;br&gt;&lt;br /&gt;a.Indentation = 3;&lt;br&gt;&lt;br /&gt;a.WriteStartDocument();&lt;br&gt;&lt;br /&gt;a.WriteComment(&amp;quot;comment 1&amp;quot;);&lt;br&gt;&lt;br /&gt;a.WriteDocType(&amp;quot;vijay&amp;quot;, null, null ,null);&lt;br&gt;&lt;br /&gt;a.WriteComment(&amp;quot;comment 2&amp;quot;);&lt;br&gt;&lt;br /&gt;a.WriteStartElement(&amp;quot;vijay&amp;quot;);&lt;br&gt;&lt;br /&gt;a.WriteAttributeString (&amp;quot;wife&amp;quot;,&amp;quot;sonal&amp;quot;);&lt;br&gt;&lt;br /&gt;a.WriteComment(&amp;quot;comment 3&amp;quot;);&lt;br&gt;&lt;br /&gt;a.WriteElementString(&amp;quot;surname&amp;quot;, &amp;quot;mukhi&amp;quot;);&lt;br&gt;&lt;br /&gt;a.Flush();&lt;br&gt;&lt;br /&gt;a.Close();&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;b.xml&lt;br&gt;&lt;br /&gt;&amp;lt;?xml version=&amp;quot;1.0&amp;quot;?&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;!--comment 1--&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;!DOCTYPE vijay&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;&amp;lt;!--comment 2--&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;vijay wife=&amp;quot;sonal&amp;quot;&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;!--comment 3--&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;surname&amp;gt;mukhi&amp;lt;/surname&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;/vijay&amp;gt;&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;Every programming language extends the facility of writing comments, even though &lt;br /&gt;it may be a &lt;br /&gt;seldom used feature. Programmers insert comments amidst their code to document &lt;br /&gt;or explain the &lt;br /&gt;functioning of their programs. At times, comments assist in deciphering the code &lt;br /&gt;from the &lt;br /&gt;programmer's perspective. Practically, it may be easier to teach an elephant how &lt;br /&gt;to tap-dance, &lt;br /&gt;than to convince a programmer to write comments.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;In the XML world, comments begin with &amp;lt;!-, and end with --&amp;gt;. This is somewhat &lt;br /&gt;similar to the &lt;br /&gt;HTML syntax. In fact, the rules of HTML are written in XML. &lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;Comments are like a liquid, since they can be moulded to fit-in anywhere, except &lt;br /&gt;on the first&lt;br /&gt;line of a program. The first line in an XML file has to be a declaration. If you &lt;br /&gt;dispense with &lt;br /&gt;the comments given with the function WriteComment, an exception will be thrown &lt;br /&gt;with the&lt;br /&gt;following message:&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;Unhandled Exception: System.InvalidOperationException: WriteStartDocument should &lt;br /&gt;be the first&lt;br /&gt;call.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;Thus, functions such as WriteComment, can be used to insert comments anywhere in &lt;br /&gt;the code,&lt;br /&gt;primarily for the purpose of documentation, which would enable even an alien &lt;br /&gt;from outer space to &lt;br /&gt;decipher the code better.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;a.cs&lt;br&gt;&lt;br /&gt;using System;&lt;br&gt;&lt;br /&gt;using System.Xml;&lt;br&gt;&lt;br /&gt;public class zzz&lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;public static void Main()&lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;XmlTextWriter a = new XmlTextWriter (&amp;quot;b.xml&amp;quot;, null);&lt;br&gt;&lt;br /&gt;a.WriteStartDocument();&lt;br&gt;&lt;br /&gt;a.Formatting = Formatting.Indented;&lt;br&gt;&lt;br /&gt;a.Indentation = 3;&lt;br&gt;&lt;br /&gt;a.WriteDocType(&amp;quot;vijay&amp;quot;, null, null ,null);&lt;br&gt;&lt;br /&gt;a.WriteStartElement(&amp;quot;vijay&amp;quot;);&lt;br&gt;&lt;br /&gt;a.WriteProcessingInstruction (&amp;quot;sonal&amp;quot;, &amp;quot;mukhi=no&amp;quot;);&lt;br&gt;&lt;br /&gt;a.Flush();&lt;br&gt;&lt;br /&gt;a.Close();&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;b.xml&lt;br&gt;&lt;br /&gt;&amp;lt;?xml version=&amp;quot;1.0&amp;quot;?&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;!DOCTYPE vijay&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;vijay&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;?sonal mukhi=no?&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;/vijay&amp;gt;&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;A line beginning with &amp;lt;?, Is called a Processing Instruction (PI). This line is &lt;br /&gt;inserted using &lt;br /&gt;the function WriteProcessingInstruction, and is passed two parameters: &lt;br&gt;&lt;br /&gt;• the first is the name of the processing instruction.&lt;/p&gt;&lt;br /&gt;&lt;p align="justify"&gt;• the second is the text that is to be inserted for the processing instruction.&lt;br /&gt;&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;A Procession Instruction is used by XML to communicate with other programs &lt;br /&gt;during the &lt;br /&gt;performance of certain tasks. XML does not have the wherewithal to execute &lt;br /&gt;instructions. It &lt;br /&gt;therefore delegates this task to the XML processor. The processor is a program &lt;br /&gt;that is able to &lt;br /&gt;recognise an XML file. When it encounters the processing instruction, and if it &lt;br /&gt;is able to&lt;br /&gt;understand it, it executes it. In cases where it cannot comprehend it, the &lt;br /&gt;processor simply &lt;br /&gt;ignores the instruction. This is the methodology by which XML communicates with &lt;br /&gt;external &lt;br /&gt;programs.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;In our program, the instruction 'sonal' is ignored, as it does not provide any &lt;br /&gt;meaningful input &lt;br&gt;&lt;br /&gt;to the processor.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;a.cs&lt;br&gt;&lt;br /&gt;using System;&lt;br&gt;&lt;br /&gt;using System.Xml;&lt;br&gt;&lt;br /&gt;public class zzz&lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;public static void Main()&lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;XmlTextWriter a = new XmlTextWriter (&amp;quot;b.xml&amp;quot;, null);&lt;br&gt;&lt;br /&gt;a.WriteStartDocument();&lt;br&gt;&lt;br /&gt;a.Formatting = Formatting.Indented;&lt;br&gt;&lt;br /&gt;a.WriteDocType(&amp;quot;vijay&amp;quot;, null, null ,null);&lt;br&gt;&lt;br /&gt;a.WriteStartElement(&amp;quot;vijay&amp;quot;);&lt;br&gt;&lt;br /&gt;a.WriteString(&amp;quot;mukhi&amp;quot;);&lt;br&gt;&lt;br /&gt;a.Flush();&lt;br&gt;&lt;br /&gt;a.Close();&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;b.xml&lt;br&gt;&lt;br /&gt;&amp;lt;?xml version=&amp;quot;1.0&amp;quot;?&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;!DOCTYPE vijay&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;vijay&amp;gt;mukhi&amp;lt;/vijay&amp;gt;&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;An XML file mainly consists of strings and tags. The WriteString function is &lt;br /&gt;very extensively&lt;br /&gt;exploited, since it writes content/strings between tags. &lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;In the above example, the text 'mukhi' is enclosed within the tags of 'vijay'. &lt;br /&gt;Even though we&lt;br /&gt;have not explicitly asked the XmlTextWriter class to close the tag, the ending &lt;br /&gt;tag has been used&lt;br /&gt;because there exists some content after the opening tag. &lt;br&gt;&lt;br /&gt;a.cs&lt;br&gt;&lt;br /&gt;using System;&lt;br&gt;&lt;br /&gt;using System.Xml;&lt;br&gt;&lt;br /&gt;public class zzz&lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;public static void Main()&lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;XmlTextWriter a = new XmlTextWriter (&amp;quot;b.xml&amp;quot;, null);&lt;br&gt;&lt;br /&gt;a.WriteStartDocument();&lt;br&gt;&lt;br /&gt;a.Formatting = Formatting.Indented;&lt;br&gt;&lt;br /&gt;a.Indentation = 3;&lt;br&gt;&lt;br /&gt;a.WriteDocType(&amp;quot;vijay&amp;quot;, null, null ,null);&lt;br&gt;&lt;br /&gt;a.WriteStartElement(&amp;quot;vijay&amp;quot;);&lt;br&gt;&lt;br /&gt;a.WriteAttributeString (&amp;quot;friend&amp;quot;,&amp;quot;two&amp;quot;);&lt;br&gt;&lt;br /&gt;a.WriteString(&amp;quot;hi&amp;quot;);&lt;br&gt;&lt;br /&gt;//a.WriteAttributeString (&amp;quot;friend&amp;quot;,&amp;quot;three&amp;quot;);&lt;br&gt;&lt;br /&gt;a.WriteStartElement(&amp;quot;mukhi&amp;quot;);&lt;br&gt;&lt;br /&gt;a.WriteAttributeString (&amp;quot;friend&amp;quot;,&amp;quot;two&amp;quot;);&lt;br&gt;&lt;br /&gt;a.WriteString(&amp;quot;bye&amp;quot;);&lt;br&gt;&lt;br /&gt;a.Flush();&lt;br&gt;&lt;br /&gt;a.Close();&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;b.xml&lt;br&gt;&lt;br /&gt;&amp;lt;?xml version=&amp;quot;1.0&amp;quot;?&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;!DOCTYPE vijay&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;vijay friend=&amp;quot;two&amp;quot;&amp;gt;hi&amp;lt;mukhi friend=&amp;quot;two&amp;quot;&amp;gt;bye&amp;lt;/mukhi&amp;gt;&amp;lt;/vijay&amp;gt;&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;The function WriteString can be inserted almost anywhere in the program. The &lt;br /&gt;first WriteString &lt;br&gt;&lt;br /&gt;function writes 'hi' between the tags of 'vijay', while the second WriteString &lt;br /&gt;function writes &lt;br&gt;&lt;br /&gt;'bye' between the tags of 'mukhi'. The WriteString is aware of the active tag. &lt;br /&gt;Therefore, it &lt;br&gt;&lt;br /&gt;inserts the text accordingly. Here also, if we uncomment the line, &lt;br&gt;&lt;br /&gt;a.WriteAttributeString(&amp;quot;friend&amp;quot;,&amp;quot;three&amp;quot;), the following exception will be &lt;br /&gt;generated.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;Unhandled Exception: System.InvalidOperationException: Token StartAttribute in &lt;br /&gt;state Content &lt;br&gt;&lt;br /&gt;would result in an invalid XML document.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;XML is very strict and meticulous in the sense that, it expects a certain order &lt;br /&gt;to be &lt;br /&gt;maintained, or else, it throws an exception. For instance, an element or a tag &lt;br /&gt;has to be created &lt;br /&gt;first. Only then, can all the attributes be written; and finally, the text or &lt;br /&gt;content has to be &lt;br /&gt;supplied. We are not permitted to write the text first and enter the attributes &lt;br /&gt;later. In the &lt;br /&gt;XmlTextWriter class, there is no going back. It is a one-way path, which only &lt;br /&gt;moves in the&lt;br /&gt;forward direction.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;a.cs&lt;br&gt;&lt;br /&gt;using System;&lt;br&gt;&lt;br /&gt;using System.Xml;&lt;br&gt;&lt;br /&gt;public class zzz&lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;public static void Main()&lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;XmlTextWriter a = new XmlTextWriter (&amp;quot;b.xml&amp;quot;, null);&lt;br&gt;&lt;br /&gt;a.WriteStartDocument();&lt;br&gt;&lt;br /&gt;a.Formatting = Formatting.Indented;&lt;br&gt;&lt;br /&gt;a.Indentation = 3;&lt;br&gt;&lt;br /&gt;a.WriteDocType(&amp;quot;vijay&amp;quot;, null, null ,null);&lt;br&gt;&lt;br /&gt;a.WriteStartElement(&amp;quot;vijay&amp;quot;);&lt;br&gt;&lt;br /&gt;a.WriteCharEntity ('A');&lt;br&gt;&lt;br /&gt;a.Flush();&lt;br&gt;&lt;br /&gt;a.Close();&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;b.xml&lt;br&gt;&lt;br /&gt;&amp;lt;?xml version=&amp;quot;1.0&amp;quot;?&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;!DOCTYPE vijay&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;vijay&amp;gt;&amp;amp;#x41;&amp;lt;/vijay&amp;gt;&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;During our exploratory journey of XML, we shall discuss a large number of &lt;br /&gt;characters that are &lt;br /&gt;'reserved'. They have a special significance and cannot be used literally. These &lt;br /&gt;Unicode &lt;br /&gt;characters have to be written in a hex format. The function WriteCharEntity &lt;br /&gt;performs this task.&lt;br /&gt;It accepts a char or a Unicode character as a parameter and returns a number in &lt;br /&gt;hex, prefaced &lt;br /&gt;with the &amp;amp;# symbol. &lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;For those who do not understand hexadecimal and consider it Greek and Latin, 41 &lt;br /&gt;hex is equal to &lt;br /&gt;ASCII 65, which is the ASCII value for the capital letter A. You can pass &lt;br /&gt;different characters &lt;br /&gt;to this function and see their equivalent hex values.&lt;br&gt;&lt;br /&gt;a.cs&lt;br&gt;&lt;br /&gt;using System;&lt;br&gt;&lt;br /&gt;using System.Xml;&lt;br&gt;&lt;br /&gt;public class zzz&lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;public static void Main()&lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;XmlTextWriter a = new XmlTextWriter (&amp;quot;b.xml&amp;quot;, null);&lt;br&gt;&lt;br /&gt;a.WriteStartDocument();&lt;br&gt;&lt;br /&gt;a.Formatting = Formatting.Indented;&lt;br&gt;&lt;br /&gt;a.Indentation = 3;&lt;br&gt;&lt;br /&gt;a.WriteDocType(&amp;quot;vijay&amp;quot;, null, null ,null);&lt;br&gt;&lt;br /&gt;a.WriteStartElement(&amp;quot;vijay&amp;quot;);&lt;br&gt;&lt;br /&gt;a.WriteCData(&amp;quot;mukhi &amp;amp; &amp;lt;sonal&amp;gt;&amp;quot;);&lt;br&gt;&lt;br /&gt;a.Flush();&lt;br&gt;&lt;br /&gt;a.Close();&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;b.xml&lt;br&gt;&lt;br /&gt;&amp;lt;?xml version=&amp;quot;1.0&amp;quot;?&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;!DOCTYPE vijay&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;vijay&amp;gt;&amp;lt;![CDATA[mukhi &amp;amp; &amp;lt;sonal&amp;gt;]]&amp;gt;&amp;lt;/vijay&amp;gt;&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;The above program introduces a new function called WriteCData, which creates a &lt;br /&gt;node called&lt;br /&gt;CDATA. The parameter passed to this function is placed as it is, but is enclosed &lt;br /&gt;within square&lt;br /&gt;brackets. &lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;A CDATA section is used whenever we want to use characters such as &amp;lt;, &amp;gt;, &amp;amp; and &lt;br /&gt;the likes, in&lt;br /&gt;their literal sense, which would otherwise be mistaken for Markup characters. &lt;br /&gt;Thus, in the above &lt;br /&gt;program, the CDATA section that contains the symbol &amp;amp;, interprets it as the &lt;br /&gt;literal character &amp;amp;,&lt;br /&gt;and not as a special character. Also, &amp;lt;sonal&amp;gt; is not recognized as a tag in this &lt;br /&gt;section. A&lt;br /&gt;CDATA section cannot be nested within another CDATA section.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;a.cs&lt;br&gt;&lt;br /&gt;using System;&lt;br&gt;&lt;br /&gt;using System.Xml;&lt;br&gt;&lt;br /&gt;public class zzz&lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;public static void Main()&lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;XmlTextWriter a = new XmlTextWriter (&amp;quot;b.xml&amp;quot;, null);&lt;br&gt;&lt;br /&gt;a.WriteStartDocument();&lt;br&gt;&lt;br /&gt;a.Formatting = Formatting.Indented;&lt;br&gt;&lt;br /&gt;a.Indentation = 3;&lt;br&gt;&lt;br /&gt;a.WriteDocType(&amp;quot;vijay&amp;quot;, null, null ,null);&lt;br&gt;&lt;br /&gt;a.WriteStartElement(&amp;quot;vijay&amp;quot;);&lt;br&gt;&lt;br /&gt;a.WriteString(&amp;quot;&amp;lt;A&amp;gt;&amp;amp;&amp;quot;);&lt;br&gt;&lt;br /&gt;a.WriteCData(&amp;quot;&amp;lt;A&amp;gt;&amp;amp;&amp;quot;);&lt;br&gt;&lt;br /&gt;a.Flush();&lt;br&gt;&lt;br /&gt;a.Close();&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;b.xml&lt;br&gt;&lt;br /&gt;&amp;lt;?xml version=&amp;quot;1.0&amp;quot;?&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;!DOCTYPE vijay&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;vijay&amp;gt;&amp;amp;lt;A&amp;amp;gt;&amp;amp;amp;&lt;br&gt;&lt;br /&gt;&amp;lt;![CDATA[&amp;lt;A&amp;gt;&amp;amp;]]&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;/vijay&amp;gt;&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;This program illustrates certain characters that are special to XML. These are &lt;br /&gt;the obvious &lt;br /&gt;characters, such as &amp;lt;, &amp;gt; and &amp;amp;, since they are used whilst an XML file is being &lt;br /&gt;created. Thus, &lt;br /&gt;whenever XML comes across the following symbols, it replaces them with the &lt;br /&gt;symbols depicted &lt;br /&gt;against each:&lt;br&gt;&lt;br /&gt;• &amp;lt; is replaced with '&amp;amp;lt;' &lt;br&gt;&lt;br /&gt;• &amp;gt; is replaced with '&amp;amp;gt;' &lt;br&gt;&lt;br /&gt;• &amp;amp; is replaced with '&amp;amp;amp;'. &lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;If the same string that contains the above mentioned special characters is &lt;br /&gt;placed within a CDATA &lt;br&gt;&lt;br /&gt;statement, gets written verbatim, without any conversions.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;a.cs&lt;br&gt;&lt;br /&gt;using System;&lt;br&gt;&lt;br /&gt;using System.Xml;&lt;br&gt;&lt;br /&gt;public class zzz&lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;public static void Main()&lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;XmlTextWriter a = new XmlTextWriter (&amp;quot;b.xml&amp;quot;, null);&lt;br&gt;&lt;br /&gt;a.WriteStartDocument();&lt;br&gt;&lt;br /&gt;a.Formatting = Formatting.Indented;&lt;br&gt;&lt;br /&gt;a.Indentation = 3;&lt;br&gt;&lt;br /&gt;a.WriteDocType(&amp;quot;vijay&amp;quot;, null, null ,null);&lt;br&gt;&lt;br /&gt;a.WriteStartElement(&amp;quot;vijay&amp;quot;);&lt;br&gt;&lt;br /&gt;a.WriteEntityRef(&amp;quot;Hi&amp;quot;);&lt;br&gt;&lt;br /&gt;a.Flush();&lt;br&gt;&lt;br /&gt;a.Close();&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;b.xml&lt;br&gt;&lt;br /&gt;&amp;lt;?xml version=&amp;quot;1.0&amp;quot;?&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;!DOCTYPE vijay&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;vijay&amp;gt;&amp;amp;Hi;&amp;lt;/vijay&amp;gt;&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;The entity ref is very straightforward to understand. The string passed to the &lt;br /&gt;function &lt;br /&gt;WriteEntityRef is placed in the XML file, preceded by a '&amp;amp;' sign and followed by &lt;br /&gt;a semi-colon.&lt;br /&gt;An entity ref in XML is equivalent to a variable. It is included to provide &lt;br /&gt;flexibility to the &lt;br /&gt;program.&lt;br&gt;&lt;br /&gt;Thus in the above code, a variable called 'hi' is created. The task of stating &lt;br /&gt;what 'hi' &lt;br /&gt;signifies, can be defined in the XML file. &lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;a.cs&lt;br&gt;&lt;br /&gt;using System;&lt;br&gt;&lt;br /&gt;using System.Xml;&lt;br&gt;&lt;br /&gt;public class zzz&lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;public static void Main()&lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;XmlTextWriter a = new XmlTextWriter (&amp;quot;b.xml&amp;quot;, null);&lt;br&gt;&lt;br /&gt;a.WriteStartDocument();&lt;br&gt;&lt;br /&gt;a.Formatting = Formatting.Indented;&lt;br&gt;&lt;br /&gt;a.Indentation = 3;&lt;br&gt;&lt;br /&gt;a.WriteDocType(&amp;quot;vijay&amp;quot;, null, null ,null);&lt;br&gt;&lt;br /&gt;a.WriteStartElement(&amp;quot;vijay&amp;quot;);&lt;br&gt;&lt;br /&gt;a.WriteRaw(&amp;quot;&amp;lt;A&amp;gt;&amp;amp;&amp;quot;);&lt;br&gt;&lt;br /&gt;a.Flush();&lt;br&gt;&lt;br /&gt;a.Close();&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;b.xml&lt;br&gt;&lt;br /&gt;&amp;lt;?xml version=&amp;quot;1.0&amp;quot;?&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;!DOCTYPE vijay&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;vijay&amp;gt;&amp;lt;A&amp;gt;&amp;amp;&amp;lt;/vijay&amp;gt;&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;The WriteRaw function writes the characters passed to it, without carrying out &lt;br /&gt;any conversions.&lt;br /&gt;The above XML file is obviously erroneous, as no end tag has been specified for &lt;br /&gt;the tag A. Also, &lt;br /&gt;no name has been specified after the &amp;amp; sign.&lt;br&gt;&lt;br /&gt;a.cs&lt;br&gt;&lt;br /&gt;using System;&lt;br&gt;&lt;br /&gt;using System.Xml;&lt;br&gt;&lt;br /&gt;public class zzz&lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;public static void Main()&lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;XmlTextWriter a = new XmlTextWriter (&amp;quot;b.xml&amp;quot;, null);&lt;br&gt;&lt;br /&gt;a.WriteStartDocument();&lt;br&gt;&lt;br /&gt;a.Formatting = Formatting.Indented;&lt;br&gt;&lt;br /&gt;a.Indentation = 3;&lt;br&gt;&lt;br /&gt;a.WriteDocType(&amp;quot;vijay&amp;quot;, null, null ,null);&lt;br&gt;&lt;br /&gt;a.WriteStartElement(&amp;quot;vijay&amp;quot;);&lt;br&gt;&lt;br /&gt;Boolean b = true;&lt;br&gt;&lt;br /&gt;a.WriteElementString(&amp;quot;Logical&amp;quot;, XmlConvert.ToString(b));&lt;br&gt;&lt;br /&gt;Int32 c = -2147483648;&lt;br&gt;&lt;br /&gt;a.WriteElementString(&amp;quot;SmallInt&amp;quot;, XmlConvert.ToString(c));&lt;br&gt;&lt;br /&gt;Int64 d = 9223372036854775807;&lt;br&gt;&lt;br /&gt;a.WriteElementString(&amp;quot;Largelong&amp;quot;, XmlConvert.ToString(d));&lt;br&gt;&lt;br /&gt;Single e = ((Single)22)/((Single)7);&lt;br&gt;&lt;br /&gt;a.WriteElementString(&amp;quot;Single&amp;quot;, XmlConvert.ToString(e));&lt;br&gt;&lt;br /&gt;Double f = 1.79769313486231570E+308;&lt;br&gt;&lt;br /&gt;a.WriteElementString(&amp;quot;Double&amp;quot;, XmlConvert.ToString(f));&lt;br&gt;&lt;br /&gt;DateTime h = new DateTime(2001, 07, 08 ,22, 0, 30, 500);&lt;br&gt;&lt;br /&gt;a.WriteElementString(&amp;quot;DateTime&amp;quot;, XmlConvert.ToString(h));&lt;br&gt;&lt;br /&gt;a.Flush();&lt;br&gt;&lt;br /&gt;a.Close();&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;b.xml&lt;br&gt;&lt;br /&gt;&amp;lt;?xml version=&amp;quot;1.0&amp;quot;?&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;!DOCTYPE vijay&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;vijay&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;Logical&amp;gt;true&amp;lt;/Logical&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;SmallInt&amp;gt;-2147483648&amp;lt;/SmallInt&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;Largelong&amp;gt;9223372036854775807&amp;lt;/Largelong&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;Single&amp;gt;3.142857&amp;lt;/Single&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;Double&amp;gt;1.7976931348623157E+308&amp;lt;/Double&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;DateTime&amp;gt;2001-07-08T22:00:30.5000000+05:30&amp;lt;/DateTime&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;/vijay&amp;gt;&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;The above example contains a plethora of data types such as, boolean, int, &lt;br /&gt;double and Data Time.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;The XmlConvert class has a large number of static functions that help us convert &lt;br /&gt;one data type &lt;br&gt;&lt;br /&gt;to another. One such function is the ToString function. For types such as int or &lt;br /&gt;long, the &lt;br&gt;&lt;br /&gt;smallest and the largest values are used, in order to check the veracity of the &lt;br /&gt;ToString &lt;br&gt;&lt;br /&gt;function. &lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;The ToString function is overloaded to handle many more data types than we have &lt;br /&gt;shown. The point&lt;br /&gt;here is that, it is possible for us to convert any data type into a string and &lt;br /&gt;write it to disk. &lt;br /&gt;This factor gains immense importance when data is being received from a &lt;br /&gt;database, and requires &lt;br /&gt;to be converted into a string in an XML file.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;a.cs&lt;br&gt;&lt;br /&gt;using System;&lt;br&gt;&lt;br /&gt;using System.Xml;&lt;br&gt;&lt;br /&gt;public class zzz&lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;public static void Main()&lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;XmlTextWriter a = new XmlTextWriter (&amp;quot;b.xml&amp;quot;, null);&lt;br&gt;&lt;br /&gt;a.WriteStartDocument();&lt;br&gt;&lt;br /&gt;a.Formatting = Formatting.Indented;&lt;br&gt;&lt;br /&gt;a.Indentation = 3;&lt;br&gt;&lt;br /&gt;a.WriteDocType(&amp;quot;vijay&amp;quot;, null, null ,null);&lt;br&gt;&lt;br /&gt;a.WriteStartElement(&amp;quot;vijay&amp;quot;);&lt;br&gt;&lt;br /&gt;a.WriteStartAttribute(&amp;quot;hi&amp;quot;, &amp;quot;mukhi&amp;quot;, &amp;quot;xxx:yyy&amp;quot;);&lt;br&gt;&lt;br /&gt;a.WriteString(&amp;quot;1-861003-78&amp;quot;);&lt;br&gt;&lt;br /&gt;a.WriteEndAttribute(); &lt;br&gt;&lt;br /&gt;a.Flush();&lt;br&gt;&lt;br /&gt;a.Close();&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;b.xml&lt;br&gt;&lt;br /&gt;&amp;lt;?xml version=&amp;quot;1.0&amp;quot;?&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;!DOCTYPE vijay&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;vijay hi:mukhi=&amp;quot;1-861003-78&amp;quot; xmlns:hi=&amp;quot;xxx:yyy&amp;quot; /&amp;gt;&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;In the above example, we have introduced the WriteStartAttribute function. As is &lt;br /&gt;apparent from&lt;br /&gt;its name, it starts an attribute. The first parameter to this function is 'hi', &lt;br /&gt;which is the &lt;br /&gt;namespace, to which the prefix of the attribute belongs. The second parameter &lt;br /&gt;'mukhi' is the&lt;br /&gt;name of the attribute. &lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;The names assigned to attributes and tags may not always result in a unique &lt;br /&gt;name. A programmer &lt;br /&gt;may inadvertently create a tag or an attribute with a name that already exists. &lt;br /&gt;How then does &lt;br /&gt;XML decide what the tag denotes?&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;To help resolve such potential conflicts, each tag or entity is prefaced with a &lt;br /&gt;name known as &lt;br /&gt;the namespace. This is followed by a colon sign. Normally, meaningful names are &lt;br /&gt;assigned, rather &lt;br /&gt;than words like 'hi'. Prefixes or namespaces like xmlns, are reserved by XML. &lt;br /&gt;The concept of&lt;br /&gt;namespaces in XML is identical to the concept of namespaces in C#.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;The third parameter is a Uniform Resource Identifier (URI). This parameter &lt;br /&gt;reveals greater &lt;br /&gt;details about the location of the namespace. It informs XML that somewhere &lt;br /&gt;within the document, &lt;br /&gt;additional information about the namespace 'hi' is available. In this case it is &lt;br /&gt;at xxx:yyy. As&lt;br /&gt;the WriteStartAttribute function does not specify any value for the attribute, &lt;br /&gt;the WriteString&lt;br /&gt;function is employed to assign the value 1-861003-78, to the attribute 'mukhi' &lt;br /&gt;in the namespace &lt;br&gt;&lt;br /&gt;'hi'.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;a.cs&lt;br&gt;&lt;br /&gt;using System;&lt;br&gt;&lt;br /&gt;using System.Xml;&lt;br&gt;&lt;br /&gt;public class zzz&lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;public static void Main() {&lt;br&gt;&lt;br /&gt;XmlTextWriter a = new XmlTextWriter (&amp;quot;b.xml&amp;quot;, null);&lt;br&gt;&lt;br /&gt;a.WriteStartDocument();&lt;br&gt;&lt;br /&gt;a.Formatting = Formatting.Indented;&lt;br&gt;&lt;br /&gt;a.Indentation = 3;&lt;br&gt;&lt;br /&gt;a.WriteDocType(&amp;quot;vijay&amp;quot;, null, null ,null);&lt;br&gt;&lt;br /&gt;a.WriteStartElement(&amp;quot;vijay&amp;quot;);&lt;br&gt;&lt;br /&gt;a.WriteAttributeString(&amp;quot;xmlns&amp;quot;, &amp;quot;bk&amp;quot;, null, &amp;quot;sonal:wife&amp;quot;);&lt;br&gt;&lt;br /&gt;string p = a.LookupPrefix(&amp;quot;sonal:wife&amp;quot;);&lt;br&gt;&lt;br /&gt;a.WriteStartAttribute(p, &amp;quot;mukhi&amp;quot;, &amp;quot;sonal:wife&amp;quot;);&lt;br&gt;&lt;br /&gt;a.WriteString(&amp;quot;sonal&amp;quot;);&lt;br&gt;&lt;br /&gt;a.WriteEndAttribute(); &lt;br&gt;&lt;br /&gt;a.Flush();&lt;br&gt;&lt;br /&gt;a.Close();&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;b.xml&lt;br&gt;&lt;br /&gt;&amp;lt;?xml version=&amp;quot;1.0&amp;quot;?&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;!DOCTYPE vijay&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;vijay xmlns:bk=&amp;quot;sonal:wife&amp;quot; bk:mukhi=&amp;quot;sonal&amp;quot; /&amp;gt;&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;Here, the function WriteAttributeString is called with four parameters. The &lt;br /&gt;first, as always, is&lt;br /&gt;the name of the namespace, i.e. xmlns. The second is the name of the attribute &lt;br /&gt;i.e. bk, which is&lt;br /&gt;suffixed to the name of the namespace, as xmlns:bk. The third parameter is the &lt;br /&gt;namespace URI. In&lt;br /&gt;the earlier program, we had specified the value of xxx:yyy for the URI. For this &lt;br /&gt;program, since&lt;br /&gt;the namespace xmlns is a reserved namespace, the URI parameter is specified as &lt;br /&gt;null. The last &lt;br&gt;&lt;br /&gt;parameter is the value of the attribute. &lt;br&gt;&lt;br /&gt;As a consequence, the above function takes the form of an attribute consisting &lt;br /&gt;of &lt;br /&gt;xmlns:bk=sonal:wife. The next function LookupPrefix, accepts a namespace URI and &lt;br /&gt;returns the&lt;br /&gt;prefix. As the parameter supplied to this function is sonal:wife, the prefix &lt;br /&gt;returned is bk,&lt;br /&gt;which is stored in p.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;The WriteStartAttribute then uses the following:&lt;br&gt;&lt;br /&gt;• 'bk' as the namespace,&lt;br&gt;&lt;br /&gt;• 'mukhi' as the name of the attribute, and&lt;br&gt;&lt;br /&gt;• 'sonal:wife' as the namespace URI. &lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;Thus, the attribute 'mukhi' is prefaced with the namespace 'bk'. Finally, the &lt;br /&gt;WriteString &lt;br&gt;&lt;br /&gt;function assigns the value of 'sonal' to the attribute bk:mukhi.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;a.cs&lt;br&gt;&lt;br /&gt;using System;&lt;br&gt;&lt;br /&gt;using System.Xml;&lt;br&gt;&lt;br /&gt;public class zzz&lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;public static void Main()&lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;XmlTextWriter a = new XmlTextWriter (&amp;quot;b.xml&amp;quot;, null);&lt;br&gt;&lt;br /&gt;a.WriteStartDocument();&lt;br&gt;&lt;br /&gt;a.Formatting = Formatting.Indented;&lt;br&gt;&lt;br /&gt;a.Indentation = 3;&lt;br&gt;&lt;br /&gt;a.WriteDocType(&amp;quot;vijay&amp;quot;, null, null ,null);&lt;br&gt;&lt;br /&gt;a.WriteStartElement(&amp;quot;vijay&amp;quot;);&lt;br&gt;&lt;br /&gt;a.WriteAttributeString(&amp;quot;xmlns&amp;quot;, &amp;quot;bk&amp;quot;, null, &amp;quot;sonal:wife&amp;quot;);&lt;br&gt;&lt;br /&gt;a.WriteAttributeString(&amp;quot;jjj&amp;quot;, &amp;quot;bk&amp;quot;, &amp;quot;kkk&amp;quot;, &amp;quot;sonal:wife&amp;quot;);&lt;br&gt;&lt;br /&gt;a.Flush();&lt;br&gt;&lt;br /&gt;a.Close();&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;b.xml&lt;br&gt;&lt;br /&gt;&amp;lt;?xml version=&amp;quot;1.0&amp;quot;?&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;!DOCTYPE vijay&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;vijay xmlns:bk=&amp;quot;sonal:wife&amp;quot; jjj:bk=&amp;quot;sonal:wife&amp;quot; xmlns:jjj=&amp;quot;kkk&amp;quot; /&amp;gt;&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;In this version of the WriteAttributeString function, the namespace is jjj and &lt;br /&gt;the attribute&lt;br /&gt;name is bk, with the value sonal:wife. Thus, the attribute becomes &lt;br /&gt;jjj:bk=sonal:wife. The third&lt;br /&gt;parameter to the function is the namespace URI, which is now assigned a value of &lt;br /&gt;kkk, instead of&lt;br /&gt;null. &lt;br /&gt;&lt;br&gt;&lt;br /&gt;Thus, one more attribute xmlns:jjj gets added, which indicates that the &lt;br /&gt;namespace URI is kkk. We&lt;br /&gt;notice that this attribute does not get added for the xmlns namespace. We have &lt;br /&gt;chosen the&lt;br /&gt;attribute name 'bk' again, just to demonstrate that they belong to different &lt;br /&gt;namespaces. &lt;br&gt;&lt;br /&gt;Therefore, this bk is considered to be a different attribute from the earlier &lt;br /&gt;bk.&lt;br&gt;&lt;br /&gt;a.cs&lt;br&gt;&lt;br /&gt;using System;&lt;br&gt;&lt;br /&gt;using System.Xml;&lt;br&gt;&lt;br /&gt;public class zzz&lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;public static void Main()&lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;XmlTextWriter a = new XmlTextWriter (&amp;quot;b.xml&amp;quot;, null);&lt;br&gt;&lt;br /&gt;a.WriteStartDocument();&lt;br&gt;&lt;br /&gt;a.Formatting = Formatting.Indented;&lt;br&gt;&lt;br /&gt;a.Indentation = 3;&lt;br&gt;&lt;br /&gt;a.WriteDocType(&amp;quot;vijay&amp;quot;, null, null ,null);&lt;br&gt;&lt;br /&gt;a.WriteStartElement(&amp;quot;vijay&amp;quot;);&lt;br&gt;&lt;br /&gt;a.WriteStartAttribute(null,&amp;quot;sonal&amp;quot;, null);&lt;br&gt;&lt;br /&gt;a.WriteQualifiedName(&amp;quot;mukhi&amp;quot;, &amp;quot;http://vijaymukhi.com&amp;quot;);&lt;br&gt;&lt;br /&gt;a.WriteEndAttribute();&lt;br&gt;&lt;br /&gt;a.Flush();&lt;br&gt;&lt;br /&gt;a.Close();&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;b.xml&lt;br&gt;&lt;br /&gt;&amp;lt;?xml version=&amp;quot;1.0&amp;quot;?&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;!DOCTYPE vijay&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;vijay sonal=&amp;quot;n1:mukhi&amp;quot; xmlns:n1=&amp;quot;http://vijaymukhi.com&amp;quot; /&amp;gt;&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;In the WriteStartAttribute function, only the second parameter out of the three &lt;br /&gt;parameters, has&lt;br /&gt;a value 'sonal, which is the name of the attribute. The first parameter, which &lt;br /&gt;is the name of&lt;br /&gt;the namespace and the third parameter, which is the URI of the namespace, are &lt;br /&gt;both assigned null &lt;br /&gt;values. &lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;The next function, WriteQualifiedName assigns a value to the attribute 'sonal'. &lt;br /&gt;This function&lt;br /&gt;takes two parameters, the value 'mukhi' and the namespace URI for the value. &lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;The value 'mukhi' gets prefaced by a namespace n1, which is created dynamically &lt;br /&gt;by XML. The name&lt;br /&gt;n1 belongs to the reserved xmlns namespace and the URI to n1 is specified in the &lt;br /&gt;second&lt;br /&gt;parameter, http://vijaymukhi.com. The method WriteQualifiedName, then looks up &lt;br /&gt;the prefix within&lt;br /&gt;the scope for the given namespace.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;a.cs&lt;br&gt;&lt;br /&gt;using System;&lt;br&gt;&lt;br /&gt;using System.Xml;&lt;br&gt;&lt;br /&gt;public class zzz&lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;public static void Main()&lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;XmlTextWriter a = new XmlTextWriter (&amp;quot;b.xml&amp;quot;, null);&lt;br&gt;&lt;br /&gt;a.WriteStartDocument();&lt;br&gt;&lt;br /&gt;a.Formatting = Formatting.Indented;&lt;br&gt;&lt;br /&gt;a.WriteStartElement(&amp;quot;vijay&amp;quot;);&lt;br&gt;&lt;br /&gt;a.WriteAttributeString(&amp;quot;xmlns&amp;quot;,&amp;quot;mukhi&amp;quot;,null,&amp;quot;xxx:yyy&amp;quot;);&lt;br&gt;&lt;br /&gt;a.WriteString(&amp;quot;Hi &amp;quot;);&lt;br&gt;&lt;br /&gt;a.WriteQualifiedName(&amp;quot;sonal&amp;quot;,&amp;quot;xxx:yyy&amp;quot;);&lt;br&gt;&lt;br /&gt;a.Flush();&lt;br&gt;&lt;br /&gt;a.Close();&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;b.xml&lt;br&gt;&lt;br /&gt;&amp;lt;?xml version=&amp;quot;1.0&amp;quot;?&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;vijay xmlns:mukhi=&amp;quot;xxx:yyy&amp;quot;&amp;gt;Hi mukhi:sonal&amp;lt;/vijay&amp;gt;&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;In this example, we first create an attribute 'mukhi' in the reserved namespace &lt;br /&gt;xmlns. This&lt;br /&gt;attribute is then rendered a value of xxx:yyy. The WriteString function writes &lt;br /&gt;'Hi' as the&lt;br /&gt;content and then, the WriteQualifiedName writes the string 'sonal'. However, &lt;br /&gt;since 'sonal' is a&lt;br /&gt;Qualified name, it is prefaced by 'mukhi' and not by xxx:yyy, because 'mukhi' is &lt;br /&gt;equated to &lt;br&gt;&lt;br /&gt;xxx:yyy. &lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;The prefix in the scope for the namespace is given precedence.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;a.cs&lt;br&gt;&lt;br /&gt;using System;&lt;br&gt;&lt;br /&gt;using System.Xml;&lt;br&gt;&lt;br /&gt;public class zzz&lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;public static void Main() {&lt;br&gt;&lt;br /&gt;XmlTextWriter a = new XmlTextWriter (&amp;quot;b.xml&amp;quot;, null);&lt;br&gt;&lt;br /&gt;a.WriteStartDocument();&lt;br&gt;&lt;br /&gt;a.Formatting = Formatting.Indented;&lt;br&gt;&lt;br /&gt;a.WriteStartElement(&amp;quot;vijay&amp;quot;);&lt;br&gt;&lt;br /&gt;a.WriteElementString(&amp;quot;vijay&amp;quot;,&amp;quot;mukhi&amp;quot;);&lt;br&gt;&lt;br /&gt;a.WriteElementString(&amp;quot;vijay&amp;quot;,&amp;quot;sonal&amp;quot;,&amp;quot;mukhi&amp;quot;);&lt;br&gt;&lt;br /&gt;a.Flush();&lt;br&gt;&lt;br /&gt;a.Close();&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;b.xml&lt;br&gt;&lt;br /&gt;&amp;lt;?xml version=&amp;quot;1.0&amp;quot;?&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;vijay&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;vijay&amp;gt;mukhi&amp;lt;/vijay&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;vijay xmlns=&amp;quot;sonal&amp;quot;&amp;gt;mukhi&amp;lt;/vijay&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;/vijay&amp;gt;&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;As we have just observed, the WriteElementString function had only two &lt;br /&gt;parameters in the earlier&lt;br /&gt;program. However, here it has three parameters. The first and the third &lt;br /&gt;parameters are the same, &lt;br /&gt;i.e. the tag name and the value. The newly inducted second parameter indicates &lt;br /&gt;the namespace &lt;br /&gt;'sonal'. The tag in the first parameter 'vijay', has the namespace of sonal. &lt;br /&gt;Thus, the XML file&lt;br /&gt;contains the tag with the attribute of xmlns=sonal.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;a.cs&lt;br&gt;&lt;br /&gt;using System;&lt;br&gt;&lt;br /&gt;using System.Xml;&lt;br&gt;&lt;br /&gt;public class zzz&lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;public static void Main() {&lt;br&gt;&lt;br /&gt;XmlTextWriter a = new XmlTextWriter (Console.Out);&lt;br&gt;&lt;br /&gt;a.WriteStartDocument();&lt;br&gt;&lt;br /&gt;a.WriteStartElement(&amp;quot;vijay&amp;quot;);&lt;br&gt;&lt;br /&gt;a.Close();&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;Output&lt;br&gt;&lt;br /&gt;&amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;IBM437&amp;quot;?&amp;gt;&amp;lt;vijay /&amp;gt;&lt;br&gt;&lt;br /&gt;The XmlTextWriter class can write to different entities, using the constructor &lt;br /&gt;that accepts a&lt;br /&gt;single parameter. The Console class has a static property out of datatype &lt;br /&gt;TextWriter that&lt;br /&gt;represents the console. Thus, the output is now displayed on the console. By &lt;br /&gt;default, the &lt;br /&gt;encoding attribute is assigned a value of IBM437.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;One of the primary reasons for designing XML was to introduce validation of the &lt;br /&gt;tags in order to &lt;br /&gt;produce a well-evolved XML file.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;There are a few validations that need to be performed in an XML file, such as:&lt;br&gt;&lt;br /&gt;• It should be ensured that the basic rules of XML as well as our indigenous &lt;br /&gt;rules are &lt;br&gt;&lt;br /&gt;followed. &lt;br&gt;&lt;br /&gt;• Certain tags should be placed only within specified tags and cannot be used&lt;br /&gt;&lt;br&gt;&lt;br /&gt;independently.&lt;br&gt;&lt;br /&gt;• The number of times a tag is being used can be regulated, since it cannot be &lt;br /&gt;used &lt;br&gt;&lt;br /&gt;infinite times. &lt;br&gt;&lt;br /&gt;• A check should be placed on the name and the number of times an attribute is &lt;br /&gt;used within &lt;br&gt;&lt;br /&gt;a tag.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;All such rules that need to be enforced are enunciated in XML parlance and then, &lt;br /&gt;placed in a DTD&lt;br /&gt;or a Document Type Description. The DTD may either be placed in a separate file &lt;br /&gt;or may be made&lt;br /&gt;part of the DOCTYPE declaration. In the XML file shown below, the DTD is &lt;br /&gt;internal. &lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;Thus, a DTD stores the grammar that is permissible in an XML file. The entity &lt;br /&gt;refs are also&lt;br /&gt;defined in a DTD. One of the reasons why HTML is also reffered to as XHTML is &lt;br /&gt;that, the rules of&lt;br /&gt;well-formed html are available in the form of a DTD.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;a.cs&lt;br&gt;&lt;br /&gt;using System;&lt;br&gt;&lt;br /&gt;using System.Xml;&lt;br&gt;&lt;br /&gt;public class zzz&lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;public static void Main()&lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;XmlTextWriter a = new XmlTextWriter (&amp;quot;b.xml&amp;quot;, null);&lt;br&gt;&lt;br /&gt;a.WriteStartDocument();&lt;br&gt;&lt;br /&gt;a.Formatting = Formatting.Indented;&lt;br&gt;&lt;br /&gt;String s = &amp;quot;&amp;lt;!ELEMENT vijay (#PCDATA)&amp;gt;&amp;quot;;&lt;br&gt;&lt;br /&gt;a.WriteDocType(&amp;quot;vijay&amp;quot;, null, null, s);&lt;br&gt;&lt;br /&gt;a.WriteStartElement(&amp;quot;vijay&amp;quot;);&lt;br&gt;&lt;br /&gt;a.Flush();&lt;br&gt;&lt;br /&gt;a.Close();&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;b.xml&lt;br&gt;&lt;br /&gt;&amp;lt;?xml version=&amp;quot;1.0&amp;quot;?&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;!DOCTYPE vijay[&amp;lt;!ELEMENT vijay (#PCDATA)&amp;gt;]&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;vijay /&amp;gt;&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;The WriteDocType function accepts four parameters. The first parameter is the &lt;br /&gt;starting or root&lt;br /&gt;tag 'vijay'. Hence, it must contain a value. The last parameter is the subset &lt;br /&gt;(as referred to by&lt;br /&gt;the documentation), which follows the root tag 'vijay'. If you observe the &lt;br /&gt;DOCTYPE statement&lt;br /&gt;carefully, you will notice that an extra pair of square brackets [], have been &lt;br /&gt;added.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;a.cs&lt;br&gt;&lt;br /&gt;using System;&lt;br&gt;&lt;br /&gt;using System.Xml;&lt;br&gt;&lt;br /&gt;public class zzz&lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;public static void Main()&lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;XmlTextWriter a = new XmlTextWriter (&amp;quot;b.xml&amp;quot;, null);&lt;br&gt;&lt;br /&gt;a.WriteStartDocument();&lt;br&gt;&lt;br /&gt;a.Formatting = Formatting.Indented;&lt;br&gt;&lt;br /&gt;a.WriteDocType(&amp;quot;vijay&amp;quot;, null, &amp;quot;a.dtd&amp;quot;, null);&lt;br&gt;&lt;br /&gt;a.WriteStartElement(&amp;quot;vijay&amp;quot;);&lt;br&gt;&lt;br /&gt;a.Flush();&lt;br&gt;&lt;br /&gt;a.Close();&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;b.xml&lt;br&gt;&lt;br /&gt;&amp;lt;?xml version=&amp;quot;1.0&amp;quot;?&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;!DOCTYPE vijay SYSTEM &amp;quot;a.dtd&amp;quot;&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;vijay /&amp;gt;&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;The third parameter to WriteDocType function specifies the name of the DTD file. &lt;br /&gt;In other words,&lt;br /&gt;it states the URI of the DTD. The second parameter is assigned the value of &lt;br /&gt;null. Hence, the&lt;br /&gt;word SYSTEM is displayed before the name of the file, in the XML file. &lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;Whenever XML wishes to ensure the validity of an XML file, it ascertains the &lt;br /&gt;rules from a.dtd.&lt;br /&gt;If both internal and external DTDs are present, both of them are checked. &lt;br /&gt;However, the internal&lt;br /&gt;DTD is accorded priority. &lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;a.cs&lt;br&gt;&lt;br /&gt;using System;&lt;br&gt;&lt;br /&gt;using System.Xml;&lt;br&gt;&lt;br /&gt;public class zzz&lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;public static void Main()&lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;XmlTextWriter a = new XmlTextWriter (&amp;quot;b.xml&amp;quot;, null);&lt;br&gt;&lt;br /&gt;a.WriteStartDocument();&lt;br&gt;&lt;br /&gt;a.Formatting = Formatting.Indented;&lt;br&gt;&lt;br /&gt;a.WriteDocType(&amp;quot;vijay&amp;quot;, &amp;quot;mmm&amp;quot;, &amp;quot;a.dtd&amp;quot;, null);&lt;br&gt;&lt;br /&gt;a.WriteStartElement(&amp;quot;vijay&amp;quot;);&lt;br&gt;&lt;br /&gt;a.Flush();&lt;br&gt;&lt;br /&gt;a.Close();&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;b.xml&lt;br&gt;&lt;br /&gt;&amp;lt;?xml version=&amp;quot;1.0&amp;quot;?&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;!DOCTYPE vijay PUBLIC &amp;quot;mmm&amp;quot; &amp;quot;a.dtd&amp;quot;&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;vijay /&amp;gt;&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;In the earlier program, SYSTEM was added in the XML file, since the second &lt;br /&gt;parameter had been &lt;br /&gt;specified as null. However, in this program, the second parameter is not null. &lt;br /&gt;Hence, the word &lt;br /&gt;PUBLIC gets added. Thereafter, the string or the id specified in the second &lt;br /&gt;parameter is added. &lt;br&gt;&lt;br /&gt;And then, the dtd in the third parameter is specified.&lt;br&gt;&lt;br /&gt;Therefore, it is either the PUBLIC identifier or the SYSTEM identifier, which &lt;br /&gt;would be present. &lt;br&gt;&lt;br /&gt;The XML program or the processor scanning the XML file, uses the PUBLIC &lt;br /&gt;identifier to retrieve &lt;br&gt;&lt;br /&gt;the content for the entities that use the URI. If it fails, it falls back upon &lt;br /&gt;to the SYSTEM &lt;br&gt;&lt;br /&gt;literal.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;a.cs&lt;br&gt;&lt;br /&gt;using System;&lt;br&gt;&lt;br /&gt;using System.Xml;&lt;br&gt;&lt;br /&gt;public class zzz&lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;public static void Main() {&lt;br&gt;&lt;br /&gt;XmlTextWriter a = new XmlTextWriter (&amp;quot;b.xml&amp;quot;, null);&lt;br&gt;&lt;br /&gt;a.WriteStartDocument(false);&lt;br&gt;&lt;br /&gt;a.Flush();&lt;br&gt;&lt;br /&gt;a.Close();&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;b.xml&lt;br&gt;&lt;br /&gt;&amp;lt;?xml version=&amp;quot;1.0&amp;quot; standalone=&amp;quot;no&amp;quot;?&amp;gt;&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;The WriteStartDocument can take a boolean parameter that adds an attribute which &lt;br /&gt;could either be&lt;br /&gt;'standalone = yes' or 'standalone=no', depending upon the value specified. This &lt;br /&gt;attribute &lt;br /&gt;determines whether the DTD is in an external file or it is internal to the XML &lt;br /&gt;file. If the&lt;br /&gt;standalone has a value of 'yes', it is suggestive of the fact that there is no &lt;br /&gt;external DTD, and&lt;br /&gt;therefore, all the grammatical rules have to be placed within the XML file &lt;br /&gt;itself. &lt;br&gt;&lt;br /&gt;a.cs&lt;br&gt;&lt;br /&gt;using System;&lt;br&gt;&lt;br /&gt;using System.Xml;&lt;br&gt;&lt;br /&gt;public class zzz&lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;public static void Main() {&lt;br&gt;&lt;br /&gt;XmlTextWriter a = new XmlTextWriter (&amp;quot;b.xml&amp;quot;, null);&lt;br&gt;&lt;br /&gt;System.Console.WriteLine(a.WriteState);&lt;br&gt;&lt;br /&gt;a.WriteStartDocument();&lt;br&gt;&lt;br /&gt;System.Console.WriteLine(a.WriteState);&lt;br&gt;&lt;br /&gt;a.Formatting = Formatting.Indented;&lt;br&gt;&lt;br /&gt;a.Indentation = 3;&lt;br&gt;&lt;br /&gt;a.WriteDocType(&amp;quot;vijay&amp;quot;, null, null ,null);&lt;br&gt;&lt;br /&gt;System.Console.WriteLine(a.WriteState);&lt;br&gt;&lt;br /&gt;a.WriteStartElement(&amp;quot;vijay&amp;quot;);&lt;br&gt;&lt;br /&gt;System.Console.WriteLine(a.WriteState);&lt;br&gt;&lt;br /&gt;a.WriteAttributeString (&amp;quot;wife&amp;quot;,&amp;quot;sonal&amp;quot;);&lt;br&gt;&lt;br /&gt;System.Console.WriteLine(a.WriteState);&lt;br&gt;&lt;br /&gt;a.WriteStartAttribute(&amp;quot;hi&amp;quot;, &amp;quot;mukhi&amp;quot;, &amp;quot;xxx:yyy&amp;quot;);&lt;br&gt;&lt;br /&gt;System.Console.WriteLine(a.WriteState);&lt;br&gt;&lt;br /&gt;a.WriteString(&amp;quot;1-861003-78&amp;quot;);&lt;br&gt;&lt;br /&gt;a.WriteElementString(&amp;quot;surname&amp;quot;, &amp;quot;mukhi&amp;quot;);&lt;br&gt;&lt;br /&gt;a.Flush();&lt;br&gt;&lt;br /&gt;System.Console.WriteLine(a.WriteState);&lt;br&gt;&lt;br /&gt;a.Close();&lt;br&gt;&lt;br /&gt;System.Console.WriteLine(a.WriteState);&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;Output&lt;br&gt;&lt;br /&gt;Start&lt;br&gt;&lt;br /&gt;Prolog&lt;br&gt;&lt;br /&gt;Prolog&lt;br&gt;&lt;br /&gt;Element&lt;br&gt;&lt;br /&gt;Element&lt;br&gt;&lt;br /&gt;Attribute&lt;br&gt;&lt;br /&gt;Content&lt;br&gt;&lt;br /&gt;Closed&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;b.xml&lt;br&gt;&lt;br /&gt;&amp;lt;?xml version=&amp;quot;1.0&amp;quot;?&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;!DOCTYPE vijay&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;vijay wife=&amp;quot;sonal&amp;quot; hi:mukhi=&amp;quot;1-861003-78&amp;quot; xmlns:hi=&amp;quot;xxx:yyy&amp;quot;&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;surname&amp;gt;mukhi&amp;lt;/surname&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;/vijay&amp;gt;&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;The XmlTextWriter object can be in any one of six different states. The &lt;br /&gt;WriteState property&lt;br /&gt;reveals its current state. When an XmlTextWriter Object is created, it is in the &lt;br /&gt;Start state, as&lt;br /&gt;may be evident from the fact that, no write method has been called so far. After &lt;br /&gt;the Close &lt;br /&gt;function, the Writer is in the Closed state. When the WriteStartDocument and &lt;br /&gt;WriteDocType&lt;br /&gt;functions are called, they reach the Prolog state, because the prolog is being &lt;br /&gt;written. &lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;The WriteStartElement function actually starts writing to the XML file, thereby, &lt;br /&gt;morphing to the&lt;br /&gt;Element state. The element start tag 'vijay' begins the XML file. The next &lt;br /&gt;function&lt;br /&gt;WriteAttributeString does not change the state, since the element in focus still &lt;br /&gt;is 'vijay'. The&lt;br /&gt;WriteStartAttribute function needs the WriteString to complete the attribute. &lt;br /&gt;Thus, after the&lt;br /&gt;WriteStartAttribute function executes, the Text Writer assumes the Attribute &lt;br /&gt;mode. The surname&lt;br /&gt;attribute becomes the content in the XML file. Hence, the state changes to &lt;br /&gt;Content mode. &lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;This goes on to prove that the TextWriter can possibly be in any one of the &lt;br /&gt;above six states,&lt;br /&gt;depending upon the entities written to the file. While the TextWrtier is in the &lt;br /&gt;Attribute state,&lt;br /&gt;it cannot switch to an element state to write an element. Therefore, it throws &lt;br /&gt;an exception.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;a.cs&lt;br&gt;&lt;br /&gt;using System;&lt;br&gt;&lt;br /&gt;using System.Xml;&lt;br&gt;&lt;br /&gt;public class zzz&lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;public static void Main()&lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;XmlTextWriter a = new XmlTextWriter (&amp;quot;b.xml&amp;quot;, null);&lt;br&gt;&lt;br /&gt;a.Namespaces = false;&lt;br&gt;&lt;br /&gt;a.WriteStartDocument();&lt;br&gt;&lt;br /&gt;a.WriteDocType(&amp;quot;vijay&amp;quot;, null, null ,null);&lt;br&gt;&lt;br /&gt;a.WriteStartElement(&amp;quot;vijay&amp;quot;);&lt;br&gt;&lt;br /&gt;a.WriteAttributeString(&amp;quot;jjj&amp;quot;, &amp;quot;bk&amp;quot;, &amp;quot;kkk&amp;quot;, &amp;quot;sonal:wife&amp;quot;);&lt;br&gt;&lt;br /&gt;a.Flush();&lt;br&gt;&lt;br /&gt;a.Close();&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;Output&lt;br&gt;&lt;br /&gt;Unhandled Exception: System.ArgumentException: Cannot set the namespace if &lt;br /&gt;Namespaces is &lt;br&gt;&lt;br /&gt;'false'.&lt;br&gt;&lt;br /&gt;at System.Xml.XmlTextWriter.WriteStartAttribute(String prefix, String localName, &lt;br /&gt;String ns)&lt;br&gt;&lt;br /&gt;at System.Xml.XmlWriter.WriteAttributeString(String prefix, String localName, &lt;br /&gt;String ns, String &lt;br&gt;&lt;br /&gt;value) &lt;br&gt;&lt;br /&gt;at zzz.Main()&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;The TextWriter class has a Namespaces property that is read-write, and it has a &lt;br /&gt;default value of &lt;br&gt;&lt;br /&gt;true. The Namespace property is turned off, by setting this property to false. &lt;br /&gt;The above runtime &lt;br&gt;&lt;br /&gt;exception is thrown because, we have attempted to introduce a namespace jjj, in &lt;br /&gt;the &lt;br&gt;&lt;br /&gt;WriteAttributeString function.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;a.cs&lt;br&gt;&lt;br /&gt;using System;&lt;br&gt;&lt;br /&gt;using System.Xml;&lt;br&gt;&lt;br /&gt;public class zzz {&lt;br&gt;&lt;br /&gt;public static void Main() {&lt;br&gt;&lt;br /&gt;XmlTextWriter a = new XmlTextWriter (&amp;quot;b.xml&amp;quot;, null);&lt;br&gt;&lt;br /&gt;a.WriteStartDocument();&lt;br&gt;&lt;br /&gt;a.QuoteChar = '\'';&lt;br&gt;&lt;br /&gt;a.Formatting = Formatting.Indented;&lt;br&gt;&lt;br /&gt;a.Indentation = 3;&lt;br&gt;&lt;br /&gt;a.WriteDocType(&amp;quot;vijay&amp;quot;, null, null ,null);&lt;br&gt;&lt;br /&gt;a.WriteStartElement(&amp;quot;vijay&amp;quot;);&lt;br&gt;&lt;br /&gt;a.WriteAttributeString(&amp;quot;jjj&amp;quot;, &amp;quot;bk&amp;quot;);&lt;br&gt;&lt;br /&gt;a.Flush();&lt;br&gt;&lt;br /&gt;a.Close();&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;b.xml&lt;br&gt;&lt;br /&gt;&amp;lt;?xml version=&amp;quot;1.0&amp;quot;?&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;!DOCTYPE vijay&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;vijay jjj='bk' /&amp;gt;&lt;br&gt;&lt;br /&gt;Various facets of XML can be modified. By using the property QuoteChar, we can &lt;br /&gt;modify the&lt;br /&gt;default quoting character, from double inverted commas to single inverted &lt;br /&gt;commas. Since a single &lt;br /&gt;quote cannot be enclosed within a set of single quotes, we use the backslash to &lt;br /&gt;escape it. All &lt;br /&gt;attributes can now be placed in single quotes instead of double quotes.&lt;br&gt;&lt;br /&gt;a.cs&lt;br&gt;&lt;br /&gt;using System;&lt;br&gt;&lt;br /&gt;using System.Xml;&lt;br&gt;&lt;br /&gt;public class zzz&lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;public static void Main()&lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;XmlTextWriter a = new XmlTextWriter (&amp;quot;b.xml&amp;quot;, null);&lt;br&gt;&lt;br /&gt;a.WriteStartDocument();&lt;br&gt;&lt;br /&gt;a.Formatting = Formatting.Indented;&lt;br&gt;&lt;br /&gt;a.Indentation = 3;&lt;br&gt;&lt;br /&gt;a.WriteDocType(&amp;quot;vijay&amp;quot;, null, null ,null);&lt;br&gt;&lt;br /&gt;a.WriteStartElement(&amp;quot;vijay&amp;quot;);&lt;br&gt;&lt;br /&gt;a.WriteStartAttribute(&amp;quot;hi&amp;quot;, &amp;quot;mukhi&amp;quot;, &amp;quot;xxx:yyy&amp;quot;);&lt;br&gt;&lt;br /&gt;a.WriteString(&amp;quot;1-861003-78&amp;quot;);&lt;br&gt;&lt;br /&gt;a.WriteEndAttribute(); &lt;br&gt;&lt;br /&gt;a.WriteEndElement();&lt;br&gt;&lt;br /&gt;a.WriteEndDocument();&lt;br&gt;&lt;br /&gt;a.Flush();&lt;br&gt;&lt;br /&gt;a.Close();&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;b.xml&lt;br&gt;&lt;br /&gt;&amp;lt;?xml version=&amp;quot;1.0&amp;quot;?&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;!DOCTYPE vijay&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;vijay hi:mukhi=&amp;quot;1-861003-78&amp;quot; xmlns:hi=&amp;quot;xxx:yyy&amp;quot; /&amp;gt;&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;Good programming style necessitates every 'open' to have a corresponding &lt;br /&gt;'close'. Thus, the &lt;br /&gt;Begin functions for an Element, Attribute and Document have corresponding Close &lt;br /&gt;functions too. &lt;br /&gt;However, if we do not End them, they close by default and no major calamity &lt;br /&gt;befalls them. We are &lt;br /&gt;using them in the above program as an abandon caution. &lt;br /&gt;The WriteEndDocument function puts the Text Writer in the Start mode.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;Reading an XML file&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;b.xml&lt;br&gt;&lt;br /&gt;&amp;lt;?xml version=&amp;quot;1.0&amp;quot; standalone=&amp;quot;yes&amp;quot;?&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;!DOCTYPE vijay SYSTEM &amp;quot;a.dtd&amp;quot; [&amp;lt;!ENTITY baby &amp;quot;No&amp;quot;&amp;gt;]&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;vijay aa=&amp;quot;no&amp;quot;&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;!--comment 2--&amp;gt;&amp;lt;?sonal mukhi=no?&amp;gt;&lt;br&gt;&lt;br /&gt;Hi&amp;amp;baby;&lt;br&gt;&lt;br /&gt;&amp;lt;![CDATA[,mukhi&amp;gt;]]&amp;gt;&amp;lt;aa&amp;gt;bb&amp;lt;/aa&amp;gt;&lt;br&gt;&lt;br /&gt;&amp;lt;/vijay&amp;gt;&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;&amp;gt; copy con a.dtd&lt;br&gt;&lt;br /&gt;Enter&lt;br&gt;&lt;br /&gt;^Z&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;a.cs&lt;br&gt;&lt;br /&gt;using System;&lt;br&gt;&lt;br /&gt;using System.IO;&lt;br&gt;&lt;br /&gt;using System.Xml;&lt;br&gt;&lt;br /&gt;public class zzz&lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;public static void Main() {&lt;br&gt;&lt;br /&gt;XmlTextReader r;&lt;br&gt;&lt;br /&gt;r = new XmlTextReader(&amp;quot;b.xml&amp;quot;);&lt;br&gt;&lt;br /&gt;while (r.Read())&lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;Console.Write(&amp;quot;{0} D={1} L={2} P={3} &amp;quot;, r.NodeType, r.Depth, r.LineNumber, &lt;br /&gt;r.LinePosition );&lt;br&gt;&lt;br /&gt;Console.Write(&amp;quot; name={0} value={1} AC={2}&amp;quot;,r.Name,r.Value,r.AttributeCount);&lt;br&gt;&lt;br /&gt;Console.WriteLine();&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;Output&lt;br&gt;&lt;br /&gt;XmlDeclaration D=0 L=1 P=3 name=xml value=version=&amp;quot;1.0&amp;quot; standalone=&amp;quot;yes&amp;quot; AC=2&lt;br&gt;&lt;br /&gt;Whitespace D=0 L=1 P=39 name= value= AC=0&lt;br&gt;&lt;br /&gt;DocumentType D=0 L=2 P=11 name=vijay value=&amp;lt;!ENTITY baby &amp;quot;No&amp;quot;&amp;gt; AC=1&lt;br&gt;&lt;br /&gt;Whitespace D=0 L=2 P=54 name= value= AC=0&lt;br&gt;&lt;br /&gt;Element D=0 L=3 P=2 name=vijay value= AC=1&lt;br&gt;&lt;br /&gt;Whitespace D=1 L=3 P=16 name= value= AC=0&lt;br&gt;&lt;br /&gt;Comment D=1 L=4 P=5 name= value=comment 2 AC=0&lt;br&gt;&lt;br /&gt;ProcessingInstruction D=1 L=4 P=19 name=sonal value=mukhi=no AC=0&lt;br&gt;&lt;br /&gt;Text D=1 L=4 P=36 name= value=Hi AC=0&lt;br&gt;&lt;br /&gt;EntityReference D=1 L=5 P=4 name=baby value= AC=0&lt;br&gt;&lt;br /&gt;Whitespace D=1 L=5 P=9 name= value= AC=0&lt;br&gt;&lt;br /&gt;CDATA D=1 L=6 P=10 name= value=,mukhi&amp;gt; AC=0&lt;br&gt;&lt;br /&gt;Element D=1 L=6 P=21 name=aa value= AC=0&lt;br&gt;&lt;br /&gt;Text D=2 L=6 P=24 name= value=bb AC=0&lt;br&gt;&lt;br /&gt;EndElement D=1 L=6 P=28 name=aa value= AC=0&lt;br&gt;&lt;br /&gt;Whitespace D=1 L=6 P=31 name= value= AC=0&lt;br&gt;&lt;br /&gt;EndElement D=0 L=7 P=3 name=vijay value= AC=0&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;In this program, we read an XML file and display all the nodes contained &lt;br /&gt;therein. To avoid any &lt;br /&gt;errors from being displayed, you should create an empty file by the name of &lt;br /&gt;a.dtd.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;We have a class called XmlTextReader that accepts a filename as a parameter. We &lt;br /&gt;pass the&lt;br /&gt;filename b.xml to it. This file contains most of the entities present in an XML &lt;br /&gt;file. The Read&lt;br /&gt;function in this class picks up a single node or XML entity at a time. It &lt;br /&gt;returns true, if there &lt;br /&gt;are more nodes to be read, or else, it returns false. Thus, when there are no &lt;br /&gt;more nodes to be &lt;br /&gt;read from the file, the while loop ends. The Read function scans the active node &lt;br /&gt;and displays &lt;br /&gt;its contents in the loop. &lt;br&gt;&lt;br /&gt;&amp;nbsp;&lt;/p&gt;&lt;br /&gt;&lt;p align="justify"&gt;The NodeType property displays the name of the nodetype. As an XML file normally &lt;br /&gt;starts with a&lt;br /&gt;declaration, the NodeType property displays the NodeType as XMLDeclaration, &lt;br /&gt;using the ToString&lt;br /&gt;function. &lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;The Depth property gets incremented by one, every time an element or a tag is &lt;br /&gt;encountered. At &lt;br /&gt;the Declaration statement, the depth is 0. At the EndElement or at the end of &lt;br /&gt;the tag, its value&lt;br /&gt;reduces by one. Thus, the Depth property reveals the number of open tags in the &lt;br /&gt;file and it can &lt;br /&gt;be used for indentation.&lt;br&gt;&lt;br /&gt;The Line Number indicates the line on which the statement is positioned, while &lt;br /&gt;the LinePosition&lt;br /&gt;property displays the position on the line at which the statement begins. The &lt;br /&gt;Name property in &lt;br /&gt;the class reveals the name of the tag, XML. The output displayed by this &lt;br /&gt;property depends upon &lt;br /&gt;the active node type. On acute observation, you shall notice that the word XML &lt;br /&gt;is not preceded &lt;br /&gt;by the symbol &amp;lt;? in the output.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;The value property relates to the name property, in this case, to &lt;br /&gt;XmlDeclaration. It displays&lt;br /&gt;the entire gamut of attributes to the node. As there exist two attributes, &lt;br /&gt;version and &lt;br /&gt;standalone, the property AttributeCount displays a value of 2. &lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;If the enter key is pressed after the node declaration, it is interpreted as a &lt;br /&gt;Whitespace&lt;br /&gt;character. Whitespace characters are separators, which could consist of an &lt;br /&gt;enter, space et al.&lt;br /&gt;The Position property specifies the character position as 39. &lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;The XmlDeclaration has to be the first node in an XML file, and it cannot have &lt;br /&gt;any children. The&lt;br /&gt;DOCTYPE declaration, which is known as a DocumentType Node, displays the name as &lt;br /&gt;vijay, which is&lt;br /&gt;the root node. The value is displayed as &amp;lt;!ENTITY baby &amp;quot;No&amp;quot;&amp;gt;, which includes &lt;br /&gt;everything except &lt;br /&gt;the SYSTEM and a.dtd. Thus, in the case of a DocumentType Node, value is the &lt;br /&gt;internal DTD.&lt;br /&gt;&lt;br&gt;&lt;br /&gt;We shall encounter the Whitespace Node very frequently. Hence, we shall not &lt;br /&gt;discuss it&lt;br /&gt;hereinafter. The Attribute Count will be displayed in the next program. This &lt;br /&gt;node can have the &lt;br /&gt;Notation and Entity as child nodes.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;The next node in sequence is our very first element or tag 'vijay', which is the &lt;br /&gt;same value that&lt;br /&gt;was displayed earlier, with the name property for the DocumentType Node. The &lt;br /&gt;Value property for&lt;br /&gt;this element shows null, since tags are devoid of Values. Instead, they have &lt;br /&gt;Attributes. &lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;The attribute Count displays a value of one. At the following Whitespace node, &lt;br /&gt;the Depth&lt;br /&gt;property gets incremented by one. This is the only way to ascertain whether we &lt;br /&gt;are at the root &lt;br /&gt;node or not. We now stumble upon a comment, which has no name. The value &lt;br /&gt;displayed is the value &lt;br /&gt;of the comment. And yet again, the &amp;lt;!-characters are not displayed along with &lt;br /&gt;the value.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;Thereafter, a processing instruction (PI) is encountered. No whitespace is &lt;br /&gt;displayed between the&lt;br /&gt;comment and the PI, since we have not pressed the Enter key. 'Sonal' becomes the &lt;br /&gt;name of the&lt;br /&gt;program that runs 'vijay'. The rest turns into the value property having no &lt;br /&gt;attributes. TextNode&lt;br /&gt;is displayed next because the text 'Hi' is displayed in the XML file. This node &lt;br /&gt;too is not &lt;br /&gt;assigned any name and the value is depicted as 'Hi'. &lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;What follows the text is an Entity Reference. It is assigned the name 'baby' and &lt;br /&gt;is devoid of &lt;br /&gt;the ampersand sign. Its value is null and it does not have any attributes. The &lt;br /&gt;CDATA section is&lt;br /&gt;given the name as null. The value is assigned the content of the CDATA, after &lt;br /&gt;stripping away the&lt;br /&gt;square brackets. &lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;The value of the Depth property is incremented by 1. The Text Node follows the &lt;br /&gt;element aa. This&lt;br /&gt;node does not have any name and it displays the value as 'bb'. In the following &lt;br /&gt;program, we &lt;br /&gt;explore the various attributes.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;a.cs&lt;br&gt;&lt;br /&gt;using System;&lt;br&gt;&lt;br /&gt;using System.IO;&lt;br&gt;&lt;br /&gt;using System.Xml;&lt;br&gt;&lt;br /&gt;public class zzz&lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;public static void Main()&lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;XmlTextReader r;&lt;br&gt;&lt;br /&gt;r = new XmlTextReader(&amp;quot;b.xml&amp;quot;);&lt;br&gt;&lt;br /&gt;r.WhitespaceHandling = WhitespaceHandling.None;&lt;br&gt;&lt;br /&gt;while (r.Read())&lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;Console.Write(&amp;quot;{0} D={1} L={2} &lt;br /&gt;P={3}&amp;quot;,r.NodeType,r.Depth,r.LineNumber,r.LinePosition);&lt;br&gt;&lt;br /&gt;Console.Write(&amp;quot; name={0} value={1} AC={2}&amp;quot;,r.Name,r.Value,r.AttributeCount);&lt;br&gt;&lt;br /&gt;Console.WriteLine();&lt;br&gt;&lt;br /&gt;if (r.HasAttributes)&lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;for ( int i =0; i &amp;lt; r.AttributeCount; i++)&lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;r.MoveToAttribute(i);&lt;br&gt;&lt;br /&gt;System.Console.WriteLine(&amp;quot;Att {0}={1}&amp;quot;,r.Name,r[i]);&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;Output&lt;br&gt;&lt;br /&gt;XmlDeclaration D=0 L=1 P=3 name=xml value=version=&amp;quot;1.0&amp;quot; standalone=&amp;quot;yes&amp;quot; AC=2&lt;br&gt;&lt;br /&gt;Att version=1.0&lt;br&gt;&lt;br /&gt;Att standalone=yes&lt;br&gt;&lt;br /&gt;DocumentType D=0 L=2 P=11 name=vijay value=&amp;lt;!ENTITY baby &amp;quot;No&amp;quot;&amp;gt; AC=1&lt;br&gt;&lt;br /&gt;Att SYSTEM=a.dtd&lt;br&gt;&lt;br /&gt;Element D=0 L=3 P=2 name=vijay value= AC=1&lt;br&gt;&lt;br /&gt;Att aa=no&lt;br&gt;&lt;br /&gt;Comment D=1 L=4 P=5 name= value=comment 2 AC=0&lt;br&gt;&lt;br /&gt;ProcessingInstruction D=1 L=4 P=19 name=sonal value=mukhi=no AC=0&lt;br&gt;&lt;br /&gt;Text D=1 L=4 P=36 name= value=&lt;br&gt;&lt;br /&gt;Hi AC=0&lt;br&gt;&lt;br /&gt;EntityReference D=1 L=5 P=4 name=baby value= AC=0&lt;br&gt;&lt;br /&gt;CDATA D=1 L=6 P=10 name= value=,mukhi&amp;gt; AC=0&lt;br&gt;&lt;br /&gt;Element D=1 L=6 P=21 name=aa value= AC=0&lt;br&gt;&lt;br /&gt;Text D=2 L=6 P=24 name= value=bb AC=0&lt;br&gt;&lt;br /&gt;EndElement D=1 L=6 P=28 name=aa value= AC=0&lt;br&gt;&lt;br /&gt;EndElement D=0 L=7 P=3 name=vijay value= AC=0&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;A property called WhiteSpaceHandling is initialized to None, as a result of &lt;br /&gt;which, the node&lt;br /&gt;Whitespace is not visible in the output. &lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;The XmlTextReader has a member HasAttributes, which returns a True value if the &lt;br /&gt;node has&lt;br /&gt;attributes and False otherwise. Alternatively, we could also have used the &lt;br /&gt;property &lt;br /&gt;AttributeCount to obtain the number of attributes that the node contains.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;If the node has attributes, a 'for statement' is used to display all of them. In &lt;br /&gt;the loop, we &lt;br /&gt;first use the function MoveToAttribute to initially activate the attribute. This &lt;br /&gt;is achieved by&lt;br /&gt;passing the number as a parameter to the function. Bear in mind that the index &lt;br /&gt;starts from Zero &lt;br /&gt;and not One.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;Thereafter, the Name property is used to display the name of the attribute. If &lt;br /&gt;the attribute is &lt;br /&gt;not activated, the Name property displays the name of the node. This explains &lt;br /&gt;the significance &lt;br /&gt;of the MoveToAttribute function. &lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;As you would recall, the XmlTextReader class has an indexer for the attributes, &lt;br /&gt;and like all&lt;br /&gt;indexers, it is zero based, i.e. r[0] accesses the value of the first attribute. &lt;br /&gt;This is how we &lt;br /&gt;display the details of all attributes of the node.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;For the node DOCTYPE, the SYSTEM becomes the name of the attribute and the value &lt;br /&gt;becomes the&lt;br /&gt;name of the DTD file. For an element, the attributes are specified in name-value &lt;br /&gt;pairs.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;a.cs&lt;br&gt;&lt;br /&gt;using System;&lt;br&gt;&lt;br /&gt;using System.IO;&lt;br&gt;&lt;br /&gt;using System.Xml;&lt;br&gt;&lt;br /&gt;public class zzz&lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;public static XmlTextReader r;&lt;br&gt;&lt;br /&gt;public static void Main()&lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;r = new XmlTextReader(&amp;quot;b.xml&amp;quot;);&lt;br&gt;&lt;br /&gt;int declaration=0, pi=0, doc=0, comment=0, element=0, attribute=0, text=0, &lt;br&gt;&lt;br /&gt;whitespace=0,cdata=0,endelement=0,&lt;br&gt;&lt;br /&gt;entityr=0,entitye=0,entity=0,swhitespace=0,notation=0;&lt;br&gt;&lt;br /&gt;while (r.Read())&lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;Console.Write(&amp;quot;{0} D={1} L={2} &lt;br /&gt;P={3}&amp;quot;,r.NodeType,r.Depth,r.LineNumber,r.LinePosition);&lt;br&gt;&lt;br /&gt;Console.Write(&amp;quot; name={0} value={1} AC={2}&amp;quot;,r.Name,r.Value,r.AttributeCount);&lt;br&gt;&lt;br /&gt;Console.WriteLine();&lt;br&gt;&lt;br /&gt;if (r.HasAttributes)&lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;for ( int i =0; i &amp;lt; r.AttributeCount; i++)&lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;r.MoveToAttribute(i);&lt;br&gt;&lt;br /&gt;System.Console.WriteLine(&amp;quot;Att {0}={1}&amp;quot;,r.Name,r[i]);&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;switch (r.NodeType)&lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;case XmlNodeType.XmlDeclaration:&lt;br&gt;&lt;br /&gt;declaration++;&lt;br&gt;&lt;br /&gt;break;&lt;br&gt;&lt;br /&gt;case XmlNodeType.ProcessingInstruction:&lt;br&gt;&lt;br /&gt;pi++;&lt;br&gt;&lt;br /&gt;break;&lt;br&gt;&lt;br /&gt;case XmlNodeType.DocumentType:&lt;br&gt;&lt;br /&gt;doc++;&lt;br&gt;&lt;br /&gt;break;&lt;br&gt;&lt;br /&gt;case XmlNodeType.Comment:&lt;br&gt;&lt;br /&gt;comment++;&lt;br&gt;&lt;br /&gt;break;&lt;br&gt;&lt;br /&gt;case XmlNodeType.Element:&lt;br&gt;&lt;br /&gt;element++;&lt;br&gt;&lt;br /&gt;if (r.HasAttributes)&lt;br&gt;&lt;br /&gt;attribute += r.AttributeCount;&lt;br&gt;&lt;br /&gt;break;&lt;br&gt;&lt;br /&gt;case XmlNodeType.Text:&lt;br&gt;&lt;br /&gt;text++;&lt;br&gt;&lt;br /&gt;break;&lt;br&gt;&lt;br /&gt;case XmlNodeType.CDATA:&lt;br&gt;&lt;br /&gt;cdata++;&lt;br&gt;&lt;br /&gt;break;&lt;br&gt;&lt;br /&gt;case XmlNodeType.EndElement:&lt;br&gt;&lt;br /&gt;endelement++;&lt;br&gt;&lt;br /&gt;break;&lt;br&gt;&lt;br /&gt;case XmlNodeType.EntityReference:&lt;br&gt;&lt;br /&gt;entityr++;&lt;br&gt;&lt;br /&gt;break;&lt;br&gt;&lt;br /&gt;case XmlNodeType.EndEntity:&lt;br&gt;&lt;br /&gt;entitye++;&lt;br&gt;&lt;br /&gt;break;&lt;br&gt;&lt;br /&gt;case XmlNodeType.Notation:&lt;br&gt;&lt;br /&gt;notation++;&lt;br&gt;&lt;br /&gt;break;&lt;br&gt;&lt;br /&gt;case XmlNodeType.Entity:&lt;br&gt;&lt;br /&gt;entity++;&lt;br&gt;&lt;br /&gt;break;&lt;br&gt;&lt;br /&gt;case XmlNodeType.SignificantWhitespace:&lt;br&gt;&lt;br /&gt;swhitespace++;&lt;br&gt;&lt;br /&gt;break;&lt;br&gt;&lt;br /&gt;case XmlNodeType.Whitespace:&lt;br&gt;&lt;br /&gt;whitespace++;&lt;br&gt;&lt;br /&gt;break;&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;Console.WriteLine ();&lt;br&gt;&lt;br /&gt;Console.WriteLine(&amp;quot;XmlDeclaration: {0}&amp;quot;,declaration);&lt;br&gt;&lt;br /&gt;Console.WriteLine(&amp;quot;ProcessingInstruction: {0}&amp;quot;,pi);&lt;br&gt;&lt;br /&gt;Console.WriteLine(&amp;quot;DocumentType: {0}&amp;quot;,doc);&lt;br&gt;&lt;br /&gt;Console.WriteLine(&amp;quot;Comment: {0}&amp;quot;,comment);&lt;br&gt;&lt;br /&gt;Console.WriteLine(&amp;quot;Element: {0}&amp;quot;,element);&lt;br&gt;&lt;br /&gt;Console.WriteLine(&amp;quot;Attribute: {0}&amp;quot;,attribute);&lt;br&gt;&lt;br /&gt;Console.WriteLine(&amp;quot;Text: {0}&amp;quot;,text);&lt;br&gt;&lt;br /&gt;Console.WriteLine(&amp;quot;Cdata: {0}&amp;quot;,cdata);&lt;br&gt;&lt;br /&gt;Console.WriteLine(&amp;quot;EndElement: {0}&amp;quot;,endelement);&lt;br&gt;&lt;br /&gt;Console.WriteLine(&amp;quot;Entity Reference: {0}&amp;quot;,entityr);&lt;br&gt;&lt;br /&gt;Console.WriteLine(&amp;quot;End Entity: {0}&amp;quot;,entitye);&lt;br&gt;&lt;br /&gt;Console.WriteLine(&amp;quot;Entity: {0}&amp;quot;,entity);&lt;br&gt;&lt;br /&gt;Console.WriteLine(&amp;quot;Whitespace: {0}&amp;quot;,whitespace);&lt;br&gt;&lt;br /&gt;Console.WriteLine(&amp;quot;Notation: {0}&amp;quot;,notation);&lt;br&gt;&lt;br /&gt;Console.WriteLine(&amp;quot;Significant Whitespace: {0}&amp;quot;,swhitespace);&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;Output&lt;br&gt;&lt;br /&gt;XmlDeclaration D=0 L=1 P=3 name=xml value=version=&amp;quot;1.0&amp;quot; standalone=&amp;quot;yes&amp;quot; AC=2&lt;br&gt;&lt;br /&gt;Att version=1.0&lt;br&gt;&lt;br /&gt;Att standalone=yes&lt;br&gt;&lt;br /&gt;Whitespace D=0 L=1 P=39 name= value=&lt;br&gt;&lt;br /&gt;AC=0&lt;br&gt;&lt;br /&gt;DocumentType D=0 L=2 P=11 name=vijay value=&amp;lt;!ENTITY baby &amp;quot;No&amp;quot;&amp;gt; AC=1&lt;br&gt;&lt;br /&gt;Att SYSTEM=a.dtd&lt;br&gt;&lt;br /&gt;Whitespace D=0 L=2 P=54 name= value=&lt;br&gt;&lt;br /&gt;AC=0&lt;br&gt;&lt;br /&gt;Element D=0 L=3 P=2 name=vijay value= AC=1&lt;br&gt;&lt;br /&gt;Att aa=no&lt;br&gt;&lt;br /&gt;Whitespace D=1 L=3 P=16 name= value=&lt;br&gt;&lt;br /&gt;AC=0&lt;br&gt;&lt;br /&gt;Comment D=1 L=4 P=5 name= value=comment 2 AC=0&lt;br&gt;&lt;br /&gt;ProcessingInstruction D=1 L=4 P=19 name=sonal value=mukhi=no AC=0&lt;br&gt;&lt;br /&gt;Text D=1 L=4 P=36 name= value=&lt;br&gt;&lt;br /&gt;Hi AC=0&lt;br&gt;&lt;br /&gt;EntityReference D=1 L=5 P=4 name=baby value= AC=0&lt;br&gt;&lt;br /&gt;Whitespace D=1 L=5 P=9 name= value=&lt;br&gt;&lt;br /&gt;AC=0&lt;br&gt;&lt;br /&gt;CDATA D=1 L=6 P=10 name= value=,mukhi&amp;gt; AC=0&lt;br&gt;&lt;br /&gt;Element D=1 L=6 P=21 name=aa value= AC=0&lt;br&gt;&lt;br /&gt;Text D=2 L=6 P=24 name= value=bb AC=0&lt;br&gt;&lt;br /&gt;EndElement D=1 L=6 P=28 name=aa value= AC=0&lt;br&gt;&lt;br /&gt;Whitespace D=1 L=6 P=31 name= value=&lt;br&gt;&lt;br /&gt;AC=0&lt;br&gt;&lt;br /&gt;EndElement D=0 L=7 P=3 name=vijay value= AC=0&lt;br&gt;&lt;br /&gt;Whitespace D=0 L=7 P=9 name= value=&lt;br&gt;&lt;br /&gt;AC=0&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;XmlDeclaration: 0&lt;br&gt;&lt;br /&gt;ProcessingInstruction: 1&lt;br&gt;&lt;br /&gt;DocumentType: 0&lt;br&gt;&lt;br /&gt;Comment: 1&lt;br&gt;&lt;br /&gt;Element: 1&lt;br&gt;&lt;br /&gt;Attribute: 0&lt;br&gt;&lt;br /&gt;Text: 2&lt;br&gt;&lt;br /&gt;Cdata: 1&lt;br&gt;&lt;br /&gt;EndElement: 2&lt;br&gt;&lt;br /&gt;Entity Reference: 1&lt;br&gt;&lt;br /&gt;End Entity: 0&lt;br&gt;&lt;br /&gt;Entity: 0&lt;br&gt;&lt;br /&gt;Whitespace: 6&lt;br&gt;&lt;br /&gt;Notation: 0&lt;br&gt;&lt;br /&gt;Significant Whitespace: 0&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;The above program is a continuation from where we left off in the previous &lt;br /&gt;program. The initial &lt;br&gt;&lt;br /&gt;portion of the code is identical. A colossal case statement is introduced in the &lt;br /&gt;program to &lt;br&gt;&lt;br /&gt;check the NodeType. &lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;For each Node Type, there is a corresponding variable, whose value is &lt;br /&gt;incremented by 1 whenever &lt;br&gt;&lt;br /&gt;the Node Type matches. Then, the values contained in these variables are &lt;br /&gt;displayed. For &lt;br&gt;&lt;br /&gt;inexplicable reasons, the NodeType property does not return the following node &lt;br /&gt;types - Document, &lt;br&gt;&lt;br /&gt;DocumentFragment, Entity, EndEntity, or Notation. &lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;a.cs&lt;br&gt;&lt;br /&gt;using System;&lt;br&gt;&lt;br /&gt;using System.IO;&lt;br&gt;&lt;br /&gt;using System.Xml;&lt;br&gt;&lt;br /&gt;public class zzz&lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;public static void Main()&lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;XmlTextReader r = new XmlTextReader(&amp;quot;b.xml&amp;quot;);&lt;br&gt;&lt;br /&gt;r.WhitespaceHandling = WhitespaceHandling.None;&lt;br&gt;&lt;br /&gt;while (r.Read())&lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;if (r.HasValue)&lt;br&gt;&lt;br /&gt;Console.WriteLine(&amp;quot;{0} {1}={2}&amp;quot;, r.NodeType, r.Name, r.Value);&lt;br&gt;&lt;br /&gt;else&lt;br&gt;&lt;br /&gt;Console.WriteLine(&amp;quot;{0} {1}&amp;quot;, r.NodeType, r.Name);&lt;br&gt;&lt;br /&gt;} &lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;Output&lt;br&gt;&lt;br /&gt;XmlDeclaration xml=version=&amp;quot;1.0&amp;quot; standalone=&amp;quot;yes&amp;quot;&lt;br&gt;&lt;br /&gt;DocumentType vijay=&amp;lt;!ENTITY baby &amp;quot;No&amp;quot;&amp;gt;&lt;br&gt;&lt;br /&gt;Element vijay&lt;br&gt;&lt;br /&gt;Comment =comment 2&lt;br&gt;&lt;br /&gt;ProcessingInstruction sonal=mukhi=no&lt;br&gt;&lt;br /&gt;Text =&lt;br&gt;&lt;br /&gt;Hi&lt;br&gt;&lt;br /&gt;EntityReference baby&lt;br&gt;&lt;br /&gt;CDATA =,mukhi&amp;gt;&lt;br&gt;&lt;br /&gt;Element aa&lt;br&gt;&lt;br /&gt;Text =bb&lt;br&gt;&lt;br /&gt;EndElement aa&lt;br&gt;&lt;br /&gt;EndElement vijay&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;The HasValue property simply identifies whether a Node can contain a value or &lt;br /&gt;not. There are&lt;br /&gt;nine nodes that can possess values. These nodes are Attribute, CDATA, Comment, &lt;br /&gt;DocumentType,&lt;br /&gt;ProcessingInstruction, Significant Whitespace, Whitespace, Text and &lt;br /&gt;XmlDeclaration. All the&lt;br /&gt;above nodes must have a value, but they need not necessarily have a name. &lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;a.cs&lt;br&gt;&lt;br /&gt;using System;&lt;br&gt;&lt;br /&gt;using System.IO;&lt;br&gt;&lt;br /&gt;using System.Xml;&lt;br&gt;&lt;br /&gt;public class zzz&lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;public static void Main()&lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;XmlTextReader r = new XmlTextReader(&amp;quot;b.xml&amp;quot;);&lt;br&gt;&lt;br /&gt;r.MoveToContent();&lt;br&gt;&lt;br /&gt;string s = r[&amp;quot;mukhi&amp;quot;];&lt;br&gt;&lt;br /&gt;Console.WriteLine(s);&lt;br&gt;&lt;br /&gt;s = r.GetAttribute(&amp;quot;sonal&amp;quot;);&lt;br&gt;&lt;br /&gt;Console.WriteLine(s);&lt;br&gt;&lt;br /&gt;s = r[2];&lt;br&gt;&lt;br /&gt;Console.WriteLine(s);&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;b.xml&lt;br&gt;&lt;br /&gt;&amp;lt;vijay mukhi=&amp;quot;no&amp;quot; sonal=&amp;quot;yes&amp;quot; aaa=&amp;quot;bad&amp;quot; /&amp;gt;&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;Output&lt;br&gt;&lt;br /&gt;no&lt;br&gt;&lt;br /&gt;yes&lt;br&gt;&lt;br /&gt;bad&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;The MoveToContent function moves to the first element in the XML file.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;In this program, we display the attributes using different methods. In the first &lt;br /&gt;approach, the &lt;br /&gt;indexer is passed a string, which is the name of the attribute 'mukhi'. It &lt;br /&gt;receives 'no' as the&lt;br /&gt;return value. &lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;In the second approach, the indexer is passed the integer value 2 as a &lt;br /&gt;parameter, to access the &lt;br /&gt;value of the third attribute, which is 'bad'.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;Alternatively, the WriteAttribue function could have been given the string &lt;br /&gt;'sonal' as a&lt;br /&gt;parameter, to return the value of the attribute as 'yes'. Thus, there are &lt;br /&gt;multiple means to &lt;br /&gt;achieving the same objective.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;a.cs&lt;br&gt;&lt;br /&gt;using System;&lt;br&gt;&lt;br /&gt;using System.IO;&lt;br&gt;&lt;br /&gt;using System.Xml;&lt;br&gt;&lt;br /&gt;public class zzz&lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;public static void Main() {&lt;br&gt;&lt;br /&gt;XmlTextReader r = new XmlTextReader(&amp;quot;b.xml&amp;quot;);&lt;br&gt;&lt;br /&gt;r.MoveToContent();&lt;br&gt;&lt;br /&gt;string s ;&lt;br&gt;&lt;br /&gt;s = r.GetAttribute(&amp;quot;aa:bb&amp;quot;);&lt;br&gt;&lt;br /&gt;Console.WriteLine(s);&lt;br&gt;&lt;br /&gt;s = r.GetAttribute(&amp;quot;bb&amp;quot;);&lt;br&gt;&lt;br /&gt;Console.WriteLine(s);&lt;br&gt;&lt;br /&gt;s = r.GetAttribute(&amp;quot;bb&amp;quot;,&amp;quot;sonal:mukhi&amp;quot;);&lt;br&gt;&lt;br /&gt;Console.WriteLine(s);&lt;br&gt;&lt;br /&gt;s = r.GetAttribute(&amp;quot;bb&amp;quot;,&amp;quot;sonal:mukhi&amp;quot;);&lt;br&gt;&lt;br /&gt;Console.WriteLine(s);&lt;br&gt;&lt;br /&gt;s = r.GetAttribute(&amp;quot;bb&amp;quot;,&amp;quot;aa&amp;quot;);&lt;br&gt;&lt;br /&gt;Console.WriteLine(s);&lt;br&gt;&lt;br /&gt;s = r.GetAttribute(&amp;quot;xmlns:aa&amp;quot;);&lt;br&gt;&lt;br /&gt;Console.WriteLine(s);&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;b.xml&lt;br&gt;&lt;br /&gt;&amp;lt;vijay xmlns:aa=&amp;quot;sonal:mukhi&amp;quot; aa:bb=&amp;quot;no&amp;quot; /&amp;gt;&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;Output&lt;br&gt;&lt;br /&gt;no&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;no&lt;br&gt;&lt;br /&gt;no&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;sonal:mukhi&lt;br&gt;&lt;br /&gt;The MoveToContent function is used in this program, instead of the Read &lt;br /&gt;function. In the file&lt;br /&gt;b.xml, we have an attribute bb in the namespace aa. It is initialized to a value &lt;br /&gt;of 'no'. The&lt;br /&gt;namespace aa has a URI, sonal:mukhi, because of the xmlns declaration. Thus, the &lt;br /&gt;full name of&lt;br /&gt;the attribute becomes aa:bb i.e. prefix, followed by the colon, followed by the &lt;br /&gt;actual name. As&lt;br /&gt;a result, specifying aa:bb results in the display of 'no', but only specifying &lt;br /&gt;bb as a parameter&lt;br /&gt;to GetAttribute results in a null value. &lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;The full name of an attribute includes the name of the namespace too. So, we can &lt;br /&gt;use the second &lt;br /&gt;form of the GetAttribute function that has an overload of two parameters, where &lt;br /&gt;the second&lt;br /&gt;parameter is the name of the URI and not the namespace. Hence, it is acceptable &lt;br /&gt;to call the &lt;br /&gt;function with the URI sonal:mukhi, but if we use the namespace aa, no output &lt;br /&gt;will be produced.&lt;br&gt;&lt;br /&gt;The last GetAttribute utilizes the full name xmlns:aa to retrieve the URI for &lt;br /&gt;the element. Thus,&lt;br /&gt;we can use this variant of the GetAttribute function with the URI instead of the &lt;br /&gt;namespace:name.&lt;br&gt;&lt;br /&gt;a.cs&lt;br&gt;&lt;br /&gt;using System;&lt;br&gt;&lt;br /&gt;using System.IO;&lt;br&gt;&lt;br /&gt;using System.Xml;&lt;br&gt;&lt;br /&gt;public class zzz {&lt;br&gt;&lt;br /&gt;public static void Main() {&lt;br&gt;&lt;br /&gt;XmlTextReader r = new XmlTextReader(&amp;quot;b.xml&amp;quot;);&lt;br&gt;&lt;br /&gt;r.WhitespaceHandling=WhitespaceHandling.None;&lt;br&gt;&lt;br /&gt;r.MoveToContent();&lt;br&gt;&lt;br /&gt;r.MoveToAttribute(&amp;quot;cc&amp;quot;);&lt;br&gt;&lt;br /&gt;Console.WriteLine(r.Name + &amp;quot; &amp;quot; + r.Value);&lt;br&gt;&lt;br /&gt;Console.WriteLine(r.ReadAttributeValue());&lt;br&gt;&lt;br /&gt;Console.WriteLine(r.Name + &amp;quot; &amp;quot; + r.Value);&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;b.xml&lt;br&gt;&lt;br /&gt;&amp;lt;vijay aa=&amp;quot;hi&amp;quot; bb=&amp;quot;bye&amp;quot; cc=&amp;quot;no&amp;quot; /&amp;gt;&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;Output&lt;br&gt;&lt;br /&gt;cc no&lt;br&gt;&lt;br /&gt;True&lt;br&gt;&lt;br /&gt;No&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;In this example, we directly focus on the attribute that we are interested in, &lt;br /&gt;i.e. cc. The name &lt;br /&gt;and value properties in XMLTextReader display 'cc' and 'no' respectively. As &lt;br /&gt;there are numerous&lt;br /&gt;attributes of the node remaining to be read, the ReadAttribute function returns &lt;br /&gt;True. This&lt;br /&gt;function is normally used to read text or entity reference nodes that constitute &lt;br /&gt;the value of &lt;br /&gt;the attribute. &lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;The Name property of the XmlTextReader however becomes null after the function&lt;br /&gt;&lt;br&gt;&lt;br /&gt;ReadAttributeValue is called.&lt;/p&gt;</description><thr:total xmlns:thr="http://purl.org/syndication/thread/1.0">0</thr:total></item><item><title>C Language  Help, C Language Tutorials, C Language Programming, C Language Tricks  { The C# Language }</title><link>http://swebsolution.blogspot.com/2007/12/c-language-printing-c-language.html</link><author>noreply@blogger.com (Unknown)</author><pubDate>Tue, 25 Dec 2007 23:44:00 -0800</pubDate><guid isPermaLink="false">tag:blogger.com,1999:blog-7227425366482465228.post-5034109436445304718</guid><description>&lt;p align="justify"&gt;&lt;b&gt;C Language  Help, C Language Tutorials, C Language Programming, C Language Tricks &lt;/b&gt;&lt;br&gt;Printing&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;One facility that every programmer in the world would want to provide through &lt;br /&gt;his program is that of printing. The .NET world has bestowed about ten important &lt;br /&gt;printer classes that ease our job of printing text and graphics to the printer. &lt;br /&gt;We shall grapple with some small programs to grasp the concepts of printing and &lt;br /&gt;conclude this chapter with a small word processor program that is provided along &lt;br /&gt;with the .NET samples. You should create a text file named a.txt in the current &lt;br /&gt;subdirectory, and thereafter, insert the following three lines:&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;a.txt&lt;br&gt;&lt;br /&gt;hi, how are you&lt;br&gt;&lt;br /&gt;bye, take care&lt;br&gt;&lt;br /&gt;end&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;a.cs&lt;br&gt;&lt;br /&gt;using System;&lt;br&gt;&lt;br /&gt;using System.Drawing;&lt;br&gt;&lt;br /&gt;using System.Drawing.Printing;&lt;br&gt;&lt;br /&gt;using System.IO;&lt;br&gt;&lt;br /&gt;public class zzz {&lt;br&gt;&lt;br /&gt;Font f;&lt;br&gt;&lt;br /&gt;StreamReader sr;&lt;br&gt;&lt;br /&gt;public void abc()&lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;sr = new StreamReader (&amp;quot;a.txt&amp;quot;);&lt;br&gt;&lt;br /&gt;f= new Font(&amp;quot;Courier New&amp;quot;, 14);&lt;br&gt;&lt;br /&gt;PrintDocument pd = new PrintDocument(); &lt;br&gt;&lt;br /&gt;pd.PrintPage += new PrintPageEventHandler(pqr);&lt;br&gt;&lt;br /&gt;pd.Print();&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;void pqr(object o, PrintPageEventArgs e) &lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;float lpp = e.MarginBounds.Height / f.GetHeight(e.Graphics) ;&lt;br&gt;&lt;br /&gt;int c = 0 ;&lt;br&gt;&lt;br /&gt;String s=null;&lt;br&gt;&lt;br /&gt;while (c &amp;lt; lpp &amp;amp;&amp;amp; ((s=sr.ReadLine()) != null)) &lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;float y = e.MarginBounds.Top + (c * f.GetHeight(e.Graphics));&lt;br&gt;&lt;br /&gt;e.Graphics.DrawString (s, f, Brushes.Black, e.MarginBounds.Left, y);&lt;br&gt;&lt;br /&gt;c++;&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;if (s != null)&lt;br&gt;&lt;br /&gt;e.HasMorePages = true ;&lt;br&gt;&lt;br /&gt;else&lt;br&gt;&lt;br /&gt;e.HasMorePages = false ;&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;public static void Main() {&lt;br&gt;&lt;br /&gt;zzz a = new zzz();&lt;br&gt;&lt;br /&gt;a.abc();&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;} &lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;The above program prints the contents of the text file a.txt to the default &lt;br /&gt;printer. In all our programs in this chapter, we shall create an object that is &lt;br /&gt;an instance of class zzz and call function abc off it. The code within this &lt;br /&gt;function will execute the actual task of printing.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;We first create an object, sr, which is an instance of the class StreamReader. &lt;br /&gt;The constructor of this class is furnished with the name of the file that we &lt;br /&gt;want to print, i.e. a.txt. &lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;The StreamReader class is derived from the class TextReader, which reads &lt;br /&gt;characters from a stream of bytes that have been encoded in a particular format. &lt;br /&gt;The TextReader class belongs to the System.IO namespace. The Stream class too &lt;br /&gt;forms a part of the same genre. This class is designed for reading byte streams.&lt;br /&gt;&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;The StreamReader class is optimised for reading lines of data from an ASCII or a &lt;br /&gt;text file. By default, the encoding used is UTF-8, but it can be changed to any &lt;br /&gt;other encoding format, if required. The UTF-8 encoding can comprehend Unicode &lt;br /&gt;characters. The StreamReader class, by default, is not thread safe, and neither &lt;br /&gt;does it belong to the Printing classes in the .NET family. The class is used in &lt;br /&gt;this program to read the file, one line at a time.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;We want the printer to print our text file in a specific font. So, we create a &lt;br /&gt;Font object f, whose constructor is passed the Font Name 'Courier New' along &lt;br /&gt;with the size of '14 points'. We have plenty of choice of fonts from the &lt;br /&gt;innumerable fonts available. For the neophytes of the publishing world, it would &lt;br /&gt;be a revelation that 72 points makes an inch. If we specify a font name such as &lt;br /&gt;'Vijay Mukhi', which does not exist, the printer uses the default font instead, &lt;br /&gt;which in this case, is Microsoft Sans Serif. Thus, the printer always receives a &lt;br /&gt;valid font type to work with.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;We create one more object called pd, in our program, which is an instance of &lt;br /&gt;class PrintDocument. This class is the nucleus of all printing activities in the &lt;br /&gt;.NET world. It has an event called PrintPage, which requires an instance of a &lt;br /&gt;delegate PrintPageEventHandler. The delegate represents a method called pqr, &lt;br /&gt;which handles the PrintPage event. This event is generated by the Print &lt;br /&gt;function. &lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;Thus, when we call the Print function, it in turn, calls the pqr function. This &lt;br /&gt;function is passed two parameters. The second parameter is a PrintPageEventArgs &lt;br /&gt;class, which carries data that is useful for printing. The PrintPageEventArgs &lt;br /&gt;class is derived from class EventArgs. The pqr function is not called &lt;br /&gt;explicitly, since this would entail creation of an object like &lt;br /&gt;PrintPageEventArgs.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;The Print function is a blocking function. This implies that not a single line &lt;br /&gt;of code will be executed after the Print function has been called, until the pqr &lt;br /&gt;function completes execution. If the pqr function goes into an indefinite loop, &lt;br /&gt;the code succeeding the Print function will never get executed.&lt;br&gt;&lt;br /&gt;As mentioned earlier, the second parameter e of data type PrintPageEventArgs &lt;br /&gt;contains the data that is to be printed. The member MarginBounds, which is of &lt;br /&gt;data type Rectangle, specifies the rectangular portion of the page, which falls &lt;br /&gt;within the margins. In our case, the various values of the Rectangle structure &lt;br /&gt;are as follows:&lt;br&gt;&lt;br /&gt;Height = 900, Width = 650, Left = 100, Right = 750&lt;br&gt;&lt;br /&gt;Top = 100, Bottom = 1000, X = 100, Y = 100. &lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;Before we initiate printing, we need to identify the number of lines that can be &lt;br /&gt;printed on a single page. To estimate this, we use the following calculation:&lt;br&gt;&lt;br /&gt;• Height of our page in pixels divided by the height of the font in pixels. &lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;In this example, it works out to 900 / 22.02691 = 40.85. This value is stored in &lt;br /&gt;the variable lpp, which signifies 'lines per page'. &lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;We then use another variable c, which has an initial value of zero. It is &lt;br /&gt;incremented in the while loop, once for every line that is printed. As soon as &lt;br /&gt;the value of c exceeds that of lpp, the program quits out of the 'while' loop.&lt;br /&gt;&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;The ReadLine function of the StreamReader class fetches a new line from the file &lt;br /&gt;a.txt and stores it within a string variable s. If there are no more lines to be &lt;br /&gt;read from the file, this function returns a null. Thus, the while loop &lt;br /&gt;terminates when the number of lines printed exceed the total number of lines &lt;br /&gt;that can fit on a page or when there are no more lines to be read from the file.&lt;br /&gt;&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;A check must be performed to verify whether we have printed the maximum possible &lt;br /&gt;lines that can fit on a page, before we read the next line from the file. This &lt;br /&gt;is to forestall reading a line from the file and thereafter, realizing that it &lt;br /&gt;cannot be printed on the page due to lack of space. To perform this check, we &lt;br /&gt;determine the y position of each line on the page. All text is printed below the &lt;br /&gt;Top margin. So to calculate the y position of a line, we use the following &lt;br /&gt;calculation:&lt;br&gt;&lt;br /&gt;(Height of the font used MULTIPLIED BY the number of lines printed so far) PLUS &lt;br /&gt;value of the Top margin. &lt;br&gt;&lt;br /&gt;In this case, &lt;br&gt;&lt;br /&gt;The value of the top margin is 100&lt;br&gt;&lt;br /&gt;The height of the font is 22.02691&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;In the case of the first line to be printed, the number of lines printed so far &lt;br /&gt;is 0.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;So, the value of the y co-ordinate for the first line is&lt;br&gt;&lt;br /&gt;=100+(22.02691 X 0) = 100. &lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;The value of the y co-ordinate for the second line is &lt;br&gt;&lt;br /&gt;=100+(22.02691 X 1) = 122.0269. &lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;The value of the y co-ordinate for the third line is&lt;br&gt;&lt;br /&gt;=100+(22.02691 X 2) = 144.0538. &lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;We accessed these values using the WriteLine function. &lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;The Graphics member in class PrintPageEventArgs is employed to write to the &lt;br /&gt;printer. This member contains the function DrawString. This function, which we &lt;br /&gt;had used earlier to write to the screen, is now utilized to write to the &lt;br /&gt;printer. &lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;The DrawString function accepts 5 parameters:&lt;br&gt;&lt;br /&gt;1) A string, s&lt;br&gt;&lt;br /&gt;2) A font, f&lt;br&gt;&lt;br /&gt;3) A color or brush, Black&lt;br&gt;&lt;br /&gt;4) The left margin for the x coordinate, MarginBounds.Left&lt;br&gt;&lt;br /&gt;5) The y co-ordinate&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;As you may have noticed, the same DrawString function is used for printing to &lt;br /&gt;the printer and writing to the screen. Abstraction, therefore, enables us &lt;br /&gt;exploit and reuse concepts that we have learnt earlier.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;The while loop ends when any of the following two conditions is satisfied:&lt;br&gt;&lt;br /&gt;• The number of lines printed exceeds the lines per page i.e. when we need to &lt;br /&gt;print on the next page. &lt;br&gt;&lt;br /&gt;• The file has no more lines to print. &lt;br&gt;&lt;br /&gt;To figure out which of the above conditions has ended the 'while' loop, we &lt;br /&gt;verify the value contained in string s. If the value is null, it signifies that &lt;br /&gt;there are no more lines to print.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;As soon as we exceed the maximum number of lines that can be printed on a page, &lt;br /&gt;and there are lines remaining to be printed, there has to be a mechanism to &lt;br /&gt;inform the PrintDocument class that the function pqr needs to be called again, &lt;br /&gt;to print the next page. To accomplish this, the HasMorePages member of the &lt;br /&gt;PrintPageEventArgs object e is either set to true or false. If it is set to &lt;br /&gt;true, the function pqr gets called again.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;a.cs&lt;br&gt;&lt;br /&gt;using System;&lt;br&gt;&lt;br /&gt;using System.Drawing;&lt;br&gt;&lt;br /&gt;using System.Drawing.Printing;&lt;br&gt;&lt;br /&gt;using System.IO;&lt;br&gt;&lt;br /&gt;public class zzz &lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;Font f;&lt;br&gt;&lt;br /&gt;public void abc()&lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;f= new Font(&amp;quot;Courier New&amp;quot;, 14);&lt;br&gt;&lt;br /&gt;PrintDocument pd = new PrintDocument(); &lt;br&gt;&lt;br /&gt;pd.PrintPage += new PrintPageEventHandler(pqr);&lt;br&gt;&lt;br /&gt;pd.PrintPage += new PrintPageEventHandler(xyz);&lt;br&gt;&lt;br /&gt;pd.Print();&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;void pqr(object o, PrintPageEventArgs e) &lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;e.Graphics.DrawString (&amp;quot;Vijay Mukhi&amp;quot;, f, Brushes.Black, 100, 200);&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;void xyz(object o, PrintPageEventArgs e) &lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;e.Graphics.DrawString (&amp;quot;Sonal Mukhi&amp;quot;, f, Brushes.Black , 200, 300);&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;public static void Main() &lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;zzz a = new zzz();&lt;br&gt;&lt;br /&gt;a.abc();&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;Through this example, we once again demonstrate the power of events and &lt;br /&gt;delegates. We attach two methods, pqr and xyz, with the printing event. The &lt;br /&gt;first function pqr prints 'Vijay Mukhi' at X=100 and Y=200 and then, the method &lt;br /&gt;xyz prints 'Sonal Mukhi' at X=200 and Y=300. The HasMorePages property is &lt;br /&gt;initialized to false by the program, even though it has the value of false by &lt;br /&gt;default. Both these lines get printed on the same page.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;Each time we set HasMorePages to true, the .NET framework sends a 'form feed' &lt;br /&gt;signal to the printer that sets up a new page for printing. The only mechanism &lt;br /&gt;of printing text to a printer is by means of the Graphics object.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;If the 'font' and the 'Brushes' parameters are set to null, a run-time exception &lt;br /&gt;is generated. Hence, they must be explicitly specified. Unlike a dot matrix &lt;br /&gt;printer that prints one line at a time, a laser printer first creates an entire &lt;br /&gt;page in memory and then sends it for printing. Thus, in the function pqr, if we &lt;br /&gt;change the Y coordinate from 200 to 400, both lines get printed on the same &lt;br /&gt;page, since the page first gets created in the memory, and then it is sent to &lt;br /&gt;the printer.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;The PrintDocument class creates the PrintPageEventArgs object, since that is the &lt;br /&gt;sole mechanism of activating the printer. Then, it calls all the methods &lt;br /&gt;registered through the delegate.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;a.cs&lt;br&gt;&lt;br /&gt;using System;&lt;br&gt;&lt;br /&gt;using System.Drawing;&lt;br&gt;&lt;br /&gt;using System.Drawing.Printing;&lt;br&gt;&lt;br /&gt;using System.IO;&lt;br&gt;&lt;br /&gt;public class zzz &lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;public void abc()&lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;yyy y = new yyy(); &lt;br&gt;&lt;br /&gt;y.PrintPage += new PrintPageEventHandler(pqr);&lt;br&gt;&lt;br /&gt;y.Print();&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;void pqr(object o, PrintPageEventArgs e) &lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;System.Console.WriteLine(&amp;quot;pqr&amp;quot;);&lt;br&gt;&lt;br /&gt;e.Graphics.DrawString (&amp;quot;Sonal Mukhi&amp;quot;, new Font(&amp;quot;Courier New&amp;quot;,10), Brushes.Black, &lt;br /&gt;100, 200);&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;public static void Main() &lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;zzz a = new zzz();&lt;br&gt;&lt;br /&gt;a.abc();&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;public class yyy : PrintDocument&lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;Font f;&lt;br&gt;&lt;br /&gt;protected override void OnQueryPageSettings(QueryPageSettingsEventArgs e)&lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;base.OnQueryPageSettings(e) ;&lt;br&gt;&lt;br /&gt;System.Console.WriteLine(&amp;quot;OnQueryPageSettings&amp;quot;);&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;protected override void OnBeginPrint(PrintEventArgs e) &lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;base.OnBeginPrint(e) ;&lt;br&gt;&lt;br /&gt;System.Console.WriteLine(&amp;quot;OnBeginPrint&amp;quot;);&lt;br&gt;&lt;br /&gt;f = new Font(&amp;quot;Courier New&amp;quot;,14);&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;protected override void OnEndPrint(PrintEventArgs e) &lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;base.OnEndPrint(e) ;&lt;br&gt;&lt;br /&gt;System.Console.WriteLine(&amp;quot;OnEndPrint&amp;quot;);&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;protected override void OnPrintPage(PrintPageEventArgs e) &lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;base.OnPrintPage(e) ;&lt;br&gt;&lt;br /&gt;System.Console.WriteLine(&amp;quot;OnPrintPage&amp;quot;);&lt;br&gt;&lt;br /&gt;e.Graphics.DrawString (&amp;quot;Vijay Mukhi&amp;quot;, f, Brushes.Black, 100, 400);&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;Output&lt;br&gt;&lt;br /&gt;OnBeginPrint&lt;br&gt;&lt;br /&gt;OnQueryPageSettings&lt;br&gt;&lt;br /&gt;pqr&lt;br&gt;&lt;br /&gt;OnPrintPage&lt;br&gt;&lt;br /&gt;OnEndPrint&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;In the above example, class yyy is derived from PrintDocument. Thus, all code &lt;br /&gt;embodied in the PrintDocument class is available to yyy. This also implies that &lt;br /&gt;any code in the class can be overridden. The Print method of the PrintDocument &lt;br /&gt;class had earlier called the methods registered with the delegate, whereas now, &lt;br /&gt;it calls a series of methods from the PrintDocument class. In the situation &lt;br /&gt;where we want to call our methods with the same name as in class yyy, the &lt;br /&gt;keyword 'override' has to be used.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;The output confirms that the first function to be called is OnBeginPrint. This &lt;br /&gt;function is akin to an initialization function. All code that is to be executed &lt;br /&gt;only once must be placed in this function. It is a sage programming practice to &lt;br /&gt;always call the original function from the base class. Thus, we use the keyword &lt;br /&gt;base to call the original OnBeginPrint from the class PrintDocument. &lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;Superior programming style decrees that, initially, the base functions should be &lt;br /&gt;called. It is not mandatory to do so, since even if this is not done, the class &lt;br /&gt;still behaves in a similar manner. The documentation too is silent on this &lt;br /&gt;issue, but we consider it prudent to do so and advise you also to call the &lt;br /&gt;original function first.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;The parameter PrintEventArgs passed to the function has no significant role to &lt;br /&gt;play in this release. The documentation clearly states that this PrintEventArgs &lt;br /&gt;has been designed for use in a future release. Thus, it cannot be put to any &lt;br /&gt;consequential use at present. We create a Font object in this function, since it &lt;br /&gt;is a one-time activity.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;The second function that gets called is OnQueryPageSettings, which will be &lt;br /&gt;elucidated in the next example. Following it, function pqr gets called, which &lt;br /&gt;facilitates printing. Then, function OnPrintPage gets called with the same &lt;br /&gt;parameters as that of the function pqr. If we so desire, we can print in the &lt;br /&gt;delegate method. Other than the delegate method being called first, there is no &lt;br /&gt;apparent difference. &lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;Finally, the function OnEndPrint is called, in which, we are presented with the &lt;br /&gt;opportunity to clean up any resources created in the method OnBeginPrint. &lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;a.cs&lt;br&gt;&lt;br /&gt;using System;&lt;br&gt;&lt;br /&gt;using System.Drawing;&lt;br&gt;&lt;br /&gt;using System.Drawing.Printing;&lt;br&gt;&lt;br /&gt;using System.IO;&lt;br&gt;&lt;br /&gt;public class zzz &lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;public void abc()&lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;yyy y = new yyy(); &lt;br&gt;&lt;br /&gt;y.PrintPage += new PrintPageEventHandler(pqr);&lt;br&gt;&lt;br /&gt;y.Print();&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;void pqr(object o, PrintPageEventArgs e) &lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;e.Graphics.DrawString (&amp;quot;Sonal Mukhi&amp;quot;, new Font(&amp;quot;Courier New&amp;quot;,10), Brushes.Black, &lt;br /&gt;100, 200);&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;public static void Main() &lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;zzz a = new zzz();&lt;br&gt;&lt;br /&gt;a.abc();&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;public class yyy : PrintDocument&lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;Font f;&lt;br&gt;&lt;br /&gt;protected override void OnQueryPageSettings (QueryPageSettingsEventArgs e)&lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;base.OnQueryPageSettings(e) ;&lt;br&gt;&lt;br /&gt;e.PageSettings.Landscape = true;&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;protected override void OnBeginPrint(PrintEventArgs e) &lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;base.OnBeginPrint(e) ;&lt;br&gt;&lt;br /&gt;f = new Font(&amp;quot;Courier New&amp;quot;,14);&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;protected override void OnPrintPage(PrintPageEventArgs e) &lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;base.OnPrintPage(e) ;&lt;br&gt;&lt;br /&gt;e.Graphics.DrawString (&amp;quot;Vijay Mukhi&amp;quot;, f, Brushes.Black, 100, 400);&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;Each time we use the printer, we may be desirous of customizing the print &lt;br /&gt;settings for each page. To enable this, the framework calls the function &lt;br /&gt;OnQueryPageSettings before printing any page. In this function, we can change &lt;br /&gt;the page settings using the parameter e, which is of type &lt;br /&gt;QueryPageSettingsEventArgs. This class is derived from PrintEventArgs class. &lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;One of the members in QueryPageSettingEventArgs is PageSettings, which contains &lt;br /&gt;a large number of properties that control the printer settings. Here, we have &lt;br /&gt;only used the Landscape property and assigned it a boolean value of true. Had we &lt;br /&gt;assigned it a value of false, the property would have been set to Portrait mode, &lt;br /&gt;which is usually the default mode. &lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;Other properties that can be modified are: the paper tray i.e. the source from &lt;br /&gt;where the paper is fed to the printer, the printer resolution, the page margins, &lt;br /&gt;the color etc. Any property of a printer that can be set manually can now be &lt;br /&gt;done using this function. &lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;a.cs&lt;br&gt;&lt;br /&gt;using System;&lt;br&gt;&lt;br /&gt;using System.Drawing;&lt;br&gt;&lt;br /&gt;using System.Drawing.Printing;&lt;br&gt;&lt;br /&gt;using System.IO;&lt;br&gt;&lt;br /&gt;public class zzz &lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;public void abc()&lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;yyy y = new yyy(); &lt;br&gt;&lt;br /&gt;y.PrintPage += new PrintPageEventHandler(pqr);&lt;br&gt;&lt;br /&gt;y.Print();&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;void pqr(object o, PrintPageEventArgs e) &lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;e.Graphics.DrawString (&amp;quot;Sonal Mukhi&amp;quot;, new Font(&amp;quot;Courier New&amp;quot;,10), Brushes.Black, &lt;br /&gt;100, 200);&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;public static void Main() &lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;zzz a = new zzz();&lt;br&gt;&lt;br /&gt;a.abc();&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;public class yyy : PrintDocument&lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;Font f;&lt;br&gt;&lt;br /&gt;protected override void OnPrintPage(PrintPageEventArgs e) &lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;base.OnPrintPage(e) ;&lt;br&gt;&lt;br /&gt;e.Cancel = true;&lt;br&gt;&lt;br /&gt;f = new Font(&amp;quot;Arial&amp;quot;,13);&lt;br&gt;&lt;br /&gt;e.Graphics.DrawString (&amp;quot;Vijay Mukhi&amp;quot;, f, Brushes.Black, 100, 400);&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;In this program, the Cancel property of PrintPageEventArgs is set to true, &lt;br /&gt;resulting in cancellation of the print job. The printing that was to be carried &lt;br /&gt;out by the delegate function also gets cancelled. All print jobs are aborted. &lt;br /&gt;The PageSettings object used in the earlier program is also a property of the &lt;br /&gt;PrintPageEventArgs class, and thus, can be used in this function too.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;a.cs&lt;br&gt;&lt;br /&gt;using System;&lt;br&gt;&lt;br /&gt;using System.Windows.Forms;&lt;br&gt;&lt;br /&gt;using System.Drawing;&lt;br&gt;&lt;br /&gt;using System.Drawing.Printing;&lt;br&gt;&lt;br /&gt;using System.IO;&lt;br&gt;&lt;br /&gt;public class zzz &lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;public void abc()&lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;PrintDocument y = new PrintDocument (); &lt;br&gt;&lt;br /&gt;y.PrintPage += new PrintPageEventHandler(pqr);&lt;br&gt;&lt;br /&gt;PrintDialog d = new PrintDialog() ;&lt;br&gt;&lt;br /&gt;d.Document = y;&lt;br&gt;&lt;br /&gt;DialogResult r = d.ShowDialog();&lt;br&gt;&lt;br /&gt;if (r == DialogResult.OK) &lt;br&gt;&lt;br /&gt;y.Print();&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;void pqr(object o, PrintPageEventArgs e) &lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;e.Graphics.DrawString (&amp;quot;Sonal Mukhi&amp;quot;, new Font(&amp;quot;Courier New&amp;quot;,10), Brushes.Black, &lt;br /&gt;100, 200);&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;public static void Main() &lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;zzz a = new zzz();&lt;br&gt;&lt;br /&gt;a.abc();&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;In this example, we would like to display a dialog box to the user where he can &lt;br /&gt;change the printing options. Therefore, firstly we create an object d as an &lt;br /&gt;instance of class PrintDialog. Creating a PrintDialog object assigns default &lt;br /&gt;values to the following eight properties: &lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;• AllowSomePages is set to False, disabling the Pages option button. &lt;br&gt;&lt;br /&gt;• AllowSelection is set to False, disabling the Selection option button. &lt;br&gt;&lt;br /&gt;• AllowPrintToFile is set to True, enabling Print to File check box. &lt;br&gt;&lt;br /&gt;• Document, which is a reference to the PrintDocument object, is set to null.&lt;br /&gt;&lt;br&gt;&lt;br /&gt;• PrinterSettings is assigned the value of False, disabling the dialog box.&lt;br&gt;&lt;br /&gt;• PrintToFile decides whether the printer would print to disk or to the printer. &lt;br /&gt;If the value is null, it prints to the printer. &lt;br&gt;&lt;br /&gt;• ShowHelp is set to False, thus disabling the help button.&lt;br&gt;&lt;br /&gt;• ShowNetwork is set to True, thus enabling the network button. &lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;We can modify any of these values to befit our requirements. We can also select &lt;br /&gt;specific parts of the document to print.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;Screen 6.1&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;When we run the program, the normal Windows Printer Dialog box is displayed, &lt;br /&gt;allowing us to modify any of the properties. The ShowDialog function facilitates &lt;br /&gt;the display of the dialog box. This function waits until one of two the buttons, &lt;br /&gt;OK or Cancel, is clicked.The value returned on clicking one of these two buttons &lt;br /&gt;is stored in the DialogResult object r. &lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;If the OK button is selected, it will start the printing, whereas if the Cancel &lt;br /&gt;button is selected, it will cancel or abort the printing. When the OK button is &lt;br /&gt;selected, the function ShowDialog returns an enum DialogResult, with a value OK. &lt;br /&gt;The above enum can have as many as 8 different values, depending upon the type &lt;br /&gt;of buttons available in the Dialog Box. Thereafter, the Print function of the &lt;br /&gt;PrintDialog class is called. &lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;One of the final actions of the function ShowDialog is to initialize the &lt;br /&gt;Document property supplied by PrintDocument object, i.e. y, to the printer &lt;br /&gt;settings chosen in the Dialog box. &lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;a.cs&lt;br&gt;&lt;br /&gt;using System;&lt;br&gt;&lt;br /&gt;using System.Windows.Forms;&lt;br&gt;&lt;br /&gt;using System.Drawing;&lt;br&gt;&lt;br /&gt;using System.Drawing.Printing;&lt;br&gt;&lt;br /&gt;using System.IO;&lt;br&gt;&lt;br /&gt;public class zzz &lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;public void abc()&lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;PrintDocument y = new PrintDocument (); &lt;br&gt;&lt;br /&gt;y.PrintPage += new PrintPageEventHandler(pqr);&lt;br&gt;&lt;br /&gt;PageSetupDialog p = new PageSetupDialog () ;&lt;br&gt;&lt;br /&gt;PageSettings s = new PageSettings();&lt;br&gt;&lt;br /&gt;p.PageSettings = s ;&lt;br&gt;&lt;br /&gt;p.ShowDialog ();&lt;br&gt;&lt;br /&gt;y.DefaultPageSettings = s; &lt;br&gt;&lt;br /&gt;y.Print();&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;void pqr(object o, PrintPageEventArgs e) &lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;e.Graphics.DrawString (&amp;quot;Sonal Mukhi&amp;quot;, new Font(&amp;quot;Courier New&amp;quot;,10), Brushes.Black, &lt;br /&gt;100, 200);&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;public static void Main() &lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;zzz a = new zzz();&lt;br&gt;&lt;br /&gt;a.abc();&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;In the above example we usher in a new class, PageSetupDialog, which allows the &lt;br /&gt;user to change page settings such as the margins and the paper orientation. When &lt;br /&gt;we create a PageSetupDialog object, properties in the object get initialized to &lt;br /&gt;their default values. &lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;Some of the properties are : &lt;br&gt;&lt;br /&gt;• AllowMargins: set to true, enables us to change the margins settings.&lt;br&gt;&lt;br /&gt;• AllowOrentation: set to true, enables the Portrait/Landscape radio buttons.&lt;br&gt;&lt;br /&gt;• AllowPaper: set to true, so that we can choose the paper size and source.&lt;br&gt;&lt;br /&gt;• AllowPrinter: set to true, enables the printer button.&lt;br&gt;&lt;br /&gt;• MinMargins: set to null, so that the default margins of 1 inch are displayed.&lt;br&gt;&lt;br /&gt;• PageSettings: set to null.&lt;br&gt;&lt;br /&gt;• PrinterSettings: set to null.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;It is mandatory to initialise the PageSettings property to an actual object &lt;br /&gt;containing the settings selected by the user. Finally the function ShowDialog &lt;br /&gt;initializes the object s. &lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;Screen 6.2&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;Then, the property DefaultPageSettings of the PrintDocument class is initialised &lt;br /&gt;to the PageSettings object s, so that the printing framework uses the settings &lt;br /&gt;stored in this property. Thus, the two dialog boxes have their own unique &lt;br /&gt;purposes, and each of them fills up an object that is used by the PrintDocument &lt;br /&gt;class. In the Windows world, Dialog boxes are used to fill up &lt;br /&gt;properties/variables in an object. &lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;In the above two programs, the function pqr does not receive any values from the &lt;br /&gt;dialog boxes.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;a.cs&lt;br&gt;&lt;br /&gt;using System;&lt;br&gt;&lt;br /&gt;using System.Windows.Forms;&lt;br&gt;&lt;br /&gt;using System.Drawing;&lt;br&gt;&lt;br /&gt;using System.Drawing.Printing;&lt;br&gt;&lt;br /&gt;using System.IO;&lt;br&gt;&lt;br /&gt;public class zzz {&lt;br&gt;&lt;br /&gt;public void abc()&lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;PrintDocument pd = new PrintDocument(); &lt;br&gt;&lt;br /&gt;pd.PrintPage += new PrintPageEventHandler(pqr);&lt;br&gt;&lt;br /&gt;PrintPreviewDialog dlg = new PrintPreviewDialog() ;&lt;br&gt;&lt;br /&gt;dlg.Document = pd;&lt;br&gt;&lt;br /&gt;dlg.ShowDialog();&lt;br&gt;&lt;br /&gt;pd.Print();&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;void pqr(object o, PrintPageEventArgs e) &lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;e.Graphics.DrawString (&amp;quot;Sonal Mukhi&amp;quot;, new Font(&amp;quot;Courier New&amp;quot;,10), Brushes.Black, &lt;br /&gt;100, 200);&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;public static void Main() {&lt;br&gt;&lt;br /&gt;zzz a = new zzz();&lt;br&gt;&lt;br /&gt;a.abc();&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;Screen 6.3&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;Before we buy goods, we often like to preview them. The same analogy is true &lt;br /&gt;when we want to print a document. The class PrintPreviewDialog enables us to &lt;br /&gt;preview a document before printing it. The only property that is to be &lt;br /&gt;initialized here is Document property. This is mandatory since there is no other &lt;br /&gt;mechanism by which the Dialog box can receive the contents of the page. &lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;We can safely assume that the ShowDialog function calls Print, which figures out &lt;br /&gt;the text to be printed on a page. It also has the ability to display the content &lt;br /&gt;in different magnification percentages and in multiple pages at a time. The &lt;br /&gt;Dialog contains a PrintPreviewControl, which has several members that can be &lt;br /&gt;toyed around with, in order to transform the appearance.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;a.cs&lt;br&gt;&lt;br /&gt;using System;&lt;br&gt;&lt;br /&gt;using System.Windows.Forms;&lt;br&gt;&lt;br /&gt;using System.Drawing;&lt;br&gt;&lt;br /&gt;using System.Drawing.Printing;&lt;br&gt;&lt;br /&gt;using System.IO;&lt;br&gt;&lt;br /&gt;public class zzz &lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;public void abc()&lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;PrintDocument pd = new PrintDocument(); &lt;br&gt;&lt;br /&gt;pd.PrintPage += new PrintPageEventHandler(pqr);&lt;br&gt;&lt;br /&gt;pd.Print();&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;void pqr(object o, PrintPageEventArgs e) &lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;Image i = Image.FromFile(&amp;quot;Sample.jpg&amp;quot;);&lt;br&gt;&lt;br /&gt;Point p = new Point(100, 100);&lt;br&gt;&lt;br /&gt;e.Graphics.DrawImage(i, p);&lt;br&gt;&lt;br /&gt;e.Graphics.DrawString (&amp;quot;Sonal Mukhi&amp;quot;, new Font(&amp;quot;Courier New&amp;quot;,10), Brushes.Black, &lt;br /&gt;100, 600);&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;public static void Main() &lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;zzz a = new zzz();&lt;br&gt;&lt;br /&gt;a.abc();&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;A printer has the capability of not only printing text, but also lines, &lt;br /&gt;ellipses, curves and images. This program prints an image. Before you proceed, &lt;br /&gt;ensure that file sample.jpg resides in the current directory. &lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;The Image class constructor is supplied with a jpg file, which we have picked up &lt;br /&gt;from the .NET samples. The Point object p represents the X, Y coordinates where &lt;br /&gt;the image is to be drawn. Then, using the DrawImage function from the Graphics &lt;br /&gt;class, the image is printed at the position specified. Using this procedure, all &lt;br /&gt;images displayed on the screen in the previous chapters can be sent to the &lt;br /&gt;printer for printing.&lt;/p&gt;</description><thr:total xmlns:thr="http://purl.org/syndication/thread/1.0">0</thr:total></item><item><title>C Language  Help, C Language Tutorials, C Language Programming, C Language Tricks  { The C# Language }</title><link>http://swebsolution.blogspot.com/2007/12/c-language-data-handling-c-language.html</link><author>noreply@blogger.com (Unknown)</author><pubDate>Sat, 22 Dec 2007 02:45:00 -0800</pubDate><guid isPermaLink="false">tag:blogger.com,1999:blog-7227425366482465228.post-8091488809835264745</guid><description>&lt;p align="justify"&gt;&lt;b&gt;C Language  Help, C Language Tutorials, C Language Programming, C Language Tricks &lt;/b&gt;&lt;br&gt;Data Handling&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;a.cs&lt;br&gt;&lt;br /&gt;using System;&lt;br&gt;&lt;br /&gt;using System.ComponentModel;&lt;br&gt;&lt;br /&gt;using System.Drawing;&lt;br&gt;&lt;br /&gt;using System.Windows.Forms;&lt;br&gt;&lt;br /&gt;using System.Data;&lt;br&gt;&lt;br /&gt;using System.Data.OleDb;&lt;br&gt;&lt;br /&gt;using System.IO;&lt;br&gt;&lt;br /&gt;using System.Collections;&lt;br&gt;&lt;br /&gt;public class CustomerList : System.Collections.CollectionBase &lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;public static CustomerList GetCustomers() &lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;CustomerList cl = new CustomerList();&lt;br&gt;&lt;br /&gt;Customer a = Customer.ReadCustomer1();&lt;br&gt;&lt;br /&gt;IList b = cl.List;&lt;br&gt;&lt;br /&gt;b.Add(a);&lt;br&gt;&lt;br /&gt;Customer c = new Customer(&amp;quot;246-12-5645&amp;quot;);&lt;br&gt;&lt;br /&gt;c.FirstName = &amp;quot;Vijay&amp;quot;;&lt;br&gt;&lt;br /&gt;c.DateOfBirth = DateTime.Parse(&amp;quot;5/3/1933&amp;quot;);&lt;br&gt;&lt;br /&gt;cl.Add1(c);&lt;br&gt;&lt;br /&gt;cl.Add1(Customer.ReadCustomer2());&lt;br&gt;&lt;br /&gt;return cl;&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;public int Add1(Customer value) &lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;return List.Add(value);&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;public class Customer : Component &lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;public string id,FirstName ;&lt;br&gt;&lt;br /&gt;public DateTime dateOfBirth;&lt;br&gt;&lt;br /&gt;public static Customer ReadCustomer1() &lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;Customer cust = new Customer(&amp;quot;536-45-1245&amp;quot;);&lt;br&gt;&lt;br /&gt;cust.FirstName = &amp;quot;Sonal&amp;quot;;&lt;br&gt;&lt;br /&gt;cust.DateOfBirth = DateTime.Parse(&amp;quot;9/9/1941&amp;quot;);&lt;br&gt;&lt;br /&gt;return cust;&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;public static Customer ReadCustomer2() &lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;Customer cust = new Customer(&amp;quot;651-27-8117&amp;quot;);&lt;br&gt;&lt;br /&gt;cust.FirstName = &amp;quot;Manish&amp;quot;;&lt;br&gt;&lt;br /&gt;cust.DateOfBirth = DateTime.Parse(&amp;quot;3/25/1942&amp;quot;);&lt;br&gt;&lt;br /&gt;return cust;&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;public Customer(string ID): base() &lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;id = ID ;&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;public string ID &lt;br&gt;&lt;br /&gt;{ &lt;br&gt;&lt;br /&gt;get &lt;br&gt;&lt;br /&gt;{ &lt;br&gt;&lt;br /&gt;return id ;&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;public string FirstName1 &lt;br&gt;&lt;br /&gt;{ &lt;br&gt;&lt;br /&gt;get &lt;br&gt;&lt;br /&gt;{ &lt;br&gt;&lt;br /&gt;return FirstName ;&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;public DateTime DateOfBirth&lt;br&gt;&lt;br /&gt;{ &lt;br&gt;&lt;br /&gt;get &lt;br&gt;&lt;br /&gt;{ &lt;br&gt;&lt;br /&gt;return dateOfBirth ;&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;set &lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;dateOfBirth = value ;&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;public class zzz : Form &lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;TextBox textBoxPosition;&lt;br&gt;&lt;br /&gt;CustomerList custList;&lt;br&gt;&lt;br /&gt;void textBoxDOB_FormatDate(object sender, ConvertEventArgs e) &lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;if (e.DesiredType != typeof(string)) &lt;br&gt;&lt;br /&gt;return ;&lt;br&gt;&lt;br /&gt;if (e.Value.GetType() != typeof(DateTime)) &lt;br&gt;&lt;br /&gt;return ;&lt;br&gt;&lt;br /&gt;DateTime dt = (DateTime)e.Value;&lt;br&gt;&lt;br /&gt;e.Value = dt.ToLongDateString();&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;void textBoxDOB_ParseDate(object sender, ConvertEventArgs e) &lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;if (e.DesiredType != typeof(DateTime)) &lt;br&gt;&lt;br /&gt;return ;&lt;br&gt;&lt;br /&gt;if (e.Value.GetType() != typeof(string)) &lt;br&gt;&lt;br /&gt;return ;&lt;br&gt;&lt;br /&gt;string value = (string)e.Value;&lt;br&gt;&lt;br /&gt;try &lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;e.Value = DateTime.Parse(value);&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;catch(Exception ex) &lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;MessageBox.Show(ex.Message);&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;void buttonMoveFirst_Click(object sender, System.EventArgs e) &lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;BindingContext[custList].Position = 0 ;&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;void buttonMoveLast_Click(object sender, System.EventArgs e) &lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;BindingContext[custList].Position = custList.Count - 1;&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;void buttonMoveNext_Click(object sender, System.EventArgs e) &lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;if (BindingContext[custList].Position &amp;lt; custList.Count - 1) &lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;BindingContext[custList].Position++;&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;void buttonMovePrev_Click(object sender, System.EventArgs e) &lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;if (BindingContext[custList].Position &amp;gt; 0) &lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;BindingContext[custList].Position--;&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;void customers_PositionChanged(object sender, System.EventArgs e) &lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;textBoxPosition.Text = &amp;quot;Record &amp;quot; + (BindingContext[custList].Position + 1) + &amp;quot; &lt;br /&gt;of &amp;quot; + custList.Count;&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;public zzz() {&lt;br&gt;&lt;br /&gt;Text = &amp;quot;Customer Details&amp;quot;;&lt;br&gt;&lt;br /&gt;ClientSize = new System.Drawing.Size(368, 413);&lt;br&gt;&lt;br /&gt;MinimumSize = new Size(368, (413 + SystemInformation.CaptionHeight));&lt;br&gt;&lt;br /&gt;Label labelFirstName,labelID,labelDOB;&lt;br&gt;&lt;br /&gt;labelID = new Label();&lt;br&gt;&lt;br /&gt;labelID.Location = new System.Drawing.Point(8, 32);&lt;br&gt;&lt;br /&gt;labelID.Text = &amp;quot;ID:&amp;quot;;&lt;br&gt;&lt;br /&gt;labelID.Size = new System.Drawing.Size(64, 16);&lt;br&gt;&lt;br /&gt;labelFirstName = new Label();&lt;br&gt;&lt;br /&gt;labelFirstName.Location = new System.Drawing.Point(8, 112);&lt;br&gt;&lt;br /&gt;labelFirstName.Text = &amp;quot;&amp;amp;First Name:&amp;quot;;&lt;br&gt;&lt;br /&gt;labelFirstName.Size = new System.Drawing.Size(64, 16);&lt;br&gt;&lt;br /&gt;labelDOB = new Label();&lt;br&gt;&lt;br /&gt;labelDOB.Location = new System.Drawing.Point(8, 194);&lt;br&gt;&lt;br /&gt;labelDOB.Text = &amp;quot;&amp;amp;Date of Birth:&amp;quot;;&lt;br&gt;&lt;br /&gt;labelDOB.Size = new System.Drawing.Size(92, 16);&lt;br&gt;&lt;br /&gt;TextBox textBoxDOB,textBoxFirstName,textBoxID;&lt;br&gt;&lt;br /&gt;textBoxID = new TextBox();&lt;br&gt;&lt;br /&gt;textBoxID.Location = new System.Drawing.Point(88, 30);&lt;br&gt;&lt;br /&gt;textBoxID.ReadOnly = true;&lt;br&gt;&lt;br /&gt;textBoxID.Enabled = false;&lt;br&gt;&lt;br /&gt;textBoxID.Size = new System.Drawing.Size(203, 20);&lt;br&gt;&lt;br /&gt;textBoxFirstName = new TextBox();&lt;br&gt;&lt;br /&gt;textBoxFirstName.Location = new System.Drawing.Point(88, 112);&lt;br&gt;&lt;br /&gt;textBoxFirstName.Size = new System.Drawing.Size(243, 20);&lt;br&gt;&lt;br /&gt;textBoxDOB = new TextBox();&lt;br&gt;&lt;br /&gt;textBoxDOB.Location = new System.Drawing.Point(88, 192);&lt;br&gt;&lt;br /&gt;textBoxDOB.Size = new System.Drawing.Size(243, 20);&lt;br&gt;&lt;br /&gt;textBoxPosition = new TextBox();&lt;br&gt;&lt;br /&gt;textBoxPosition.Location = new System.Drawing.Point(88, 14);&lt;br&gt;&lt;br /&gt;textBoxPosition.ReadOnly = true;&lt;br&gt;&lt;br /&gt;textBoxPosition.Enabled = false;&lt;br&gt;&lt;br /&gt;textBoxPosition.Size = new System.Drawing.Size(88, 20);&lt;br&gt;&lt;br /&gt;Button buttonMoveFirst,buttonMovePrev,&lt;br&gt;&lt;br /&gt;buttonMoveNext,buttonMoveLast;&lt;br&gt;&lt;br /&gt;buttonMoveNext = new Button();&lt;br&gt;&lt;br /&gt;buttonMoveNext.Location = new System.Drawing.Point(184, 8);&lt;br&gt;&lt;br /&gt;buttonMoveNext.FlatStyle = FlatStyle.Flat;&lt;br&gt;&lt;br /&gt;buttonMoveNext.Size = new System.Drawing.Size(32, 32);&lt;br&gt;&lt;br /&gt;buttonMoveNext.Text = &amp;quot;&amp;gt;&amp;quot;;&lt;br&gt;&lt;br /&gt;buttonMoveNext.Click += new System.EventHandler(buttonMoveNext_Click);&lt;br&gt;&lt;br /&gt;buttonMovePrev = new Button();&lt;br&gt;&lt;br /&gt;buttonMovePrev.Location = new System.Drawing.Point(48, 8);&lt;br&gt;&lt;br /&gt;buttonMovePrev.FlatStyle = FlatStyle.Flat;&lt;br&gt;&lt;br /&gt;buttonMovePrev.Size = new System.Drawing.Size(32, 32);&lt;br&gt;&lt;br /&gt;buttonMovePrev.Text = &amp;quot;&amp;lt;&amp;quot;;&lt;br&gt;&lt;br /&gt;buttonMovePrev.Click += new System.EventHandler(buttonMovePrev_Click);&lt;br&gt;&lt;br /&gt;buttonMoveFirst = new Button();&lt;br&gt;&lt;br /&gt;buttonMoveFirst.Location = new System.Drawing.Point(8, 8);&lt;br&gt;&lt;br /&gt;buttonMoveFirst.FlatStyle = FlatStyle.Flat;&lt;br&gt;&lt;br /&gt;buttonMoveFirst.Size = new System.Drawing.Size(32, 32);&lt;br&gt;&lt;br /&gt;buttonMoveFirst.Text = &amp;quot;|&amp;lt;&amp;quot;;&lt;br&gt;&lt;br /&gt;buttonMoveFirst.Click += new System.EventHandler(buttonMoveFirst_Click);&lt;br&gt;&lt;br /&gt;buttonMoveLast = new Button();&lt;br&gt;&lt;br /&gt;buttonMoveLast.Location = new System.Drawing.Point(224, 8);&lt;br&gt;&lt;br /&gt;buttonMoveLast.FlatStyle = FlatStyle.Flat;&lt;br&gt;&lt;br /&gt;buttonMoveLast.Size = new System.Drawing.Size(32, 32);&lt;br&gt;&lt;br /&gt;buttonMoveLast.Text = &amp;quot;&amp;gt;|&amp;quot;;&lt;br&gt;&lt;br /&gt;buttonMoveLast.Click += new System.EventHandler(buttonMoveLast_Click);&lt;br&gt;&lt;br /&gt;Panel panelVCRControl;&lt;br&gt;&lt;br /&gt;panelVCRControl = new Panel();&lt;br&gt;&lt;br /&gt;panelVCRControl.Location = new System.Drawing.Point(88, 344);&lt;br&gt;&lt;br /&gt;panelVCRControl.Size = new System.Drawing.Size(264, 48);&lt;br&gt;&lt;br /&gt;panelVCRControl.Text = &amp;quot;panel1&amp;quot;;&lt;br&gt;&lt;br /&gt;panelVCRControl.Controls.AddRange(new Control[] &lt;br&gt;&lt;br /&gt;{textBoxPosition,buttonMoveFirst,buttonMovePrev,&lt;br&gt;&lt;br /&gt;buttonMoveNext,buttonMoveLast});&lt;br&gt;&lt;br /&gt;Controls.AddRange(new Control[] &lt;br&gt;&lt;br /&gt;{textBoxDOB,labelDOB,panelVCRControl,textBoxFirstName,&lt;br&gt;&lt;br /&gt;textBoxID,labelFirstName,labelID});&lt;br&gt;&lt;br /&gt;custList = CustomerList.GetCustomers();&lt;br&gt;&lt;br /&gt;ControlBindingsCollection a = textBoxID.DataBindings;&lt;br&gt;&lt;br /&gt;a.Add(&amp;quot;Text&amp;quot;, custList, &amp;quot;ID&amp;quot;);&lt;br&gt;&lt;br /&gt;textBoxFirstName.DataBindings.Add(&amp;quot;Text&amp;quot;, custList, &amp;quot;FirstName1&amp;quot;);&lt;br&gt;&lt;br /&gt;Binding dobBinding = new Binding(&amp;quot;Text&amp;quot;, custList, &amp;quot;DateOfBirth&amp;quot;);&lt;br&gt;&lt;br /&gt;dobBinding.Format += new ConvertEventHandler(textBoxDOB_FormatDate) ;&lt;br&gt;&lt;br /&gt;dobBinding.Parse += new ConvertEventHandler(textBoxDOB_ParseDate) ;&lt;br&gt;&lt;br /&gt;textBoxDOB.DataBindings.Add(dobBinding);&lt;br&gt;&lt;br /&gt;BindingManagerBase c = BindingContext[custList];&lt;br&gt;&lt;br /&gt;c.PositionChanged += new EventHandler(customers_PositionChanged);&lt;br&gt;&lt;br /&gt;textBoxPosition.Text = &amp;quot;Record &amp;quot; + (BindingContext[custList].Position + 1) + &amp;quot; &lt;br /&gt;of &amp;quot; + custList.Count;&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;public static void Main() {&lt;br&gt;&lt;br /&gt;Application.Run(new zzz());&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;Before submerging deep into the topic of Data Binding with Windows Forms &lt;br /&gt;Controls, let us first address the issues of the User Interface. &lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;&amp;nbsp; 1&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;In the zzz constructor, the Text property of the Form class, which is &lt;br /&gt;initialized to 'Customer Details', dons the mantle of the window title. The &lt;br /&gt;ClientSize property determines the size of the Form. The MinimumSize property &lt;br /&gt;ensures that the size is not reduced beyond the value specified. We have dealt &lt;br /&gt;with these properties quite a while ago. The User Interface code is more germane &lt;br /&gt;for creating a visually appealing window, than for ameliorating our &lt;br /&gt;understanding of the core concepts of Data Binding.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;We intend to display three text labels in our window. To facilitate this, it is &lt;br /&gt;essential to create three label controls viz., labelFirstName, labelID and &lt;br /&gt;labelDOB. The text property of each is initialized to ID, First Name and Date of &lt;br /&gt;Birth respectively. We have deliberately not altered the names of the controls &lt;br /&gt;provided in the original sample. Furthermore, we have not modified any of the &lt;br /&gt;label control codes, in order to facilitate effortless comprehension. Besides &lt;br /&gt;Text, the other properties of the label control that we modify are the Location &lt;br /&gt;and the Size. &lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;This user interface code is usually written by a utility called the Screen &lt;br /&gt;Painter and not by the programmer. This is because, it is very irksome and &lt;br /&gt;arduous for a programmer to supply coordinates, in pixels, for Properties such &lt;br /&gt;as the Location etc.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;We will display data employing the services of a textbox. Thus, three textboxes &lt;br /&gt;named textBoxDOB, textBoxFirstName and textBoxID, have been provided, to store &lt;br /&gt;the date of birth, first name of customer and the customer ID, respectively. The &lt;br /&gt;Size of each textbox is defined by modifying the value contained in the Size &lt;br /&gt;property. Thereafter, a new location is specified using the Location property. &lt;br /&gt;The textbox displaying the customer ID has the ReadOnly property set to true, so &lt;br /&gt;that its contents cannot be altered. Moreover, setting the Disabled property to &lt;br /&gt;false disables the field. One more textbox control called textBoxPosition is &lt;br /&gt;introduced. It displays the current active record and the total number of &lt;br /&gt;records in the recordset. We shall not discuss the textbox properties any &lt;br /&gt;further in the forthcoming programs. &lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;We now need buttons in our window to enable the user to navigate between &lt;br /&gt;records. To attain this, the four button controls and their corresponding &lt;br /&gt;actions are given below: &lt;br&gt;&lt;br /&gt;• buttonMovePrev : Moves to the previous record.&lt;br&gt;&lt;br /&gt;• buttonMoveNext : Moves to the next record.&lt;br&gt;&lt;br /&gt;• buttonMoveFirst : Jumps to the first record.&lt;br&gt;&lt;br /&gt;• buttonMoveLast : Jumps to the last record. &lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;We can set the properties of Location, Size and Text of the button controls to &lt;br /&gt;suit our requirements. For an enhanced visual appeal, the FlatStyle property is &lt;br /&gt;also altered. We use the Click event to wire up each button to a corresponding &lt;br /&gt;method having a similar name. This is done to ensure that every time we click on &lt;br /&gt;a button, the desired code gets executed. The code that gets activated will be &lt;br /&gt;dealt with subsequently.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;A Panel control, by itself, is worthless, since it does nothing. Its role &lt;br /&gt;becomes consequential only when it aggregates or collects other controls. By &lt;br /&gt;placing controls within a Panel control, we can deal with all of them &lt;br /&gt;simultaneously. Thus, with a single line of code, we can disable a panel, &lt;br /&gt;thereby effectively, disabling all the controls contained therein. So, by using &lt;br /&gt;a panel control, a large number of controls can be treated as a single control &lt;br /&gt;and all their properties can be changed in unison. The Location and Size &lt;br /&gt;properties of the Panel control named panelVCRControl are set to certain &lt;br /&gt;specific co-ordinates. The Text property is initialized, but it does not get &lt;br /&gt;displayed on the screen.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;The Panel control, like any other control, has a Controls property having a data &lt;br /&gt;type of Control.ControlCollection. Using the AddRange method that requires an &lt;br /&gt;array, all controls are added in a single action to the ControlCollection. This &lt;br /&gt;is analogous in functionality to the Add function, which adds only a single &lt;br /&gt;control to the Controls Collection. Thus, internally, AddRange repetitively &lt;br /&gt;calls the Add function, and in each iteration, supplies it with a single member &lt;br /&gt;of the array, till it has passed all the members of the entire array as &lt;br /&gt;parameters. &lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;To summarize, we add the four textboxes and one label control to the panel, so &lt;br /&gt;that we can treat them as a single entity. In this program, we however, are not &lt;br /&gt;utilizing this property. The rest of the controls, including the panel control, &lt;br /&gt;are finally added to the main Form using the above AddRange function.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;The object custList is of user-defined data type CustomerList, which is derived &lt;br /&gt;from CollectionBase. This class contains a static function called GetCustomers. &lt;br /&gt;This function creates an object c1, which is an instance of CustomerList. By &lt;br /&gt;using New and Finally, the value in this object is returned to custList in the &lt;br /&gt;zzz constructor. &lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;We have another class Customer, which is derived from the Component class (this &lt;br /&gt;is optional). It represents a single Customer. The CustomerList class symbolizes &lt;br /&gt;a list or an assemblage of customers. The Customer class has a static function &lt;br /&gt;ReadCustomer1 that creates an instance of class Customer and passes the customer &lt;br /&gt;ID to the constructor. The constructor initializes the field id with this value.&lt;br /&gt;&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;The Customer object is represented by three variables, viz., id, FirstName and &lt;br /&gt;dateofBirth. Thus, an object is identified by its fields or variables, and not &lt;br /&gt;by the methods it employs. The programmers at Microsoft chose to initialize the &lt;br /&gt;id field through the constructor and the other fields separately. They could &lt;br /&gt;instead have initialized all three fields through the constructor or initialized &lt;br /&gt;none at all. &lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;The most noteworthy thing here is that the FirstName field can be directly &lt;br /&gt;accessed, whereas, the dateofBirth field is accessible only through the property &lt;br /&gt;DateofBirth, using its set accessor. It is sensible and prudent to prevent &lt;br /&gt;access to a field directly, and allow access only through a property. The id &lt;br /&gt;field, however, is accessed through the constructor and not through the property &lt;br /&gt;id. We shall delve upon this, before long.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;Object a represents the first customer. This object is stored in the &lt;br /&gt;CustomerList class since it is derived from class CollectionBase. It has the &lt;br /&gt;ability to store multiple objects. The CollectionBase class has a property &lt;br /&gt;called List of data type lList, which represents the collection. An IList object &lt;br /&gt;named b has a method Add, which adds any object to the Collection and returns &lt;br /&gt;the position where the object has been added. Thus, we have added a Customer &lt;br /&gt;object to the list.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;To add the second customer, we create a Customer object in the same class, and &lt;br /&gt;initialize the members directly. This customer is also added to the CustomerList &lt;br /&gt;by calling a function Add1, which uses the List property to Add the customer.&lt;br /&gt;&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;The third customer is added to the List, using a more compact form. Each one of &lt;br /&gt;us possesses a distinct style of writing code. Thus, it is ineffectual to debate &lt;br /&gt;over the issue of why a static function has been used by us to create an object, &lt;br /&gt;instead of creating it directly.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;The CustomerList class, which is derived from Collections, can store any &lt;br /&gt;arbitrary object. The Add1 function is not essential, but it facilitates the &lt;br /&gt;addition of objects to the collection. Thus, the CustomerList class is a simple &lt;br /&gt;collection of objects. Any other entity that can represent a collection could &lt;br /&gt;also have been used instead.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;Reverting back to our constructor zzz, the Custlist object now provides access &lt;br /&gt;to the three customer objects through a collection object. Every control has a &lt;br /&gt;ReadOnly property called DataBindings, which is of the data type &lt;br /&gt;ControlBindingsCollection. ControlBindingsCollection in turn, is derived from &lt;br /&gt;class BindingsCollection. This class is used to bind a control to the data &lt;br /&gt;source, since it represents a collection of all data bindings for a control. &lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;The Add function accepts three parameters and returns a Binding object. &lt;br&gt;&lt;br /&gt;• The first parameter is a string representing the name of the property of the &lt;br /&gt;control that we want to bind to. In our program, we have used the property named &lt;br /&gt;Text. &lt;br&gt;&lt;br /&gt;• The second parameter is of type object, which represents the data i.e. the &lt;br /&gt;collection object or data source. In this case, it is custlist.&lt;br&gt;&lt;br /&gt;• The third parameter is the name of the field or property that we need to bind &lt;br /&gt;to. Here, the field name is id. &lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;Thus, we are binding a field called id in the data source custList to the first &lt;br /&gt;textbox's Text property. Using the above mechanism, we could bind any column in &lt;br /&gt;the data source to any valid control property, such as, backcolor or forecolor.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;In the next set, we bind the Text property of the second textbox control, &lt;br /&gt;textBoxFirstName, to the column FirstName1 in the data source custlist.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;The third column is added with the help of a class called Binding, which only &lt;br /&gt;understands Binding. This class represents a simple relationship between the &lt;br /&gt;property of a control and that of any object. The constructor of the Binding &lt;br /&gt;class is given the same three parameters as those of the Add function, and they &lt;br /&gt;also have the same significance. &lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;We shall now explain the second parameter, i.e. a data source. The second &lt;br /&gt;parameter could be any class that derives from interfaces, IBindingList or &lt;br /&gt;ITypedList. These comprise of the DataSet, DataTable, DataView, or &lt;br /&gt;DataViewManager classes. These classes implement the IList interface. In effect, &lt;br /&gt;there are a large number of classes (over 20), which implement from this &lt;br /&gt;interface. We have used CollectionBase in our present example. The only &lt;br /&gt;safeguard to be kept in mind is that, an IList object has to be created first, &lt;br /&gt;and only then can it be used in any of the bindings functions. The object in the &lt;br /&gt;list must be of the same data type, or else, an exception will get thrown. The &lt;br /&gt;last type permitted as a data source is a strongly typed IList such as an array.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;One commendable feature about the Binding class is that it permits the user to &lt;br /&gt;determine the display patterns of data. It also acts as the validating &lt;br /&gt;authority, whenever the user makes any alteration to the data. The Binding &lt;br /&gt;manager calls the Format event when it has to display some data in the control &lt;br /&gt;and calls the Parse event when it has to retrieve data. Thus, we can build our &lt;br /&gt;own custom formats. &lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;The Format event is attached to a function called textBoxDOB_FormatDate, using &lt;br /&gt;the delegate ConvertEventHandler. This function supplies the custom format in &lt;br /&gt;which the date is to be displayed. We use the same principles to call the method &lt;br /&gt;textBoxDOB_ParseDate, which parses the date and checks for errors. We shall make &lt;br /&gt;an endeavor to grasp these functions, in a short while. &lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;The Add function used earlier, was overloaded to accept either of the following:&lt;br&gt;&lt;br /&gt;• two strings and an object parameter.&lt;br&gt;&lt;br /&gt;• two strings and a Binding object. &lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;Thus, the only difference between the two data bindings is that, by using a &lt;br /&gt;Binding object, we are able to customize the display of data.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;The Form class has a BindingContext property that returns a BindingContext &lt;br /&gt;object. The indexer returns a BindingManagerBase object, which represents all &lt;br /&gt;data-bound controls, which are bound to the same data source, and keeps them &lt;br /&gt;synchronized. It is this BindingManagerBase object that facilitates movement &lt;br /&gt;from one record to another. As of now, we initialize its PositionChanged event &lt;br /&gt;to a function customers_PositionChanged, which will be called each time the &lt;br /&gt;Position property changes. The secret of how this is done shall be revealed at a &lt;br /&gt;later date.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;Any class derived from Collections has a member called Count, which returns the &lt;br /&gt;number of objects present in the Collection. In our case, the count is shown as &lt;br /&gt;3. The BindingManagerBase has a member called Position that exposes the object &lt;br /&gt;(which is the current object in the list or data source), to which the control &lt;br /&gt;is bound. This index is zero based i.e. the first object in the list is numbered &lt;br /&gt;zero. And since it is zero based, the Text property of the label in the panel is &lt;br /&gt;initialized to a string containing the value of the Position property + 1. It is &lt;br /&gt;followed by the string 'of', and finally by the value returned by the Count &lt;br /&gt;property of the custList object. The text is finally displayed as '1 of 3'.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;&amp;nbsp; 2&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;When the form loads on, the first object is the active object. The &lt;br /&gt;BindingManagerBase class now ensures that the Text property of the three &lt;br /&gt;textboxes is initialized to the appropriate value. &lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;The first textbox is bound to a field called ID. Thus, the Customer class is &lt;br /&gt;searched for a property called ID. The 'get' accessor is called. The value &lt;br /&gt;returned by it is the value displayed in the textbox. Thus, the Customer class &lt;br /&gt;needs a property called ID with a 'get' accessor. If the name of the property is &lt;br /&gt;modified, say to ID1, or if the get accessor is removed, an exception is &lt;br /&gt;generated at run time; however, no errors will be generated at compile time.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;The same holds true for the FisrtName1 property and the DateofBirth. The &lt;br /&gt;DateofBirth differs, in that, the function textBoxDOB_FormatDate is called after &lt;br /&gt;the get accessor is called. This is done to facilitate display of the date in &lt;br /&gt;the format approved by us. The second parameter 'e' in textBoxDOB_FormatDate, &lt;br /&gt;which is of data type ConvertEventArgs, is well acquainted with the object that &lt;br /&gt;is to be displayed in the textbox. The DesiredType property of the parameter &lt;br /&gt;class contains the original data type of the property that is bound in the data &lt;br /&gt;source. If its type is not DateTime, the program exits from the function &lt;br /&gt;gracefully using Return. This parameter 'e' has a property called Value, which &lt;br /&gt;contains the actual unformatted value that exists in the data source. As the &lt;br /&gt;return value of the property is object, we use the GetType function to retrieve &lt;br /&gt;the type of this unformatted value. If the type is a string, the program &lt;br /&gt;continues execution, or else, it exits. Yet another error check!&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;The fact that the program has been able to pass beyond the above two error &lt;br /&gt;checks successfully establishes that the value in hand can now be formatted to &lt;br /&gt;the type we desire. Therefore, we first cast this value into a DateTime object &lt;br /&gt;dt, and then, use the ToLongDateString function from the class, to convert the &lt;br /&gt;date into a string using the long form. This value is stored back into the Value &lt;br /&gt;property of the parameter 'e' and is displayed as the Text property of the &lt;br /&gt;textbox. &lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;WWhenever we attempt at altering the date into a valid or an invalid one, the &lt;br /&gt;function textBoxDOB_ParseDate or the Parse event gets called. This function runs &lt;br /&gt;the same two error checks on the date, and thereafter, stores the value of the &lt;br /&gt;Value property in a string. Thereafter, the string is converted into a datetime &lt;br /&gt;object. If this process does not score a success for any reason, an exception is &lt;br /&gt;thrown and a MesssageBox is displayed. This modified value in the Value property &lt;br /&gt;of the parameter, is stored back in the data source by the framework.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;&amp;nbsp;&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;When the form loads on, we notice the first record from the data source. To see &lt;br /&gt;the next record, we have to press the button with the display of the &amp;gt; arrow. &lt;br /&gt;This is the uttonMoveNext control. This action consecutively, calls function &lt;br /&gt;buttonMoveNext_Click that uses the BindingManagerBase object and the &lt;br /&gt;BindingContext to increment the Position property by a value of 1. This process &lt;br /&gt;is encapsulated in an 'if' statement, so that a check can be performed on &lt;br /&gt;whether the record is the last one in the list or not. The last record is &lt;br /&gt;retrieved, using the Count property of the data source. &lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;The subtraction of 1 is mandatory, since the Position property is zero based. To &lt;br /&gt;move backwards, the Position property is decremented by 1, and the 'if' &lt;br /&gt;statement verifies whether the value is greater than zero or not. To move to the &lt;br /&gt;first record, we set the Position property to 0, and to move to the last record, &lt;br /&gt;we set the Position property to Count-1. Each time we change the Position &lt;br /&gt;property, the function customers_PositionChanged gets called. Here, the textbox &lt;br /&gt;was updated in a manner similar to what was done earlier to change the position &lt;br /&gt;of the record pointer.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;The program is considerably extensive, but it exhibits the data binding &lt;br /&gt;properties of a control, in order to display objects from a data source.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;a.cs&lt;br&gt;&lt;br /&gt;using System;&lt;br&gt;&lt;br /&gt;using System.ComponentModel;&lt;br&gt;&lt;br /&gt;using System.Drawing;&lt;br&gt;&lt;br /&gt;using System.Windows.Forms;&lt;br&gt;&lt;br /&gt;using System.Data;&lt;br&gt;&lt;br /&gt;using System.IO;&lt;br&gt;&lt;br /&gt;using System.Collections;&lt;br&gt;&lt;br /&gt;public class zzz : Form {&lt;br&gt;&lt;br /&gt;DataTable t; &lt;br&gt;&lt;br /&gt;int cnt;&lt;br&gt;&lt;br /&gt;TextBox textBoxPosition;&lt;br&gt;&lt;br /&gt;void buttonMoveFirst_Click(object sender, System.EventArgs e) &lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;BindingContext[t].Position = 0 ;&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;void buttonMoveLast_Click(object sender, System.EventArgs e) &lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;BindingContext[t].Position = cnt - 1;&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;void buttonMoveNext_Click(object sender, System.EventArgs e) &lt;br&gt;&lt;br /&gt;{{&lt;br&gt;&lt;br /&gt;if (BindingContext[t].Position &amp;lt; cnt - 1) {&lt;br&gt;&lt;br /&gt;BindingContext[t].Position++;&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;void buttonMovePrev_Click(object sender, System.EventArgs e) &lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;if (BindingContext[t].Position &amp;gt; 0) &lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;BindingContext[t].Position--;&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;void customers_PositionChanged(object sender, System.EventArgs e) &lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;textBoxPosition.Text = &amp;quot;Record &amp;quot; + (BindingContext[t].Position + 1) + &amp;quot; of &amp;quot; + &lt;br /&gt;cnt;&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;public zzz() {&lt;br&gt;&lt;br /&gt;Text = &amp;quot;Customer Details&amp;quot;;&lt;br&gt;&lt;br /&gt;ClientSize = new System.Drawing.Size(368, 413);&lt;br&gt;&lt;br /&gt;MinimumSize = new Size(368, (413 + SystemInformation.CaptionHeight));&lt;br&gt;&lt;br /&gt;TextBox textBoxFirstName;&lt;br&gt;&lt;br /&gt;textBoxFirstName = new TextBox();&lt;br&gt;&lt;br /&gt;textBoxFirstName.Location = new System.Drawing.Point(88, 112);&lt;br&gt;&lt;br /&gt;textBoxFirstName.Size = new System.Drawing.Size(243, 20);&lt;br&gt;&lt;br /&gt;textBoxPosition = new TextBox();&lt;br&gt;&lt;br /&gt;textBoxPosition.Location = new System.Drawing.Point(88, 14);&lt;br&gt;&lt;br /&gt;textBoxPosition.ReadOnly = true;&lt;br&gt;&lt;br /&gt;textBoxPosition.Enabled = false;&lt;br&gt;&lt;br /&gt;textBoxPosition.Size = new System.Drawing.Size(88, 20);&lt;br&gt;&lt;br /&gt;Button buttonMoveFirst,buttonMovePrev,buttonMoveNext,buttonMoveLast;&lt;br&gt;&lt;br /&gt;buttonMoveNext = new Button();&lt;br&gt;&lt;br /&gt;buttonMoveNext.Location = new System.Drawing.Point(184, 8);&lt;br&gt;&lt;br /&gt;buttonMoveNext.FlatStyle = FlatStyle.Flat;&lt;br&gt;&lt;br /&gt;buttonMoveNext.Size = new System.Drawing.Size(32, 32);&lt;br&gt;&lt;br /&gt;buttonMoveNext.Text = &amp;quot;&amp;gt;&amp;quot;;&lt;br&gt;&lt;br /&gt;buttonMoveNext.Click += new System.EventHandler(buttonMoveNext_Click);&lt;br&gt;&lt;br /&gt;buttonMovePrev = new Button();&lt;br&gt;&lt;br /&gt;buttonMovePrev.Location = new System.Drawing.Point(48, 8);&lt;br&gt;&lt;br /&gt;buttonMovePrev.FlatStyle = FlatStyle.Flat;&lt;br&gt;&lt;br /&gt;buttonMovePrev.Size = new System.Drawing.Size(32, 32);&lt;br&gt;&lt;br /&gt;buttonMovePrev.Text = &amp;quot;&amp;lt;&amp;quot;;&lt;br&gt;&lt;br /&gt;buttonMovePrev.Click += new System.EventHandler(buttonMovePrev_Click);&lt;br&gt;&lt;br /&gt;buttonMoveFirst = new Button();&lt;br&gt;&lt;br /&gt;buttonMoveFirst.Location = new System.Drawing.Point(8, 8);&lt;br&gt;&lt;br /&gt;buttonMoveFirst.FlatStyle = FlatStyle.Flat;&lt;br&gt;&lt;br /&gt;buttonMoveFirst.Size = new System.Drawing.Size(32, 32);&lt;br&gt;&lt;br /&gt;buttonMoveFirst.Text = &amp;quot;|&amp;lt;&amp;quot;;&lt;br&gt;&lt;br /&gt;buttonMoveFirst.Click += new System.EventHandler(buttonMoveFirst_Click);&lt;br&gt;&lt;br /&gt;buttonMoveLast = new Button();&lt;br&gt;&lt;br /&gt;buttonMoveLast.Location = new System.Drawing.Point(224, 8);&lt;br&gt;&lt;br /&gt;buttonMoveLast.FlatStyle = FlatStyle.Flat;&lt;br&gt;&lt;br /&gt;buttonMoveLast.Size = new System.Drawing.Size(32, 32);&lt;br&gt;&lt;br /&gt;buttonMoveLast.Text = &amp;quot;&amp;gt;|&amp;quot;;&lt;br&gt;&lt;br /&gt;buttonMoveLast.Click += new System.EventHandler(buttonMoveLast_Click);&lt;br&gt;&lt;br /&gt;Panel panelVCRControl;&lt;br&gt;&lt;br /&gt;panelVCRControl = new Panel();&lt;br&gt;&lt;br /&gt;panelVCRControl.Location = new System.Drawing.Point(88, 344);&lt;br&gt;&lt;br /&gt;panelVCRControl.Size = new System.Drawing.Size(264, 48);&lt;br&gt;&lt;br /&gt;panelVCRControl.Text = &amp;quot;panel1&amp;quot;;&lt;br&gt;&lt;br /&gt;panelVCRControl.Controls.AddRange(new Control[] &lt;br&gt;&lt;br /&gt;{textBoxPosition,buttonMoveFirst,buttonMovePrev,&lt;br&gt;&lt;br /&gt;buttonMoveNext,buttonMoveLast});&lt;br&gt;&lt;br /&gt;Controls.AddRange(new Control[] &lt;br&gt;&lt;br /&gt;{panelVCRControl,textBoxFirstName});&lt;br&gt;&lt;br /&gt;t = MakeTable();&lt;br&gt;&lt;br /&gt;textBoxFirstName.DataBindings.Add(&amp;quot;Text&amp;quot;, t, &amp;quot;Text&amp;quot;);&lt;br&gt;&lt;br /&gt;textBoxFirstName.DataBindings.Add(&amp;quot;BackColor&amp;quot;, t, &amp;quot;BackColor&amp;quot;);&lt;br&gt;&lt;br /&gt;textBoxFirstName.DataBindings.Add(&amp;quot;ForeColor&amp;quot;, t, &amp;quot;ForeColor&amp;quot;);&lt;br&gt;&lt;br /&gt;BindingManagerBase c = BindingContext[t];&lt;br&gt;&lt;br /&gt;c.PositionChanged += new EventHandler(customers_PositionChanged);&lt;br&gt;&lt;br /&gt;cnt = t.Rows.Count;&lt;br&gt;&lt;br /&gt;textBoxPosition.Text = &amp;quot;Record &amp;quot; + (BindingContext[t].Position + 1) + &amp;quot; of &amp;quot; + &lt;br /&gt;cnt;&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;private DataTable MakeTable() {&lt;br&gt;&lt;br /&gt;DataTable t = new DataTable(&amp;quot;Control&amp;quot;);&lt;br&gt;&lt;br /&gt;t.Columns.Add(&amp;quot;BackColor&amp;quot;, typeof(Color));&lt;br&gt;&lt;br /&gt;t.Columns.Add(&amp;quot;ForeColor&amp;quot;, typeof(Color));&lt;br&gt;&lt;br /&gt;t.Columns.Add(&amp;quot;Text&amp;quot;);&lt;br&gt;&lt;br /&gt;DataRow r;&lt;br&gt;&lt;br /&gt;r = t.NewRow();&lt;br&gt;&lt;br /&gt;r[&amp;quot;BackColor&amp;quot;] = Color.Blue;&lt;br&gt;&lt;br /&gt;r[&amp;quot;ForeColor&amp;quot;] = Color.Yellow;&lt;br&gt;&lt;br /&gt;r[&amp;quot;Text&amp;quot;] = &amp;quot;Yellow on Blue&amp;quot;;&lt;br&gt;&lt;br /&gt;t.Rows.Add(r);&lt;br&gt;&lt;br /&gt;r = t.NewRow();&lt;br&gt;&lt;br /&gt;r[&amp;quot;BackColor&amp;quot;] = Color.White;&lt;br&gt;&lt;br /&gt;r[&amp;quot;ForeColor&amp;quot;] = Color.Green;&lt;br&gt;&lt;br /&gt;r[&amp;quot;Text&amp;quot;] = &amp;quot;Green on white&amp;quot;;&lt;br&gt;&lt;br /&gt;t.Rows.Add(r);&lt;br&gt;&lt;br /&gt;r = t.NewRow();&lt;br&gt;&lt;br /&gt;r[&amp;quot;BackColor&amp;quot;] = Color.Orange;&lt;br&gt;&lt;br /&gt;r[&amp;quot;ForeColor&amp;quot;] = Color.Black;&lt;br&gt;&lt;br /&gt;r[&amp;quot;Text&amp;quot;] = &amp;quot;Black on Orange&amp;quot;;&lt;br&gt;&lt;br /&gt;t.Rows.Add(r);&lt;br&gt;&lt;br /&gt;return t;&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;public static void Main() {&lt;br&gt;&lt;br /&gt;Application.Run(new zzz());&lt;br&gt;&lt;br /&gt;}&lt;br&gt;}&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;&amp;nbsp; 4&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;This example borrows sufficiently from the previous example. The user interface &lt;br /&gt;too is almost similar to the earlier one.  &lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;This program has one textbox called textBoxFirstName, which has 3 data bindings &lt;br /&gt;on properties of Text, BackColor and ForeColor. The data source is a DataTable &lt;br /&gt;object t. The properties in the DataTable have the same name as the bound &lt;br /&gt;textbox properties. &lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;To create a DataTable object, we create a new instance of DataTable and pass a &lt;br /&gt;string, Control, that signifies the name of the table. Every data table desires &lt;br /&gt;columns. Hence, there is a Columns property that is a collection. The Columns &lt;br /&gt;collection has an Add method that adds a column. The Column names specified must &lt;br /&gt;correspond with those given in the Bindings. &lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;The first two columns are of data type Color, and the last column called Text, &lt;br /&gt;is of the data type String. After having added 3 columns, we need to add a row. &lt;br /&gt;The NewRow function returns a blank DataRow object. So, we use the indexer of &lt;br /&gt;the data row and pass the column name as a parameter to the indexer, in order to &lt;br /&gt;initialize the columns. Finally, we use the Add function of the Rows Collection &lt;br /&gt;to add a new row. This process is repeated thrice, once for each column.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;&amp;nbsp; 5&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;The difference here is that, the data source has changed from a Collection to a &lt;br /&gt;DataTable object. The column names correspond to the property names specified in &lt;br /&gt;the Bindings function. Thus, they can now replace the property names. We can &lt;br /&gt;bind multiple properties of a control to fields from a data source. The Binding &lt;br /&gt;manager handles this internally. Thus, each time we move from one row to another &lt;br /&gt;in the DataTable, three properties of the textbox get modified, and we get to &lt;br /&gt;see a colorful output. &lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;The Rows Collection has a member called Count, which signifies the number of &lt;br /&gt;records or rows present in the DataTable. &lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;So far, we have discovered that one or more properties of a control can be bound &lt;br /&gt;or associated with a field, column or property of a data source. Thereafter, the &lt;br /&gt;Binding Context is employed to move the record pointer in the data source. The &lt;br /&gt;framework then ensures that the properties of the control get updated &lt;br /&gt;automatically. &lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;We can bind as many properties of a control as we desire, and the data source &lt;br /&gt;too can be of different types, with strings attached. If it is a DataTable, we &lt;br /&gt;need to bind to column names, however, if it is a Collections object, we require &lt;br /&gt;a property with a get accessor.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;a.cs&lt;br&gt;&lt;br /&gt;using System;&lt;br&gt;&lt;br /&gt;using System.Data;&lt;br&gt;&lt;br /&gt;using System.Drawing;&lt;br&gt;&lt;br /&gt;using System.Globalization;&lt;br&gt;&lt;br /&gt;using System.Windows.Forms;&lt;br&gt;&lt;br /&gt;public class zzz : Form {&lt;br&gt;&lt;br /&gt;Button button1,button2,button3,button4;&lt;br&gt;&lt;br /&gt;TextBox text1,text2,text3,text4;&lt;br&gt;&lt;br /&gt;BindingManagerBase bmCustomers,bmOrders;&lt;br&gt;&lt;br /&gt;DataSet ds;&lt;br&gt;&lt;br /&gt;DateTimePicker DateTimePicker1;&lt;br&gt;&lt;br /&gt;void DecimalToCurrencyString(object sender, ConvertEventArgs cevent)&lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;if(cevent.DesiredType != typeof(string)) return;&lt;br&gt;&lt;br /&gt;cevent.Value = ((decimal) cevent.Value).ToString(&amp;quot;c&amp;quot;);&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;void CurrencyStringToDecimal(object sender, ConvertEventArgs cevent)&lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;if(cevent.DesiredType != typeof(decimal)) return;&lt;br&gt;&lt;br /&gt;cevent.Value = Decimal.Parse(cevent.Value.ToString(),NumberStyles.Currency, &lt;br /&gt;null);&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;protected void button1_Click(object sender, System.EventArgs e)&lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;bmCustomers.Position -= 1;&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;protected void button2_Click(object sender, System.EventArgs e)&lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;bmCustomers.Position += 1;&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;protected void button3_Click(object sender, System.EventArgs e)&lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;bmOrders.Position-=1;&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;protected void button4_Click(object sender, System.EventArgs e)&lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;bmOrders.Position+=1;&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;protected void BindControls()&lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;text1.DataBindings.Add(new Binding(&amp;quot;Text&amp;quot;, ds, &amp;quot;customers.custName&amp;quot;));&lt;br&gt;&lt;br /&gt;text2.DataBindings.Add(new Binding(&amp;quot;Text&amp;quot;, ds, &amp;quot;customers.custID&amp;quot;));&lt;br&gt;&lt;br /&gt;DateTimePicker1.DataBindings.Add(new Binding(&amp;quot;Value&amp;quot;, ds, &amp;quot;customers.CustToOrders.OrderDate&amp;quot;));&lt;br&gt;&lt;br /&gt;Binding b = new Binding(&amp;quot;Text&amp;quot;, ds, &amp;quot;customers.custToOrders.OrderAmount&amp;quot;);&lt;br&gt;&lt;br /&gt;b.Parse+=new ConvertEventHandler(CurrencyStringToDecimal);&lt;br&gt;&lt;br /&gt;b.Format+=new ConvertEventHandler(DecimalToCurrencyString);&lt;br&gt;&lt;br /&gt;text3.DataBindings.Add(b);&lt;br&gt;&lt;br /&gt;text4.DataBindings.Add(new Binding(&amp;quot;Text&amp;quot;, ds, &amp;quot;customers.CustToOrders.custID&amp;quot;));&lt;br&gt;&lt;br /&gt;bmCustomers = BindingContext [ds, &amp;quot;Customers&amp;quot;];&lt;br&gt;&lt;br /&gt;bmOrders = BindingContext[ds, &amp;quot;customers.CustToOrders&amp;quot;];&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;void MakeDataSet()&lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;ds = new DataSet(&amp;quot;myDataSet&amp;quot;);&lt;br&gt;&lt;br /&gt;DataTable tCust = new DataTable(&amp;quot;Customers&amp;quot;);&lt;br&gt;&lt;br /&gt;DataTable tOrders = new DataTable(&amp;quot;Orders&amp;quot;);&lt;br&gt;&lt;br /&gt;DataColumn cCustID = new DataColumn(&amp;quot;CustID&amp;quot;);&lt;br&gt;&lt;br /&gt;DataColumn cCustName = new DataColumn(&amp;quot;CustName&amp;quot;);&lt;br&gt;&lt;br /&gt;tCust.Columns.Add(cCustID);&lt;br&gt;&lt;br /&gt;tCust.Columns.Add(cCustName);&lt;br&gt;&lt;br /&gt;DataColumn cID = new DataColumn(&amp;quot;CustID&amp;quot;);&lt;br&gt;&lt;br /&gt;DataColumn cOrderDate = new DataColumn(&amp;quot;orderDate&amp;quot;,typeof(DateTime));&lt;br&gt;&lt;br /&gt;DataColumn cOrderAmount = new DataColumn(&amp;quot;OrderAmount&amp;quot;, typeof(decimal));&lt;br&gt;&lt;br /&gt;tOrders.Columns.Add(cOrderAmount);&lt;br&gt;&lt;br /&gt;tOrders.Columns.Add(cID);&lt;br&gt;&lt;br /&gt;tOrders.Columns.Add(cOrderDate);&lt;br&gt;&lt;br /&gt;ds.Tables.Add(tCust);&lt;br&gt;&lt;br /&gt;ds.Tables.Add(tOrders);&lt;br&gt;&lt;br /&gt;DataRelation dr = new DataRelation(&amp;quot;custToOrders&amp;quot;, cCustID , cID);&lt;br&gt;&lt;br /&gt;ds.Relations.Add(dr);&lt;br&gt;&lt;br /&gt;DataRow newRow1,newRow2;&lt;br&gt;&lt;br /&gt;for(int i = 1; i &amp;lt; 4; i++)&lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;newRow1 = tCust.NewRow();&lt;br&gt;&lt;br /&gt;newRow1[&amp;quot;custID&amp;quot;] = &amp;quot;Cust &amp;quot; + i;&lt;br&gt;&lt;br /&gt;tCust.Rows.Add(newRow1);&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;tCust.Rows[0][&amp;quot;custName&amp;quot;] = &amp;quot;Vijay&amp;quot;;&lt;br&gt;&lt;br /&gt;tCust.Rows[1][&amp;quot;custName&amp;quot;] = &amp;quot;Sonal&amp;quot;;&lt;br&gt;&lt;br /&gt;tCust.Rows[2][&amp;quot;custName&amp;quot;] = &amp;quot;Manish&amp;quot;;&lt;br&gt;&lt;br /&gt;for(int i = 1; i &amp;lt; 4; i++)&lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;for(int j = 1; j &amp;lt; 6; j++)&lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;newRow2 = tOrders.NewRow();&lt;br&gt;&lt;br /&gt;newRow2[&amp;quot;CustID&amp;quot;]= &amp;quot;Cust &amp;quot; + i;&lt;br&gt;&lt;br /&gt;newRow2[&amp;quot;orderDate&amp;quot;]= new DateTime(2001, i, j * 2);&lt;br&gt;&lt;br /&gt;newRow2[&amp;quot;OrderAmount&amp;quot;] = i * 10 + j * .1;&lt;br&gt;&lt;br /&gt;tOrders.Rows.Add(newRow2);&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;public zzz() {&lt;br&gt;&lt;br /&gt;Text = &amp;quot;Binding Sample&amp;quot;;&lt;br&gt;&lt;br /&gt;ClientSize = new System.Drawing.Size(450, 200);&lt;br&gt;&lt;br /&gt;button1 = new Button();&lt;br&gt;&lt;br /&gt;button1.Location = new System.Drawing.Point(24, 16);&lt;br&gt;&lt;br /&gt;button1.Size = new System.Drawing.Size(64, 24);&lt;br&gt;&lt;br /&gt;button1.Text = &amp;quot;&amp;lt;&amp;quot;;&lt;br&gt;&lt;br /&gt;button1.Click+=new System.EventHandler(button1_Click);&lt;br&gt;&lt;br /&gt;button2 = new Button();&lt;br&gt;&lt;br /&gt;button2.Location = new System.Drawing.Point(90, 16);&lt;br&gt;&lt;br /&gt;button2.Size = new System.Drawing.Size(64, 24);&lt;br&gt;&lt;br /&gt;button2.Text = &amp;quot;&amp;gt;&amp;quot;;&lt;br&gt;&lt;br /&gt;button2.Click+=new System.EventHandler(button2_Click);&lt;br&gt;&lt;br /&gt;button3 = new Button();&lt;br&gt;&lt;br /&gt;button3.Location = new System.Drawing.Point(90, 100);&lt;br&gt;&lt;br /&gt;button3.Size = new System.Drawing.Size(64, 24);&lt;br&gt;&lt;br /&gt;button3.Text = &amp;quot;&amp;lt;&amp;quot;;&lt;br&gt;&lt;br /&gt;button3.Click+=new System.EventHandler(button3_Click);&lt;br&gt;&lt;br /&gt;button4 = new Button();&lt;br&gt;&lt;br /&gt;button4.Location = new System.Drawing.Point(150, 100);&lt;br&gt;&lt;br /&gt;button4.Size = new System.Drawing.Size(64, 24);&lt;br&gt;&lt;br /&gt;button4.Text = &amp;quot;&amp;gt;&amp;quot;;&lt;br&gt;&lt;br /&gt;button4.Click+=new System.EventHandler(button4_Click);&lt;br&gt;&lt;br /&gt;text1= new TextBox();&lt;br&gt;&lt;br /&gt;text1.Location = new System.Drawing.Point(24, 50);&lt;br&gt;&lt;br /&gt;text1.Size = new System.Drawing.Size(150, 24);&lt;br&gt;&lt;br /&gt;text2= new TextBox();&lt;br&gt;&lt;br /&gt;text2.Location = new System.Drawing.Point(190, 50);&lt;br&gt;&lt;br /&gt;text2.Size = new System.Drawing.Size(150, 24);&lt;br&gt;&lt;br /&gt;text3= new TextBox();&lt;br&gt;&lt;br /&gt;text3.Location = new System.Drawing.Point(290, 150);&lt;br&gt;&lt;br /&gt;text3.Size = new System.Drawing.Size(150, 24);&lt;br&gt;&lt;br /&gt;text4= new TextBox();&lt;br&gt;&lt;br /&gt;text4.Location = new System.Drawing.Point(9, 150);&lt;br&gt;&lt;br /&gt;text4.Size = new System.Drawing.Size(70, 24);&lt;br&gt;&lt;br /&gt;DateTimePicker1 = new DateTimePicker();&lt;br&gt;&lt;br /&gt;DateTimePicker1.Location = new System.Drawing.Point(90, 150);&lt;br&gt;&lt;br /&gt;DateTimePicker1.Size = new System.Drawing.Size(200, 800);&lt;br&gt;&lt;br /&gt;Controls.Add(button1);&lt;br&gt;&lt;br /&gt;Controls.Add(button2);&lt;br&gt;&lt;br /&gt;Controls.Add(button3);&lt;br&gt;&lt;br /&gt;Controls.Add(button4);&lt;br&gt;&lt;br /&gt;Controls.Add(text1);&lt;br&gt;&lt;br /&gt;Controls.Add(text2);&lt;br&gt;&lt;br /&gt;Controls.Add(text3);&lt;br&gt;&lt;br /&gt;Controls.Add(text4);&lt;br&gt;&lt;br /&gt;Controls.Add(DateTimePicker1);&lt;br&gt;&lt;br /&gt;MakeDataSet();&lt;br&gt;&lt;br /&gt;BindControls();&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;public static void Main()&lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;Application.Run(new zzz());&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;The above example utilizes more controls as compared to the earlier ones. It &lt;br /&gt;also exploits a more complicated data source. &lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;&amp;nbsp; 6&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;On the screen, we see 4 buttons in sets of two, which assist the record pointer &lt;br /&gt;in navigating within the data source. We also have three textboxes and a control &lt;br /&gt;named DateTimePicker control, which activates a calendar. We have already &lt;br /&gt;enlightened you on the subject of this control, in the previous chapter. We add &lt;br /&gt;these controls using the Add function. The function MakeDataSet is then employed &lt;br /&gt;to create a complicated relationship between the data.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;Let us take a steal peek into the function MakeDataSet.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;In this function, we create a DataSet object and then pass a string called &lt;br /&gt;myDataSet to the constructor. This string is used to provide a name to the root &lt;br /&gt;document element in the XML representation. Since there is little utility in &lt;br /&gt;assigning a name to the data in this program currently, it can be edged out and &lt;br /&gt;abandoned. A DataSet is a collection of tables in the memory, which can be &lt;br /&gt;related to each other. We can write volumes on the DataSet concept. Microsoft &lt;br /&gt;has pulled out all stops in integrating a large number of features in a DataSet.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;The next task in hand is to associate two DataTables with our DataSet object. &lt;br /&gt;Whenever we require more than one of a similar kind, we need to assign a name to &lt;br /&gt;that entity. So, we create two tables tCust and tOrders with Customers and &lt;br /&gt;Orders. &lt;br&gt;&lt;br /&gt;We create two DataColumn objects named cCustID and cCustName, and add them to &lt;br /&gt;the Columns Collection of our DataTable Customer, using the Add member. The two &lt;br /&gt;columns represent the unique ID and the customer name. &lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;We then add the following three columns to our table called Orders:&lt;br&gt;&lt;br /&gt;• CustID: the id of the customer who bought the order. &lt;br&gt;&lt;br /&gt;• orderDate: the date on which the order was placed. &lt;br&gt;&lt;br /&gt;• OrderAmount: the amount of the order placed. &lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;Once this has been accomplished, we add the two empty tables to the DataSet, &lt;br /&gt;using the Add function from the Tables collection, in the DataSet class. &lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;We now need to relate the two tables, i.e. Customers and Orders. The field &lt;br /&gt;custID in the Customers table is unique for every customer record, and thus, is &lt;br /&gt;called the Primary Key. In the Orders table, the field cID is not unique, as a &lt;br /&gt;customer may place multiple orders. Thus, for every single customer in the &lt;br /&gt;Customer table, we may have multiple records in the Orders table. This type of &lt;br /&gt;relationship is called a parent-child relationship, or a primary key-foreign key &lt;br /&gt;relationship. A field in a table is called a foreign key, only if it is a &lt;br /&gt;primary key in another table, and if both the fields belong to the same domain. &lt;br /&gt;A DataRelation class recognizes a parent-child relationship.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;The constructor of the DataRelation class requires three parameters:&lt;br&gt;&lt;br /&gt;1) The name of the DataRelation, which may be null. &lt;br&gt;&lt;br /&gt;2) The DataColumn object that represents the parent column. In our case, it is &lt;br /&gt;the field cCustID in the Customer table. &lt;br&gt;&lt;br /&gt;3) The child column, i.e. the field cID in the Orders table. &lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;We now use the property named Relations in the DataSet class of type &lt;br /&gt;DataRelationCollection, to Add the relation to the Dataset. From now on, the &lt;br /&gt;DataSet class will relate each customer id from the Customer table to the &lt;br /&gt;multiple customer ids in the Orders table.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;Now, the tables are required to be populated with some data. So, we start by &lt;br /&gt;creating a DataRow object, with the help of which, we shall populate the tables. &lt;br /&gt;Since we want to add three customers with IDs Cust 1, Cust 2 and Cust 3, we &lt;br /&gt;first call the NewRow function, which creates the DataRow object, and then we &lt;br /&gt;use the indexer with a column name to store the data. Using the Add member of &lt;br /&gt;the Row Collection, the row is then added. &lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;Alternatively, we could have initialized the column custName in a for loop. This &lt;br /&gt;approach has been sidestepped or avoided since Microsoft samples have employed a &lt;br /&gt;different methodology. The Rows Collection object, denoted by the property Rows, &lt;br /&gt;has an indexer that facilitates access to each row. For e.g. tCust.Rows[0] &lt;br /&gt;accesses the first row. The DataRow objects indexer can be utilized to change &lt;br /&gt;the field custName.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;For each of the three customers, we would now want to add five records to the &lt;br /&gt;Order table. The Custid and the year of the order date, remain the same. The &lt;br /&gt;month number is suffixed with 1, 2 or 3, depending upon the customer. And the &lt;br /&gt;day is increased by 2, in consideration of each order. The amount on the order &lt;br /&gt;is, the month number multiplied by 10, plus the value of j in the inner for &lt;br /&gt;loop, multiplied by 0.1. This expression generates a unique value for each &lt;br /&gt;order. With the help of the for loop, the process of populating tables can &lt;br /&gt;become reasonably simpler, as against, writing the values individually. Thus, we &lt;br /&gt;have 3 records in the Customer table and 15 records in the Orders table, i.e. 5 &lt;br /&gt;per customer.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;Finally, the controls are bound to the columns in the data table, using the &lt;br /&gt;function BindControls. In the first textbox, we display the field custName from &lt;br /&gt;the Customer table, which is present in the DataSource ds. Currently, it happens &lt;br /&gt;to be a DataSet and not a DataTable or a Collection Object. &lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;The last parameter to the Binding object Constructor is tablename.fieldname. It &lt;br /&gt;is not merely a field name. It is because a DataSet consists of a collection of &lt;br /&gt;tables, and the same field name could be present in more than one table. Thus, &lt;br /&gt;we need to clearly identify the table from which the column has been obtained. &lt;br /&gt;The second textbox is bound to the customer id from the customer table. The &lt;br /&gt;DateTimePicker control behaves akin to other controls, when it comes to data &lt;br /&gt;binding. Instead of the Text property, we bind the control to the Value &lt;br /&gt;property. &lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;The point of greater significance is that, while displaying orderdate, instead &lt;br /&gt;of specifying order.orderdate, we use customers.CustToOrders.OrderDate, which is &lt;br /&gt;the parent-table-name.relation-name.field-name. The motive behind this will be &lt;br /&gt;explained in a short while from now. The date is displayed using a more &lt;br /&gt;sophisticated control than a simple textbox. &lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;Like before, we want to call the code for the fourth textbox, which displays &lt;br /&gt;data that can be edited. So, firstly we are required to create a Binding object &lt;br /&gt;b, and associate two functions with the Format and Parse events. The delegates &lt;br /&gt;are added before the Add function is called, to facilitate the addition of the &lt;br /&gt;bindings, since no formatting takes place when we change the current object in &lt;br /&gt;the DataSource.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;The BindingContext object takes more than one indexer. Therefore, the datasource, &lt;br /&gt;i.e. a DataSet or a navigational path, is used as an indexer to refer to a &lt;br /&gt;specific BindingManagerBase. The above-mentioned object is mandatory in order to &lt;br /&gt;update the record pointer and to display all the data in the data source. This &lt;br /&gt;parameter may either contain merely a table name or a table name followed by a &lt;br /&gt;relation name, e.g. customers.custtoOrders. The first BindingMangerBase object, &lt;br /&gt;bmCustomers, facilitates movement through the three customer records, because we &lt;br /&gt;have supplied the table name Customer. The second one, bmOrders, will move &lt;br /&gt;through those records using the relation, since we have supplied the relation &lt;br /&gt;name. Thus, even though we have 15 records in the Orders table, we can see only &lt;br /&gt;5 of them.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;Thus, whenever we click on the first two buttons, we use the bmCustomers &lt;br /&gt;object's Position property to move from one record to another, up or down, &lt;br /&gt;without checking for errors. The next two buttons also use the Position property &lt;br /&gt;of the bmOrders object, depending upon the active customer id, thus, reducing &lt;br /&gt;the number of records.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;&amp;nbsp; 7&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;The method DecimalToCurrencyString is called every time we desire to display a &lt;br /&gt;new value in the last textbox. The value returned by the DesiredType member of &lt;br /&gt;the ConvertEventArgs parameter, is checked with a string. If they do not &lt;br /&gt;correspond with each other, the program exits out. This is the only error check &lt;br /&gt;performed. Thus, we first ascertain whether we are allowed to convert from a &lt;br /&gt;decimal type in the original value, to a string or not. If the conversion is &lt;br /&gt;allowed, we call the ToString function with the formatting character 'c', to &lt;br /&gt;carry out the actual conversion to a Currency. &lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;The method CurrencyStringToDecimal is called whenever we modify the value in the &lt;br /&gt;textbox. As earlier, we perform the same error check on the conversion to &lt;br /&gt;string, and then we use the Parse event to convert it into the original decimal &lt;br /&gt;type. If we change the display to 20.001, we may see it as 20.00. But the &lt;br /&gt;original value still remains at 20.001. The WriteLine function can be used to &lt;br /&gt;display the unformatted value.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;Finally, the textbox control text4 displays the current customer id. If we move &lt;br /&gt;the top two buttons, the value contained in this textbox changes. However, when &lt;br /&gt;we alter the Position property on the relation, the customer id remains &lt;br /&gt;constant, while the other two fields change. This example illustrates how the &lt;br /&gt;data source can be made extremely complex using the BindingManagerBase class, &lt;br /&gt;which has its work cut out for it.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;&amp;nbsp; 8&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;a.cs&lt;br&gt;&lt;br /&gt;using System;&lt;br&gt;&lt;br /&gt;using System.Drawing;&lt;br&gt;&lt;br /&gt;using System.Windows.Forms;&lt;br&gt;&lt;br /&gt;using System.Data;&lt;br&gt;&lt;br /&gt;using System.Data.SqlClient;&lt;br&gt;&lt;br /&gt;public struct State1 &lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;string shortName, longName;&lt;br&gt;&lt;br /&gt;public State1(string longName , string shortName) &lt;br&gt;&lt;br /&gt;{ &lt;br&gt;&lt;br /&gt;this.shortName = shortName ; this.longName = longName ;&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;public string ShortName &lt;br&gt;&lt;br /&gt;{ &lt;br&gt;&lt;br /&gt;get &lt;br&gt;&lt;br /&gt;{ &lt;br&gt;&lt;br /&gt;return shortName; &lt;br&gt;&lt;br /&gt;} &lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;public string LongName &lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;get&lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;return longName;&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;public class zzz : Form &lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;int cnt;&lt;br&gt;&lt;br /&gt;DataSet customersDataSet1;&lt;br&gt;&lt;br /&gt;ComboBox comboBoxState;&lt;br&gt;&lt;br /&gt;TextBox textBoxPosition;&lt;br&gt;&lt;br /&gt;Button buttonMoveFirst,buttonMovePrev,buttonMoveNext,&lt;br&gt;&lt;br /&gt;buttonMoveLast;&lt;br&gt;&lt;br /&gt;TextBox textBoxID,textBoxRegion;&lt;br&gt;&lt;br /&gt;Label labelID;&lt;br&gt;&lt;br /&gt;Panel panelVCRControl;&lt;br&gt;&lt;br /&gt;// Washington not there&lt;br&gt;&lt;br /&gt;public State1[] States = new State1[] &lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;new State1(&amp;quot;Alaska&amp;quot;,&amp;quot;AK&amp;quot;),new State1(&amp;quot;California&amp;quot; ,&amp;quot;CA&amp;quot;),new State1(&amp;quot;Idaho&amp;quot;,&amp;quot;ID&amp;quot;),new &lt;br /&gt;State1(&amp;quot;Montana&amp;quot; ,&amp;quot;MT&amp;quot;),&lt;br&gt;&lt;br /&gt;new State1(&amp;quot;New Mexico&amp;quot; ,&amp;quot;NM&amp;quot;),new State1(&amp;quot;Oregon&amp;quot; ,&amp;quot;OR&amp;quot;)&lt;br&gt;&lt;br /&gt;,new State1(&amp;quot;Wyoming&amp;quot; ,&amp;quot;WY&amp;quot;)&lt;br&gt;&lt;br /&gt;} ;&lt;br&gt;&lt;br /&gt;public zzz() &lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;buttonMoveLast = new Button();&lt;br&gt;&lt;br /&gt;customersDataSet1 = new DataSet();&lt;br&gt;&lt;br /&gt;buttonMoveFirst = new Button();&lt;br&gt;&lt;br /&gt;textBoxID = new TextBox();&lt;br&gt;&lt;br /&gt;textBoxRegion = new TextBox();&lt;br&gt;&lt;br /&gt;textBoxPosition = new TextBox();&lt;br&gt;&lt;br /&gt;buttonMovePrev = new Button();&lt;br&gt;&lt;br /&gt;panelVCRControl = new Panel();&lt;br&gt;&lt;br /&gt;comboBoxState = new ComboBox();&lt;br&gt;&lt;br /&gt;labelID = new Label();&lt;br&gt;&lt;br /&gt;buttonMoveNext = new Button();&lt;br&gt;&lt;br /&gt;buttonMoveNext.Click += new System.EventHandler(buttonMoveNext_Click);&lt;br&gt;&lt;br /&gt;buttonMoveNext.FlatStyle = FlatStyle.Flat;&lt;br&gt;&lt;br /&gt;buttonMoveNext.Location = new System.Drawing.Point(280, 8);&lt;br&gt;&lt;br /&gt;buttonMoveNext.Size = new System.Drawing.Size(32, 32);&lt;br&gt;&lt;br /&gt;buttonMoveNext.Text = &amp;quot;&amp;gt;&amp;quot;;&lt;br&gt;&lt;br /&gt;customersDataSet1.DataSetName = &amp;quot;CustomersDataSet&amp;quot;;&lt;br&gt;&lt;br /&gt;buttonMoveFirst.FlatStyle = FlatStyle.Flat;&lt;br&gt;&lt;br /&gt;buttonMoveFirst.Click += new System.EventHandler(buttonMoveFirst_Click);&lt;br&gt;&lt;br /&gt;buttonMoveFirst.Location = new System.Drawing.Point(8, 8);&lt;br&gt;&lt;br /&gt;buttonMoveFirst.Size = new System.Drawing.Size(32, 32);&lt;br&gt;&lt;br /&gt;buttonMoveFirst.Text = &amp;quot;|&amp;lt;&amp;quot;;&lt;br&gt;&lt;br /&gt;textBoxID.Enabled = false;&lt;br&gt;&lt;br /&gt;textBoxID.Location = new System.Drawing.Point(88, 16);&lt;br&gt;&lt;br /&gt;textBoxID.ReadOnly = true;&lt;br&gt;&lt;br /&gt;textBoxID.Size = new System.Drawing.Size(299, 20);&lt;br&gt;&lt;br /&gt;textBoxRegion.Location = new Point(88, 116);&lt;br&gt;&lt;br /&gt;textBoxRegion.Size = new System.Drawing.Size(299, 20);&lt;br&gt;&lt;br /&gt;ClientSize = new System.Drawing.Size(464, 357);&lt;br&gt;&lt;br /&gt;Text = &amp;quot;Customer Details&amp;quot;;&lt;br&gt;&lt;br /&gt;textBoxPosition.Enabled = false;&lt;br&gt;&lt;br /&gt;textBoxPosition.Location = new System.Drawing.Point(88, 14);&lt;br&gt;&lt;br /&gt;textBoxPosition.ReadOnly = true;&lt;br&gt;&lt;br /&gt;textBoxPosition.Size = new System.Drawing.Size(184, 20);&lt;br&gt;&lt;br /&gt;buttonMovePrev.Click += new System.EventHandler(buttonMovePrev_Click);&lt;br&gt;&lt;br /&gt;buttonMovePrev.FlatStyle = FlatStyle.Flat;&lt;br&gt;&lt;br /&gt;buttonMovePrev.Location = new System.Drawing.Point(48, 8);&lt;br&gt;&lt;br /&gt;buttonMovePrev.Size = new System.Drawing.Size(32, 32);&lt;br&gt;&lt;br /&gt;buttonMovePrev.Text = &amp;quot;&amp;lt;&amp;quot;;&lt;br&gt;&lt;br /&gt;panelVCRControl.Location = new System.Drawing.Point(88, 288);&lt;br&gt;&lt;br /&gt;panelVCRControl.Size = new System.Drawing.Size(360, 48);&lt;br&gt;&lt;br /&gt;comboBoxState.Location = new System.Drawing.Point(88, 208);&lt;br&gt;&lt;br /&gt;comboBoxState.Size = new System.Drawing.Size(176, 20);&lt;br&gt;&lt;br /&gt;comboBoxState.Text = &amp;quot;&amp;quot;;&lt;br&gt;&lt;br /&gt;labelID.Location = new System.Drawing.Point(16, 16);&lt;br&gt;&lt;br /&gt;labelID.Size = new System.Drawing.Size(64, 16);&lt;br&gt;&lt;br /&gt;labelID.Text = &amp;quot;ID:&amp;quot;;&lt;br&gt;&lt;br /&gt;buttonMoveLast.Click += new System.EventHandler(buttonMoveLast_Click);&lt;br&gt;&lt;br /&gt;buttonMoveLast.FlatStyle = FlatStyle.Flat;&lt;br&gt;&lt;br /&gt;buttonMoveLast.Location = new System.Drawing.Point(320, 8);&lt;br&gt;&lt;br /&gt;buttonMoveLast.Size = new System.Drawing.Size(32, 32);&lt;br&gt;&lt;br /&gt;buttonMoveLast.Text = &amp;quot;&amp;gt;|&amp;quot;;&lt;br&gt;&lt;br /&gt;panelVCRControl.Controls.AddRange(new Control[] {textBoxPosition,buttonMoveFirst,buttonMovePrev,&lt;br&gt;&lt;br /&gt;buttonMoveNext,buttonMoveLast});&lt;br&gt;&lt;br /&gt;Controls.AddRange(new Control[] {comboBoxState,panelVCRControl,textBoxID,labelID,textBoxRegion});&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;SqlConnection con = new SqlConnection(&amp;quot;server=(local)\\NetSDK;&lt;br&gt;&lt;br /&gt;uid=QSUser;pwd=QSPassword;database=northwind&amp;quot;);&lt;br&gt;&lt;br /&gt;SqlDataAdapter cmd = new SqlDataAdapter(&amp;quot;Select * from Customers where &lt;br /&gt;country='USA'&amp;quot;, con);&lt;br&gt;&lt;br /&gt;cmd.Fill(customersDataSet1, &amp;quot;Customers&amp;quot;);&lt;br&gt;&lt;br /&gt;comboBoxState.DataSource=States; &lt;br&gt;&lt;br /&gt;comboBoxState.DisplayMember=&amp;quot;LongName&amp;quot;; &lt;br&gt;&lt;br /&gt;comboBoxState.ValueMember=&amp;quot;ShortName&amp;quot;; &lt;br&gt;&lt;br /&gt;comboBoxState.DataBindings.Add(&amp;quot;SelectedValue&amp;quot;, customersDataSet1, &amp;quot;Customers.Region&amp;quot;);&lt;br&gt;&lt;br /&gt;textBoxID.DataBindings.Add(&amp;quot;Text&amp;quot;, customersDataSet1, &amp;quot;Customers.CustomerID&amp;quot;);&lt;br&gt;&lt;br /&gt;textBoxRegion.DataBindings.Add(&amp;quot;Text&amp;quot;, customersDataSet1, &amp;quot;Customers.Region&amp;quot;);&lt;br&gt;&lt;br /&gt;BindingContext[customersDataSet1,&amp;quot;Customers&amp;quot;].PositionChanged += new &lt;br /&gt;System.EventHandler(customers_PositionChanged);&lt;br&gt;&lt;br /&gt;DataTableCollection tc = customersDataSet1.Tables;&lt;br&gt;&lt;br /&gt;DataTable t = tc[0];&lt;br&gt;&lt;br /&gt;cnt = t.Rows.Count;&lt;br&gt;&lt;br /&gt;textBoxPosition.Text = &amp;quot;Record &amp;quot; + (BindingContext[customersDataSet1,&amp;quot;Customers&amp;quot;].Position &lt;br /&gt;+ 1) + &amp;quot; of &amp;quot; + cnt;&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;void buttonMoveFirst_Click(object sender, System.EventArgs e) &lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;BindingContext[customersDataSet1,&amp;quot;Customers&amp;quot;].Position = 0 ;&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;void buttonMoveLast_Click(object sender, System.EventArgs e) &lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;BindingContext[customersDataSet1,&amp;quot;Customers&amp;quot;].Position = cnt - 1;&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;void buttonMoveNext_Click(object sender, System.EventArgs e) &lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;if (BindingContext[customersDataSet1,&amp;quot;Customers&amp;quot;].Position &amp;lt; cnt - 1) &lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;BindingContext[customersDataSet1,&amp;quot;Customers&amp;quot;].Position++;&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;void buttonMovePrev_Click(object sender, System.EventArgs e) &lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;if (BindingContext[customersDataSet1,&amp;quot;Customers&amp;quot;].Position &amp;gt; 0) &lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;BindingContext[customersDataSet1,&amp;quot;Customers&amp;quot;].Position--;&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;void customers_PositionChanged(object sender, System.EventArgs e) &lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;textBoxPosition.Text = &amp;quot;Record &amp;quot; + (BindingContext[customersDataSet1,&amp;quot;Customers&amp;quot;].Position &lt;br /&gt;+ 1) + &amp;quot; of &amp;quot; + cnt;&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;public static void Main() &lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;Application.Run(new zzz());&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;&amp;nbsp; 9&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;Continuing with our saga on data handling, let us launch a few more intriguing &lt;br /&gt;twists and turns. As before, let us tackle the User Interface issues first. We &lt;br /&gt;possess one label, three textboxes, one combo box and four buttons. There is &lt;br /&gt;nothing novel or innovative about this. In the earlier programs, we had entered &lt;br /&gt;the data in our program itself, using a DataTable or a DataSet, and this data &lt;br /&gt;was eventually displayed. In this program, we source the data from a database.&lt;br /&gt;&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;When we install the .NET framework, a large number of databases are brought into &lt;br /&gt;existence and installed in SQL Server. To access the data within a database, we &lt;br /&gt;have to use the SqlConnection class. The constructor is given a string that &lt;br /&gt;identifies the machine on which the database server resides. The default in our &lt;br /&gt;case is server=(local)\\NetSDK, where 'local' represents the machine we are &lt;br /&gt;currently working on. The installation program creates NetSDK. The word &lt;br /&gt;following server is 'uid', which denotes the user name, and the word 'pwd' &lt;br /&gt;implies the password. The values supplied are QSUser and QSPassword, &lt;br /&gt;respectively. &lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;All data is stored in tables that reside in a database. We are interested in a &lt;br /&gt;list of customers, which is stored in a table called Customers, residing in the &lt;br /&gt;northwind database. Therefore, in the connection string, we specify database = &lt;br /&gt;northwind.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;The SqlConnection class is merely capable of comprehending the wherewithal of &lt;br /&gt;connecting to a database. It is clueless about the word SQL or Structured Query &lt;br /&gt;Language. SQL is a language used to extricate data from one or more tables. &lt;br /&gt;Thus, we introduce a new class named SqlDataAdapter that understands SQL, and &lt;br /&gt;then, we pass the SQL statement to its constructor along with the connection &lt;br /&gt;object. The SQL statement &amp;quot;Select * from Customers where country = 'USA' &amp;quot;, &lt;br /&gt;selects all the fields, since the symbol * represents all fields of the &lt;br /&gt;Customers table. The 'where' condition restricts/filters records whose country &lt;br /&gt;field has the value of 'USA'. As of now, no data gets retrieved. The class &lt;br /&gt;stores this information internally.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;It is the Fill command of the SqlDataAdapter class, which is responsible for &lt;br /&gt;filling up the DataSet customersDataSet1. The first parameter supplied is the &lt;br /&gt;DataSet and the second parameter is a tablename whose fields are to be mapped. &lt;br /&gt;The tablename has to be a valid table name, or else, a run time exception will &lt;br /&gt;be generated. The return value is the number of rows that are present in the &lt;br /&gt;data source. Earlier, we had used the 'for' statements to provide data; whereas, &lt;br /&gt;currently we are using real life data from a database to load a table. The &lt;br /&gt;WriteLine function if given here would display the number of records in the &lt;br /&gt;DataSet, which is 13 in this case.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;The DataSource property in the Combo Box control is of type object. This &lt;br /&gt;property determines what the combo box displays. Here, we have specified an &lt;br /&gt;array called States, of data type State1. Our array contains seven members of &lt;br /&gt;type State1. &lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;The State1 object has two members, viz. longName and shortName. LongName stores &lt;br /&gt;the actual name of the state and shortName stores the two-character &lt;br /&gt;abbreviations. The constructor of the class initializes these two members. &lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;It is our misfortune that, despite being tantalized by being offered the &lt;br /&gt;exciting privilege of selecting names for parameters, the same old names as &lt;br /&gt;assigned to the fields, are being ascribed to the parameters as well. Therefore, &lt;br /&gt;to access the field shortName from within the constructor, we need to preface it &lt;br /&gt;with the word 'this'. The 'this' keyword is optional in situations where we use &lt;br /&gt;different names for the parameters. Thus, the combo box shall display one of the &lt;br /&gt;values present in the States array, which encompasses the short and the long &lt;br /&gt;names of seven different states. To authenticate this, you may click on the down &lt;br /&gt;arrow and see the names of the seven states. &lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;&amp;nbsp; 10&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;The question that comes to the fore at this stage is that, 'How does the combo &lt;br /&gt;box come to a decision on the values to be displayed?' The combo box or any &lt;br /&gt;control that displays a list, takes two different values, namely, DisplayMember &lt;br /&gt;and ValueMember. DisplayMember takes a decision on the data that the user sees &lt;br /&gt;in the list box, while ValueMember is the actual value of the selected item. &lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;Thus in our case, we get to see the full name of the state, when the &lt;br /&gt;DisplayMember property is initialized to longName. The ValueMember is equated to &lt;br /&gt;the shortName. So, the value obtained from this list box is the abbreviated name &lt;br /&gt;of a state. These two have to be properties in the class State1.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;The combo box, like all other controls, has a Binding property. The first two &lt;br /&gt;textboxes are bound to the CustomerID field and the Region field. They are &lt;br /&gt;preceded by the table-name. The name of the DataSet is also specified. The field &lt;br /&gt;from the dataset is the Region field that contains the abbreviation, but we get &lt;br /&gt;to see the full name, since the DisplayMember is longName.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;Let us now work under the assumption that the current value of the region field &lt;br /&gt;is NM. So, the combo box shall display New Mexico, and not NM. &lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;&amp;nbsp; 11&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;The framework embarks on its search from the beginning of the States array, and &lt;br /&gt;then calls the property shortName. It verifies every entry in the array, in &lt;br /&gt;order to confirm whether the value is 'NM' or otherwise. Once the value matches, &lt;br /&gt;it calls the property longName to display the full name in the combo box. &lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;Thus, if the desired shortName were at a position that is deep down the array, &lt;br /&gt;it would entail summoning the shortName property numerous times. The long name &lt;br /&gt;property, for reasons unknown, gets called only twice. The state Washington is &lt;br /&gt;not present in the array. In a situation like this, the framework moves through &lt;br /&gt;all the members in the array. If no match is found, it displays the first member &lt;br /&gt;of the array, which in this case is Alaska. &lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;To unravel the number of records in the table, we first need to access the &lt;br /&gt;DataTableCollection object. This is accomplished using the Tables property, &lt;br /&gt;which results in a collection. Thereafter, the tables are accessed, using the &lt;br /&gt;indexer. In order to access the table Customers, tc[0] is used. Subsequently, &lt;br /&gt;the Count property of the Rows collection is used to return the number of rows &lt;br /&gt;in the table.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;The Binding Manager code, which is essential to move the active object, remains &lt;br /&gt;the same. So, we shall not delve upon it any further. All that we are trying to &lt;br /&gt;explain through the above example is that, by using one value in a database, we &lt;br /&gt;can display another value. &lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;You need to commit it to your memory that, we are binding to a combo box for the &lt;br /&gt;first time ever. The combo box is bound to the SelectedValue property. If you &lt;br /&gt;comment out this binding, you will observe that the values remain impervious or &lt;br /&gt;unaltered. Further, if you click in the combo box, you will witness the display &lt;br /&gt;of the names of all the states. &lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;Data Grids &lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;a.cs&lt;br&gt;&lt;br /&gt;using System;&lt;br&gt;&lt;br /&gt;using System.Data;&lt;br&gt;&lt;br /&gt;using System.Drawing;&lt;br&gt;&lt;br /&gt;using System.Windows.Forms;&lt;br&gt;&lt;br /&gt;using System.Data.SqlClient;&lt;br&gt;&lt;br /&gt;public class zzz : Form &lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;DataGrid d;&lt;br&gt;&lt;br /&gt;DataSet c;&lt;br&gt;&lt;br /&gt;public zzz() &lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;d = new DataGrid();&lt;br&gt;&lt;br /&gt;d.Size = new Size(584, 336);&lt;br&gt;&lt;br /&gt;d.DataMember = &amp;quot;Customers1&amp;quot;;&lt;br&gt;&lt;br /&gt;ClientSize = new Size(600, 413);&lt;br&gt;&lt;br /&gt;SqlConnection con = new SqlConnection(&amp;quot;server=(local)\\NetSDK;&lt;br&gt;&lt;br /&gt;uid=QSUser;pwd=QSPassword;database=northwind&amp;quot;);&lt;br&gt;&lt;br /&gt;SqlDataAdapter Cust = new SqlDataAdapter (&amp;quot;Select * from Customers&amp;quot;, con);&lt;br&gt;&lt;br /&gt;c = new DataSet();&lt;br&gt;&lt;br /&gt;d.DataSource = c;&lt;br&gt;&lt;br /&gt;Cust.Fill(c, &amp;quot;Customers1&amp;quot;);&lt;br&gt;&lt;br /&gt;d.DataMember = &amp;quot;Customers1&amp;quot;;&lt;br&gt;&lt;br /&gt;Controls.Add(d);&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;public static void Main() &lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;Application.Run(new zzz());&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;One of the most common uses of data is to position it in a tabular form. This &lt;br /&gt;format can be achieved by using a DataGrid control, which is a collection of &lt;br /&gt;columns and rows. The above program displays data from the Customer table in a &lt;br /&gt;data grid object.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;&amp;nbsp; 12&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;We commence by creating a DataGrid object d, and assign it a certain size using &lt;br /&gt;the Size property. As earlier, we create a SqlConnection object to connect to &lt;br /&gt;the database server on a machine, and then, create a SqlDataAdaptor object Cust &lt;br /&gt;to represent all the data from the Customers table. &lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;The DataSource property of the DataGrid is initialized to a freshly created, &lt;br /&gt;albeit, empty DataSet object, c. This is because a DataGrid displays data from a &lt;br /&gt;source, and therefore, the DataSource property is specifically introduced to &lt;br /&gt;identify the source. Dataset is not the only medium, since the data source can &lt;br /&gt;obtain a value from seven different entities. We shall explore this in greater &lt;br /&gt;detail in the next example. &lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;Using the Fill function of the SqlDataAdaptor class, the DataSet 'c' is packed &lt;br /&gt;with data. You can assign any name to it. We have used Customer1 mainly to &lt;br /&gt;facilitate source mapping. Bear in mind that the DataSet, by itself, is devoid &lt;br /&gt;of any data. It comprises of other sources, which in turn, contain data. This &lt;br /&gt;highlights the fact that a DataSet contains supplementary data sources. This &lt;br /&gt;function is obtained from the DbDataAdapter class. &lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;Finally, the DataSource for the DataGrid class is to be specified. As we have &lt;br /&gt;only one source i.e. Customers1, we have initialized the DataMember property to &lt;br /&gt;it. Had the DataMember property not been supplied with a value, i.e. &lt;br /&gt;d.DataMember = &amp;quot;&amp;quot;, we would not have seen any data on start up. At this stage, a &lt;br /&gt;plus sign would be displayed in the empty grid. Clicking on the plus sign would &lt;br /&gt;then show the name of our solitary source, Customer1. This is displayed as a &lt;br /&gt;hyperlink. When we click on the link, in addition to the new dataset being &lt;br /&gt;displayed in the heading of the data grid, the previous data is also displayed.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;&amp;nbsp; 13&amp;nbsp;&amp;nbsp; 14&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;If we add the line Cust.Fill(c, &amp;quot;Customers2&amp;quot;) immediately after the first Fill &lt;br /&gt;function, it results in the creation of a second source. Thus, at this stage, &lt;br /&gt;two sources are present. &lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;&amp;nbsp; 15&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;Therefore, clicking on the + sign in the DataGrid at this stage would display &lt;br /&gt;two different sources as hyperlinks. Clicking on either of the hyperlinks will &lt;br /&gt;exhibit the same set of data. The point worthy of notice is that, we can let the &lt;br /&gt;user dynamically choose the source that he wishes to work with.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;a.cs&lt;br&gt;&lt;br /&gt;using System;&lt;br&gt;&lt;br /&gt;using System.Data;&lt;br&gt;&lt;br /&gt;using System.Drawing;&lt;br&gt;&lt;br /&gt;using System.Windows.Forms;&lt;br&gt;&lt;br /&gt;using System.Data.SqlClient;&lt;br&gt;&lt;br /&gt;public class zzz : Form &lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;DataGrid d;&lt;br&gt;&lt;br /&gt;public zzz() &lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;d = new DataGrid();&lt;br&gt;&lt;br /&gt;d.Size = new Size(584, 336);&lt;br&gt;&lt;br /&gt;ClientSize = new Size(600, 413);&lt;br&gt;&lt;br /&gt;DataTable t = new DataTable(&amp;quot;Control&amp;quot;);&lt;br&gt;&lt;br /&gt;t.Columns.Add(&amp;quot;Name&amp;quot;);&lt;br&gt;&lt;br /&gt;t.Columns.Add(&amp;quot;City&amp;quot;);&lt;br&gt;&lt;br /&gt;DataRow r;&lt;br&gt;&lt;br /&gt;r = t.NewRow();&lt;br&gt;&lt;br /&gt;r[&amp;quot;Name&amp;quot;] = &amp;quot;Vijay&amp;quot;;&lt;br&gt;&lt;br /&gt;r[&amp;quot;City&amp;quot;] = &amp;quot;Bombay&amp;quot;;&lt;br&gt;&lt;br /&gt;t.Rows.Add(r);&lt;br&gt;&lt;br /&gt;r = t.NewRow();&lt;br&gt;&lt;br /&gt;r[&amp;quot;Name&amp;quot;] = &amp;quot;Sonal&amp;quot;;&lt;br&gt;&lt;br /&gt;r[&amp;quot;City&amp;quot;] = &amp;quot;Delhi&amp;quot;;&lt;br&gt;&lt;br /&gt;t.Rows.Add(r);&lt;br&gt;&lt;br /&gt;d.DataSource = t;&lt;br&gt;&lt;br /&gt;Controls.Add(d);&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;public static void Main() &lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;Application.Run(new zzz());&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;As we had mentioned earlier, the data source should essentially be dynamic since &lt;br /&gt;data comes in different shapes and sizes. &lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;In this program, we create a simple DataTable t with two columns named Name and &lt;br /&gt;City. This DataTable is then supplied as the DataSource. Since we are making use &lt;br /&gt;of an entity that contains data, we do not have to specify the DataMember &lt;br /&gt;explicitly. &lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;&amp;nbsp; 16&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;This results in the display of two records in the DataGrid control. &lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;The DataTable could also have been initialized by sourcing data from a database &lt;br /&gt;using the SqlDataAdaptor class.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;a.cs&lt;br&gt;&lt;br /&gt;using System;&lt;br&gt;&lt;br /&gt;using System.Data;&lt;br&gt;&lt;br /&gt;using System.Drawing;&lt;br&gt;&lt;br /&gt;using System.Windows.Forms;&lt;br&gt;&lt;br /&gt;using System.Data.SqlClient;&lt;br&gt;&lt;br /&gt;using System.Collections;&lt;br&gt;&lt;br /&gt;public class CustomerList : System.Collections.CollectionBase {&lt;br&gt;&lt;br /&gt;public static CustomerList GetCustomers() {&lt;br&gt;&lt;br /&gt;CustomerList cl = new CustomerList();&lt;br&gt;&lt;br /&gt;Customer a = Customer.ReadCustomer1();&lt;br&gt;&lt;br /&gt;IList b = cl.List;&lt;br&gt;&lt;br /&gt;b.Add(a);&lt;br&gt;&lt;br /&gt;Customer c = new Customer(&amp;quot;246-12-5645&amp;quot;);&lt;br&gt;&lt;br /&gt;c.FirstName = &amp;quot;Vijay&amp;quot;;&lt;br&gt;&lt;br /&gt;c.DateOfBirth = DateTime.Parse(&amp;quot;5/3/1933&amp;quot;);&lt;br&gt;&lt;br /&gt;cl.Add1(c);&lt;br&gt;&lt;br /&gt;cl.Add1(Customer.ReadCustomer2());&lt;br&gt;&lt;br /&gt;return cl;&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;public int Add1(Customer value) {&lt;br&gt;&lt;br /&gt;return List.Add(value);&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;public class Customer {&lt;br&gt;&lt;br /&gt;public string id,FirstName ;&lt;br&gt;&lt;br /&gt;public DateTime dateOfBirth;&lt;br&gt;&lt;br /&gt;public static Customer ReadCustomer1() &lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;Customer cust = new Customer(&amp;quot;536-45-1245&amp;quot;);&lt;br&gt;&lt;br /&gt;cust.FirstName = &amp;quot;Sonal&amp;quot;;&lt;br&gt;&lt;br /&gt;cust.DateOfBirth = DateTime.Parse(&amp;quot;9/9/1941&amp;quot;);&lt;br&gt;&lt;br /&gt;return cust;&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;public static Customer ReadCustomer2() &lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;Customer cust = new Customer(&amp;quot;651-27-8117&amp;quot;);&lt;br&gt;&lt;br /&gt;cust.FirstName = &amp;quot;Manish&amp;quot;;&lt;br&gt;&lt;br /&gt;cust.DateOfBirth = DateTime.Parse(&amp;quot;3/25/1942&amp;quot;);&lt;br&gt;&lt;br /&gt;return cust;&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;public Customer(string ID): base() &lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;id = ID ;&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;public string ID &lt;br&gt;&lt;br /&gt;{ &lt;br&gt;&lt;br /&gt;get &lt;br&gt;&lt;br /&gt;{ &lt;br&gt;&lt;br /&gt;return id ;&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;public string FirstName1 &lt;br&gt;&lt;br /&gt;{ &lt;br&gt;&lt;br /&gt;get &lt;br&gt;&lt;br /&gt;{ &lt;br&gt;&lt;br /&gt;return FirstName ;&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;public DateTime DateOfBirth&lt;br&gt;&lt;br /&gt;{ &lt;br&gt;&lt;br /&gt;get &lt;br&gt;&lt;br /&gt;{ &lt;br&gt;&lt;br /&gt;return dateOfBirth ;&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;set &lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;dateOfBirth = value ;&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;public class zzz : Form {&lt;br&gt;&lt;br /&gt;DataGrid d;&lt;br&gt;&lt;br /&gt;public zzz() {&lt;br&gt;&lt;br /&gt;d = new DataGrid();&lt;br&gt;&lt;br /&gt;d.Size = new Size(584, 336);&lt;br&gt;&lt;br /&gt;ClientSize = new Size(600, 413);&lt;br&gt;&lt;br /&gt;CustomerList custList;&lt;br&gt;&lt;br /&gt;custList = CustomerList.GetCustomers();&lt;br&gt;&lt;br /&gt;d.DataSource = custList ;&lt;br&gt;&lt;br /&gt;Controls.Add(d);&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;public static void Main() {&lt;br&gt;&lt;br /&gt;Application.Run(new zzz());&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;A DataSource for a DataGrid can comprise of seven different types of Data &lt;br /&gt;sources. These are as follows:&lt;br&gt;&lt;br /&gt;• DataTable&lt;br&gt;&lt;br /&gt;• DataView&lt;br&gt;&lt;br /&gt;• DataSet&lt;br&gt;&lt;br /&gt;• DataViewManager&lt;br&gt;&lt;br /&gt;• Single dimensional array&lt;br&gt;&lt;br /&gt;• IList interface &lt;br&gt;&lt;br /&gt;• An object that implements the IListSource&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;&amp;nbsp; 17&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;The above example is merely a replica of the first example of this Chapter, &lt;br /&gt;where we had used a class derived from CollectionBase. The same rules as &lt;br /&gt;mentioned in the first example shall be applicable here also.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;For e.g. the presence of properties that represent column names, etc. As we have &lt;br /&gt;only three properties, only three columns are displayed in the DataGrid. This &lt;br /&gt;program also illustrates the utilization of the varied data sources in a data &lt;br /&gt;grid.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;a.cs&lt;br&gt;&lt;br /&gt;using System;&lt;br&gt;&lt;br /&gt;using System.Data;&lt;br&gt;&lt;br /&gt;using System.Drawing;&lt;br&gt;&lt;br /&gt;using System.Windows.Forms;&lt;br&gt;&lt;br /&gt;using System.Data.SqlClient;&lt;br&gt;&lt;br /&gt;public class zzz : Form {&lt;br&gt;&lt;br /&gt;DataGrid d;&lt;br&gt;&lt;br /&gt;DataSet c;&lt;br&gt;&lt;br /&gt;public zzz() &lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;d = new DataGrid();&lt;br&gt;&lt;br /&gt;d.Size = new Size(584, 336);&lt;br&gt;&lt;br /&gt;d.DataMember = &amp;quot;Customers1&amp;quot;;&lt;br&gt;&lt;br /&gt;ClientSize = new Size(600, 413);&lt;br&gt;&lt;br /&gt;SqlConnection con = new SqlConnection(&amp;quot;server=(local)\\NetSDK;&lt;br&gt;&lt;br /&gt;uid=QSUser;pwd=QSPassword;database=northwind&amp;quot;);&lt;br&gt;&lt;br /&gt;SqlDataAdapter Cust = new SqlDataAdapter (&amp;quot;Select * from Customers&amp;quot;, con);&lt;br&gt;&lt;br /&gt;c = new DataSet();&lt;br&gt;&lt;br /&gt;d.DataSource = c;&lt;br&gt;&lt;br /&gt;Cust.Fill(c, &amp;quot;Customers1&amp;quot;);&lt;br&gt;&lt;br /&gt;d.DataMember = &amp;quot;Customers1&amp;quot;;&lt;br&gt;&lt;br /&gt;d.AlternatingBackColor = Color.Red;&lt;br&gt;&lt;br /&gt;d.BackColor = Color.Blue;&lt;br&gt;&lt;br /&gt;d.BackgroundColor = Color.Green;&lt;br&gt;&lt;br /&gt;Rectangle r = d.Bounds;&lt;br&gt;&lt;br /&gt;System.Console.WriteLine(r);&lt;br&gt;&lt;br /&gt;r = new Rectangle(1,100,200,400);&lt;br&gt;&lt;br /&gt;d.Bounds = r;&lt;br&gt;&lt;br /&gt;Controls.Add(d);&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;public static void Main() &lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;Application.Run(new zzz());&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;&amp;nbsp; 18&amp;nbsp;&amp;nbsp; 19&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;In the above example, we have a large number of properties whose values can be &lt;br /&gt;altered. Within a DataGrid, if we click on a column, a sort is performed on the &lt;br /&gt;values in the column. If the column is clicked again, the sort order is &lt;br /&gt;reversed. The column which decides the sorting has an arrow displayed next to &lt;br /&gt;it. &lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;Sorting is enabled by default and can be disabled by initializing the property &lt;br /&gt;AllowSorting to False. However, there is no way of disabling the facility to &lt;br /&gt;sort on a single column. Provision is available to sort on an expression.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;The property AlternatingBackColor bestows a ledger-like appearance to our Grid. &lt;br /&gt;The background color of every alternate row is of a specific shade. In this &lt;br /&gt;case, it is red. The BackColor property of the DataGrid control, which bestows &lt;br /&gt;every row with the same background color, is set to blue. The default color for &lt;br /&gt;this property is the system color of Windows. Setting the BackColor property to &lt;br /&gt;Color.Empty switches the color mode to the default color. Thus, we observe that &lt;br /&gt;the first row and every alternate row thereafter are blue in color, and the &lt;br /&gt;remaining rows are red in color. &lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;There is a narrow tract at the bottom of the grid, which is displayed in green &lt;br /&gt;color. This color is determined by the value assigned to the property &lt;br /&gt;BackgroundColor. This narrow strip, which is part of the non-row area of the &lt;br /&gt;grid, is distinctly visible when the grid comprises of only a few rows, or when &lt;br /&gt;there is no table to be displayed in the grid.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;The size of the DataGrid control can be controlled programmatically. The Bounds &lt;br /&gt;property in the DataGrid is a read-write property. Hence, the default rectangle &lt;br /&gt;structure, which contains the DataGrid, can be displayed using the WriteLine &lt;br /&gt;function. The X and Y co-ordinates are specified as 0,0, the Width is 584 pixels &lt;br /&gt;and the Height is 336 pixels. &lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;Thus, it is evident that we have the discretion to determine the size of the &lt;br /&gt;DataGrid, since we have to share real estate on our Window with other controls.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;a.cs&lt;br&gt;&lt;br /&gt;using System;&lt;br&gt;&lt;br /&gt;using System.Data;&lt;br&gt;&lt;br /&gt;using System.Drawing;&lt;br&gt;&lt;br /&gt;using System.Windows.Forms;&lt;br&gt;&lt;br /&gt;using System.Data.SqlClient;&lt;br&gt;&lt;br /&gt;public class zzz : Form &lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;DataGrid d;&lt;br&gt;&lt;br /&gt;DataSet c;&lt;br&gt;&lt;br /&gt;public zzz() &lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;d = new DataGrid();&lt;br&gt;&lt;br /&gt;d.Size = new Size(584, 336);&lt;br&gt;&lt;br /&gt;d.DataMember = &amp;quot;Customers1&amp;quot;;&lt;br&gt;&lt;br /&gt;ClientSize = new Size(600, 413);&lt;br&gt;&lt;br /&gt;SqlConnection con = new SqlConnection(&amp;quot;server=(local)\\NetSDK;uid=QSUser;&lt;br&gt;&lt;br /&gt;pwd=QSPassword;database=northwind&amp;quot;);&lt;br&gt;&lt;br /&gt;SqlDataAdapter Cust = new SqlDataAdapter (&amp;quot;Select * from Customers&amp;quot;, con);&lt;br&gt;&lt;br /&gt;c = new DataSet();&lt;br&gt;&lt;br /&gt;d.DataSource = c;&lt;br&gt;&lt;br /&gt;Cust.Fill(c, &amp;quot;Customers1&amp;quot;);&lt;br&gt;&lt;br /&gt;d.DataMember = &amp;quot;Customers1&amp;quot;;&lt;br&gt;&lt;br /&gt;d.CaptionForeColor = Color.Blue;&lt;br&gt;&lt;br /&gt;d.CaptionBackColor = Color.Red;&lt;br&gt;&lt;br /&gt;d.CaptionText = &amp;quot;Vijay Muhki&amp;quot;;&lt;br&gt;&lt;br /&gt;Font f = new Font(&amp;quot;Arial&amp;quot;,10);&lt;br&gt;&lt;br /&gt;d.CaptionVisible = true;&lt;br&gt;&lt;br /&gt;Rectangle r = d.ClientRectangle;&lt;br&gt;&lt;br /&gt;System.Console.WriteLine(r);&lt;br&gt;&lt;br /&gt;Size s = d.ClientSize;&lt;br&gt;&lt;br /&gt;System.Console.WriteLine(s);&lt;br&gt;&lt;br /&gt;System.Console.WriteLine(d.Height + &amp;quot; &amp;quot; + d.Width);&lt;br&gt;&lt;br /&gt;System.Console.WriteLine(d.Left + &amp;quot; &amp;quot; + d.Right);&lt;br&gt;&lt;br /&gt;Point p = d.Location;&lt;br&gt;&lt;br /&gt;System.Console.WriteLine(p.X + &amp;quot; &amp;quot; + p.Y);&lt;br&gt;&lt;br /&gt;System.Console.WriteLine(d.Size);&lt;br&gt;&lt;br /&gt;d.ColumnHeadersVisible = false;&lt;br&gt;&lt;br /&gt;System.Console.WriteLine(d.CompanyName);&lt;br&gt;&lt;br /&gt;Control.ControlCollection cc = d.Controls;&lt;br&gt;&lt;br /&gt;System.Console.WriteLine(cc.Count);&lt;br&gt;&lt;br /&gt;Control c1,c2;&lt;br&gt;&lt;br /&gt;c1 = cc[0]; c2 = cc[1]; &lt;br&gt;&lt;br /&gt;System.Console.WriteLine(c1);&lt;br&gt;&lt;br /&gt;System.Console.WriteLine(c2);&lt;br&gt;&lt;br /&gt;System.Console.WriteLine(d.HasChildren);&lt;br&gt;&lt;br /&gt;Controls.Add(d);&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;public static void Main() &lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;Application.Run(new zzz());&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;Output&lt;br&gt;&lt;br /&gt;{X=0,Y=0,Width=584,Height=336}&lt;br&gt;&lt;br /&gt;{Width=584, Height=336}&lt;br&gt;&lt;br /&gt;336 584&lt;br&gt;&lt;br /&gt;0 584&lt;br&gt;&lt;br /&gt;0 0 &lt;br&gt;&lt;br /&gt;{Width=584, Height=336}&lt;br&gt;&lt;br /&gt;Microsoft Corporation&lt;br&gt;&lt;br /&gt;2&lt;br&gt;&lt;br /&gt;System.Windows.Forms.HScrollBar, Minimum: 0, Maximum: 100, Value: 0&lt;br&gt;&lt;br /&gt;System.Windows.Forms.VScrollBar, Minimum: 0, Maximum: 0, Value: 0&lt;br&gt;&lt;br /&gt;True&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;A caption, which is similar to a Windows title, is displayed above the column &lt;br /&gt;names. It furnishes additional information to the user. The text to be displayed &lt;br /&gt;is decided by the CaptionText property, which by default is an empty string. &lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;&amp;nbsp; 20&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;The CaptionForeColor property determines the foreground color of the caption, &lt;br /&gt;while the CaptionBackColor decides on the background color. The CaptionFont &lt;br /&gt;property refers to the font in which the text is to be displayed. The boolean &lt;br /&gt;value in the property CaptionVisible exhibits or suppresses the display of the &lt;br /&gt;caption. &lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;There are a large number of properties for a Caption since it is derived from &lt;br /&gt;the base class of Control. The documentation specifies whether the property is &lt;br /&gt;read-write or not, thereby signifying whether change is permitted at the design &lt;br /&gt;stage or not.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;The ClientRectangle property, which is read-only, displays the same results as &lt;br /&gt;that of the Bounds property. The co-ordinates are relative to the upper left &lt;br /&gt;corner of the client area or window. Thus, they start at X=0 and Y=0. The width &lt;br /&gt;and height are used as the drawing surface within which data of the grid is to &lt;br /&gt;be placed. &lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;The ClientSize property returns a Size object that contains the Height and Width &lt;br /&gt;of the DataGrid. These dimensions are akin to those returned by the Bounds &lt;br /&gt;property. The Height and Width properties also return the same values. The Left &lt;br /&gt;property denotes the leftmost edge and returns zero. The Right property denotes &lt;br /&gt;the rightmost edge of the data grid, which happens to be 584. It therefore &lt;br /&gt;returns this value. The Location property returns a point whose X and Y &lt;br /&gt;co-ordinates have the value 0,0 since that is where the left edge of the &lt;br /&gt;DataGrid commences. Finally, the Size property once again returns the same data &lt;br /&gt;containing the width and height. &lt;br&gt;&lt;br /&gt;Thus, we have a large number of properties, which return the same information. &lt;br /&gt;The property ColumnHeadersVisible is similar to CaptionVisible. It is boolean &lt;br /&gt;and decides whether the column/field names are to be displayed above the data or &lt;br /&gt;not. This row is also called the parent row.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;The CompanyName property divulges the name of the company that created the &lt;br /&gt;control. The answer, quite obviously, is Microsoft Corporation. &lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;Every control has a property called Controls that returns a ControlCollection &lt;br /&gt;object. The collection constitutes of a list of controls, which form the &lt;br /&gt;DataGrid. The Count property in the Collection reports that the DataGrid control &lt;br /&gt;is made up of 2 controls. Using the indexer, we access the two individual &lt;br /&gt;controls, c1 and c2. The WriteLine function displays these two controls as the &lt;br /&gt;vertical and horizontal scrollbars. &lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;For the ones who tuned in late, every class has a ToString function that &lt;br /&gt;discloses relevant information about the class. The output of the function &lt;br /&gt;ToString varies, depending upon what the class wants to reveal about itself.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;In order to verify that the DataGrid object is fabricated from other controls, &lt;br /&gt;we display the value contained in the property HasChildren. The return value is &lt;br /&gt;True.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;DataGrid Events&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;a.cs&lt;br&gt;&lt;br /&gt;using System;&lt;br&gt;&lt;br /&gt;using System.Data;&lt;br&gt;&lt;br /&gt;using System.Drawing;&lt;br&gt;&lt;br /&gt;using System.Windows.Forms;&lt;br&gt;&lt;br /&gt;using System.Data.SqlClient;&lt;br&gt;&lt;br /&gt;public class zzz : Form {&lt;br&gt;&lt;br /&gt;DataGrid d;&lt;br&gt;&lt;br /&gt;DataSet c;&lt;br&gt;&lt;br /&gt;public zzz() {&lt;br&gt;&lt;br /&gt;d = new DataGrid();&lt;br&gt;&lt;br /&gt;d.Size = new Size(584, 336);&lt;br&gt;&lt;br /&gt;d.DataMember = &amp;quot;Customers1&amp;quot;;&lt;br&gt;&lt;br /&gt;ClientSize = new Size(600, 413);&lt;br&gt;&lt;br /&gt;SqlConnection con = new SqlConnection(&amp;quot;server=(local)\\NetSDK;&lt;br&gt;&lt;br /&gt;uid=QSUser;pwd=QSPassword;database=northwind&amp;quot;);&lt;br&gt;&lt;br /&gt;SqlDataAdapter Cust = new SqlDataAdapter (&amp;quot;Select * from Customers&amp;quot;, con);&lt;br&gt;&lt;br /&gt;c = new DataSet();&lt;br&gt;&lt;br /&gt;d.DataSource = c;&lt;br&gt;&lt;br /&gt;Cust.Fill(c, &amp;quot;Customers1&amp;quot;);&lt;br&gt;&lt;br /&gt;d.DataMember = &amp;quot;Customers1&amp;quot;;&lt;br&gt;&lt;br /&gt;d.MouseDown += new MouseEventHandler(abc);&lt;br&gt;&lt;br /&gt;Controls.Add(d);&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;void abc(object s, MouseEventArgs e){&lt;br&gt;&lt;br /&gt;System.Console.WriteLine(e.X + &amp;quot; &amp;quot; + e.Y + &amp;quot; &amp;quot; + e.Clicks + &amp;quot; &amp;quot; + e.Button) ;&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;public static void Main() {&lt;br&gt;&lt;br /&gt;Application.Run(new zzz());&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;Output&lt;br&gt;&lt;br /&gt;87 66 1 Left&lt;br&gt;&lt;br /&gt;90 82 1 Right&lt;br&gt;&lt;br /&gt;84 101 1 Middle&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;&amp;nbsp; 21&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;Event handling is the arena, wherein the true ability of a Control comes into &lt;br /&gt;focus. We desire that our own event-handling function should be called, every &lt;br /&gt;time an event occurs in the DataGrid control. So far, whenever the control has &lt;br /&gt;been displayed, none of our code has come into play. &lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;In the above example, we desire that whenever the user clicks in the DataGrid, &lt;br /&gt;our code should be executed. To accomplish this, we trap one of the Events using &lt;br /&gt;the MouseEventHandler delegate, and ensure that the function abc is called every &lt;br /&gt;time someone clicks in the DataGrid. &lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;The handler functions is always given the control that generated the event as &lt;br /&gt;the first parameter. In our case, it is the DataGrid. You can verify this by &lt;br /&gt;executing the WriteLine function, which displays the name of the control as &lt;br /&gt;System.Windows.Forms.DataGrid.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;The second parameter to abc is an object that contains the X and Y co-ordinates &lt;br /&gt;i.e the location at which the mouse was clicked; the number of times the mouse &lt;br /&gt;was clicked; and finally, the button that was employed. &lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;You are at liberty to insert the code that you wish to execute, when the event &lt;br /&gt;is called. The basic concept is that, certain events that occur in the DataGrid &lt;br /&gt;can be trapped and, accordingly, specific user-defined functions can be called. &lt;br /&gt;We will focus on some of these events in the forthcoming programs.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;a.cs&lt;br&gt;&lt;br /&gt;using System;&lt;br&gt;&lt;br /&gt;using System.Data;&lt;br&gt;&lt;br /&gt;using System.Drawing;&lt;br&gt;&lt;br /&gt;using System.Windows.Forms;&lt;br&gt;&lt;br /&gt;using System.Data.SqlClient;&lt;br&gt;&lt;br /&gt;public class zzz : Form &lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;DataGrid d;&lt;br&gt;&lt;br /&gt;DataSet c;&lt;br&gt;&lt;br /&gt;public zzz() &lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;d = new DataGrid();&lt;br&gt;&lt;br /&gt;d.Size = new Size(584, 336);&lt;br&gt;&lt;br /&gt;d.DataMember = &amp;quot;Customers1&amp;quot;;&lt;br&gt;&lt;br /&gt;ClientSize = new Size(600, 413);&lt;br&gt;&lt;br /&gt;SqlConnection con = new SqlConnection(&amp;quot;server=(local)\\NetSDK;&lt;br&gt;&lt;br /&gt;uid=QSUser;pwd=QSPassword;database=northwind&amp;quot;);&lt;br&gt;&lt;br /&gt;SqlDataAdapter Cust = new SqlDataAdapter (&amp;quot;Select * from Customers&amp;quot;, con);&lt;br&gt;&lt;br /&gt;c = new DataSet();&lt;br&gt;&lt;br /&gt;d.DataSource = c;&lt;br&gt;&lt;br /&gt;Cust.Fill(c, &amp;quot;Customers1&amp;quot;);&lt;br&gt;&lt;br /&gt;d.DataMember = &amp;quot;Customers1&amp;quot;;&lt;br&gt;&lt;br /&gt;d.CurrentCellChanged += new EventHandler(abc);&lt;br&gt;&lt;br /&gt;Controls.Add(d);&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;void abc(object s, EventArgs e) &lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;DataGridCell g = d.CurrentCell;&lt;br&gt;&lt;br /&gt;int c = g.ColumnNumber ;&lt;br&gt;&lt;br /&gt;int r = g.RowNumber; &lt;br&gt;&lt;br /&gt;System.Console.WriteLine( &amp;quot;Column &amp;quot; + c + &amp;quot; Row &amp;quot; + r + &amp;quot; &amp;quot; + d[r,c]);&lt;br&gt;&lt;br /&gt;d[r,c + 1] = &amp;quot;hi &amp;quot; + r;&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;public static void Main() {&lt;br&gt;&lt;br /&gt;Application.Run(new zzz());&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;Output&lt;br&gt;&lt;br /&gt;Column 0 Row 1 ANATR&lt;br&gt;&lt;br /&gt;Column 1 Row 2 Antonio Moreno Taquer¡a&lt;br&gt;&lt;br /&gt;Column 2 Row 3 Thomas Hardy&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;It is more constructive to trap the event CurrentCellChanged, which gets &lt;br /&gt;activated whenever a cell is selected from the DataGrid. The function abc gets &lt;br /&gt;called with similar parameters as identified by the event in the earlier &lt;br /&gt;program. &lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;In function abc, using the DataGrid property of CurrentCell of type DataGridCell, &lt;br /&gt;we retrieve information such as the Column number, RowNumber etc. of the current &lt;br /&gt;cell. The row number is stored in the variable r, whereas the column number is &lt;br /&gt;stored in the variable c.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;The indexer in the DataGrid aids us in accessing the contents of the cell that &lt;br /&gt;has been clicked on. Since the indexer requires the row number followed the &lt;br /&gt;column number, we supply it with variables r and c. &lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;This indexer is read-write. Therefore, it allows us to change the value of any &lt;br /&gt;cell in the grid. Here, we have altered the value of the column to the right of &lt;br /&gt;the current cell to contain the string 'hi', followed by the row number. This &lt;br /&gt;demonstrates the flexibility provided by a DataGrid control. &lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;&amp;nbsp; 22&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;Every minuscule aspect of the grid can be altered at run time, but all &lt;br /&gt;properties are not available at design time. The events that a grid can respond &lt;br /&gt;to are ever so many to be listed here. The only event that it does not presently &lt;br /&gt;respond to is the 'End of the World' event, which as per our conviction, &lt;br /&gt;Microsoft is likely to redress in the next version.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;Master-Detail or Parent-Child Relationship&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;a.cs&lt;br&gt;&lt;br /&gt;using System;&lt;br&gt;&lt;br /&gt;using System.Data;&lt;br&gt;&lt;br /&gt;using System.Drawing;&lt;br&gt;&lt;br /&gt;using System.Windows.Forms;&lt;br&gt;&lt;br /&gt;using System.Data.SqlClient;&lt;br&gt;&lt;br /&gt;public class zzz : Form {&lt;br&gt;&lt;br /&gt;ccc c;&lt;br&gt;&lt;br /&gt;DataGrid d;&lt;br&gt;&lt;br /&gt;public zzz() {&lt;br&gt;&lt;br /&gt;d = new DataGrid();&lt;br&gt;&lt;br /&gt;c = new ccc();&lt;br&gt;&lt;br /&gt;d.BeginInit();&lt;br&gt;&lt;br /&gt;d.PreferredRowHeight = 16;&lt;br&gt;&lt;br /&gt;d.Size = new System.Drawing.Size(584, 336);&lt;br&gt;&lt;br /&gt;d.DataSource = c;&lt;br&gt;&lt;br /&gt;d.DataMember = &amp;quot;Customers&amp;quot;;&lt;br&gt;&lt;br /&gt;d.ForeColor = System.Drawing.Color.Navy;&lt;br&gt;&lt;br /&gt;d.Location = new System.Drawing.Point(8, 8);&lt;br&gt;&lt;br /&gt;d.BackColor = System.Drawing.Color.Gainsboro;&lt;br&gt;&lt;br /&gt;d.AlternatingBackColor = System.Drawing.Color.WhiteSmoke;&lt;br&gt;&lt;br /&gt;ClientSize = new Size(600, 413);&lt;br&gt;&lt;br /&gt;c.DataSetName = &amp;quot;CustomersDataSet&amp;quot;;&lt;br&gt;&lt;br /&gt;Controls.Add(d);&lt;br&gt;&lt;br /&gt;d.EndInit();&lt;br&gt;&lt;br /&gt;SqlConnection con = new SqlConnection(&amp;quot;server=(local)\\NetSDK;&lt;br&gt;&lt;br /&gt;uid=QSUser;pwd=QSPassword;database=northwind&amp;quot;);&lt;br&gt;&lt;br /&gt;SqlDataAdapter Cust = new SqlDataAdapter (&amp;quot;Select * from Customers&amp;quot;, con);&lt;br&gt;&lt;br /&gt;SqlDataAdapter Ord = new SqlDataAdapter (&amp;quot;Select * from Orders&amp;quot;, con);&lt;br&gt;&lt;br /&gt;Cust.Fill(c, &amp;quot;Customers&amp;quot;);&lt;br&gt;&lt;br /&gt;Ord.Fill(c, &amp;quot;Orders&amp;quot;);&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;public static void Main() {&lt;br&gt;&lt;br /&gt;Application.Run(new zzz());&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;public class ccc : DataSet {&lt;br&gt;&lt;br /&gt;cus tc;&lt;br&gt;&lt;br /&gt;Orders to;&lt;br&gt;&lt;br /&gt;DataRelation r;&lt;br&gt;&lt;br /&gt;public ccc() &lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;tc= new cus(&amp;quot;Customers&amp;quot;);&lt;br&gt;&lt;br /&gt;Tables.Add(this.tc);&lt;br&gt;&lt;br /&gt;to= new Orders(&amp;quot;Orders&amp;quot;);&lt;br&gt;&lt;br /&gt;Tables.Add(this.to);&lt;br&gt;&lt;br /&gt;r = new DataRelation(&amp;quot;custord&amp;quot;,tc.cID,to.oID);&lt;br&gt;&lt;br /&gt;//r = new DataRelation(&amp;quot;custord&amp;quot;,new DataColumn[]{tc.cID},new &lt;br /&gt;DataColumn[]{to.oID});&lt;br&gt;&lt;br /&gt;Relations.Add(r);&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;public class cus : DataTable&lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;public DataColumn cID;&lt;br&gt;&lt;br /&gt;public cus(string name) : base(name) &lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;cID = new DataColumn(&amp;quot;CustomerID&amp;quot;);&lt;br&gt;&lt;br /&gt;Columns.Add(cID);&lt;br&gt;&lt;br /&gt;PrimaryKey = new System.Data.DataColumn[] {cID};&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;public class Orders : DataTable&lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;public DataColumn oID;&lt;br&gt;&lt;br /&gt;public Orders(string name) : base(name) &lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;oID = new DataColumn(&amp;quot;CustomerID&amp;quot;);&lt;br&gt;&lt;br /&gt;Columns.Add(oID);&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;First, let us steal a look at what occurs when the above program is run. &lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;&amp;nbsp; 23&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;When the form loads on, we see a list of customers from the Customers table, &lt;br /&gt;with a plus sign displayed on the left. &lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;&amp;nbsp; 24&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;Clicking on the plus sign will exhibit a hyperlink with the word 'custord'. When &lt;br /&gt;we click on the hyperlink, a list of orders placed by this customer along with &lt;br /&gt;the customer details is displayed on the first row. The topmost right hand &lt;br /&gt;corner has a back button, which takes us back to the Customers table. &lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;&amp;nbsp; 25&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;This is a perfect example of a master-detail relationship. What follows next, is &lt;br /&gt;an explanation of the code that implements this parent-child relationship.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;At the outset, the DataGrid must be populated with data from a database. At &lt;br /&gt;times, it may take considerable time to fetch this data. While this process is &lt;br /&gt;on, we would obviously not want the user to interact with or use the control. In &lt;br /&gt;order to forbid interference by the user while the data is being retrieved, the &lt;br /&gt;DataGrid control provides us with two functions, viz., BeginInit and EndInit. &lt;br /&gt;The function BeginInit informs the DataGrid that initialization has begun and &lt;br /&gt;the function EndInit signals that the process of data retrieval has been &lt;br /&gt;accomplished. The EndInit function is placed at the absolute end of the code &lt;br /&gt;handling the database. If we comment out the EndInit function, the DataGrid &lt;br /&gt;behaves similarly, but with one small difference, i.e. it now assumes a &lt;br /&gt;read-only state.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;The DataSource property is an object that is normally derived from DataSet. In &lt;br /&gt;our program this property is initialized to c, which is an instance of class ccc. &lt;br /&gt;Class ccc is derived from DataSet. The constructor of this class performs many &lt;br /&gt;interesting actions. &lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;We begin by creating an object tc, which is an instance of class cus. This class &lt;br /&gt;in turn, is derived from class DataTable. In the constructor of class cus, the &lt;br /&gt;parameter 'Customers' is supplied to the constructor of the DataTable, using the &lt;br /&gt;base keyword. Thus, the DataTable is now called Customers. We also create a &lt;br /&gt;DataColumn object cID, which is named CustomerID, and we use the Add function in &lt;br /&gt;the Columns collection of the DataTable class, to add this column to the &lt;br /&gt;Customers DataTable. &lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;Every table should essentially consist of one or more columns, which can &lt;br /&gt;uniquely identify a row of the table. This set of columns is called a Primary &lt;br /&gt;Key. The PrimaryKey property is set to an array of DataColumn objects, which &lt;br /&gt;constitute the columns comprising the primary key. Normally, the primary key is &lt;br /&gt;a single column. Most modern databases would not create a table without first &lt;br /&gt;coercing the user to specify the primary key. In this program, the setting of &lt;br /&gt;the PrimaryKey property is optional. However, creating the column called &lt;br /&gt;CustomerID, is mandatory.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;Now that we have created a DataTable object, we need to add it to the DataSet. &lt;br /&gt;This is achieved by employing the Add function in the Tables collection. An &lt;br /&gt;instance of class Orders that is derived from class DataTable, is created and &lt;br /&gt;named as Orders. This table has one column with the same name CustomerID. The &lt;br /&gt;Orders table is then added to the DataSet. You may note that this keyword is &lt;br /&gt;optional.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;The very quintessence of the application is the DataRelation object. During the &lt;br /&gt;creation of the object r, the constructor of the DataRelation object is called &lt;br /&gt;with three parameters. The first parameter is the name of the data relation. A &lt;br /&gt;name is to be provided, whenever more than one entity of the same type is &lt;br /&gt;created. In this case, the name custord becomes a hyperlink, and gets displayed &lt;br /&gt;whenever we click on the plus sign. The next two parameters are the parent and &lt;br /&gt;child columns, which are related to each other. The column CustomerID from the &lt;br /&gt;Customers table is related to the CustomerID column in the Orders table, in &lt;br /&gt;order to establish a one-to-many relationship. So, we specify these DataColumn &lt;br /&gt;objects as the next two parameters. &lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;The DataRelation object can also be related with a DataColumn object, expressed &lt;br /&gt;as an array, in case there are multiple DataColumn objects within the relation. &lt;br /&gt;The commented line displays the same relation expressed as an array of &lt;br /&gt;DataColumn objects. We finally add this freshly minted Relation object to the &lt;br /&gt;Relation collection. &lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;It is obligatory to derive from the DataSet class, since both the DataTable &lt;br /&gt;objects, Customers and Orders, need to be added. Since a DataRelation had to be &lt;br /&gt;created between two columns, one each from these tables, we had to create two &lt;br /&gt;Data Column objects in classes derived from the DataTable class. &lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;This proves that data has not been physically added to the DataTable. Presently, &lt;br /&gt;the DataTable comprises of only two columns and a relation. &lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;The utility of the second parameter to the Fill function, which is the name of &lt;br /&gt;the mapping table, will now become apparent. At this juncture, we associate the &lt;br /&gt;data from the database with the tables Customer and Order, through the &lt;br /&gt;SqlDataAdapter objects Cust and Ord. &lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;As the DataRelation is created through these tables, the DataGrid displays the &lt;br /&gt;plus sign. The second parameter to the Fill function is called the Mapping &lt;br /&gt;Tables parameter. This parameter is needed, unless we wish to build the &lt;br /&gt;relations in the database on our own, which is, by all odds, not a &lt;br /&gt;straightforward task. If the AllowNavigation property is set to False, the links &lt;br /&gt;to the child tables shall no longer be visible.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;This program clearly demonstrates how we can impose our own relationships on &lt;br /&gt;data contained in databases, which have been created by others. &lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;a.cs&lt;br&gt;&lt;br /&gt;using System;&lt;br&gt;&lt;br /&gt;using System.Data;&lt;br&gt;&lt;br /&gt;using System.Drawing;&lt;br&gt;&lt;br /&gt;using System.Windows.Forms;&lt;br&gt;&lt;br /&gt;using System.Data.SqlClient;&lt;br&gt;&lt;br /&gt;public class zzz : Form &lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;StatusBar s;&lt;br&gt;&lt;br /&gt;CustomersDataSet customersDataSet1;&lt;br&gt;&lt;br /&gt;Button b;&lt;br&gt;&lt;br /&gt;DataGrid d;&lt;br&gt;&lt;br /&gt;public zzz() &lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;d = new DataGrid();&lt;br&gt;&lt;br /&gt;s = new StatusBar();&lt;br&gt;&lt;br /&gt;customersDataSet1 = new CustomersDataSet();&lt;br&gt;&lt;br /&gt;b = new Button();&lt;br&gt;&lt;br /&gt;d.BeginInit();&lt;br&gt;&lt;br /&gt;d.PreferredRowHeight = 16;&lt;br&gt;&lt;br /&gt;d.Size = new System.Drawing.Size(584, 336);&lt;br&gt;&lt;br /&gt;d.DataSource = customersDataSet1;&lt;br&gt;&lt;br /&gt;d.DataMember = &amp;quot;Customers&amp;quot;;&lt;br&gt;&lt;br /&gt;d.ForeColor = System.Drawing.Color.Navy;&lt;br&gt;&lt;br /&gt;d.Location = new System.Drawing.Point(8, 8);&lt;br&gt;&lt;br /&gt;d.BackColor = System.Drawing.Color.Gainsboro;&lt;br&gt;&lt;br /&gt;d.AlternatingBackColor = System.Drawing.Color.WhiteSmoke;&lt;br&gt;&lt;br /&gt;AcceptButton = b;&lt;br&gt;&lt;br /&gt;ClientSize = new Size(600, 413);&lt;br&gt;&lt;br /&gt;s.BackColor = System.Drawing.SystemColors.Control;&lt;br&gt;&lt;br /&gt;s.Size = new System.Drawing.Size(600, 16);&lt;br&gt;&lt;br /&gt;s.Text = &amp;quot;Click on Load&amp;quot;;&lt;br&gt;&lt;br /&gt;s.Location = new System.Drawing.Point(0, 397);&lt;br&gt;&lt;br /&gt;customersDataSet1.DataSetName = &amp;quot;CustomersDataSet&amp;quot;;&lt;br&gt;&lt;br /&gt;b.FlatStyle = FlatStyle.Flat;&lt;br&gt;&lt;br /&gt;b.Size = new Size(112, 32);&lt;br&gt;&lt;br /&gt;b.Text = &amp;quot;&amp;amp;Load&amp;quot;;&lt;br&gt;&lt;br /&gt;b.Location = new Point(480, 352);&lt;br&gt;&lt;br /&gt;b.Click += new System.EventHandler(abc);&lt;br&gt;&lt;br /&gt;Controls.Add(s);&lt;br&gt;&lt;br /&gt;Controls.Add(b);&lt;br&gt;&lt;br /&gt;Controls.Add(d);&lt;br&gt;&lt;br /&gt;d.EndInit();&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;void abc(object sender, System.EventArgs e) &lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;Cursor cu = Cursor.Current;&lt;br&gt;&lt;br /&gt;try &lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;Cursor.Current = Cursors.WaitCursor;&lt;br&gt;&lt;br /&gt;SqlConnection con = new SqlConnection(&amp;quot;server=(local)\\NetSDK;uid=QSUser;pwd=QSPassword;database=northwind&amp;quot;);&lt;br&gt;&lt;br /&gt;SqlDataAdapter Cust = new SqlDataAdapter (&amp;quot;Select * from Customers&amp;quot;, con);&lt;br&gt;&lt;br /&gt;SqlDataAdapter Ord = new SqlDataAdapter (&amp;quot;Select * from Orders&amp;quot;, con);&lt;br&gt;&lt;br /&gt;SqlDataAdapter OrdD = new SqlDataAdapter (&amp;quot;Select * from [Order Details]&amp;quot;, con);&lt;br&gt;&lt;br /&gt;s.Text =&amp;quot;Loading Customers...&amp;quot;;&lt;br&gt;&lt;br /&gt;Cust.Fill(customersDataSet1, &amp;quot;Customers&amp;quot;);&lt;br&gt;&lt;br /&gt;s.Text =&amp;quot;Loading Orders...&amp;quot;;&lt;br&gt;&lt;br /&gt;Ord.Fill(customersDataSet1, &amp;quot;Orders&amp;quot;);&lt;br&gt;&lt;br /&gt;s.Text =&amp;quot;Loading Order Details...&amp;quot;;&lt;br&gt;&lt;br /&gt;OrdD.Fill(customersDataSet1, &amp;quot;Order_Details&amp;quot;);&lt;br&gt;&lt;br /&gt;s.Text =&amp;quot;Updating Grid...&amp;quot;;&lt;br&gt;&lt;br /&gt;} &lt;br&gt;&lt;br /&gt;finally &lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;s.Text =&amp;quot;Done&amp;quot;;&lt;br&gt;&lt;br /&gt;Cursor.Current = cu;&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;public static void Main() &lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;Application.Run(new zzz());&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;public class CustomersDataSet : DataSet &lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;Customers tc;&lt;br&gt;&lt;br /&gt;Orders to;&lt;br&gt;&lt;br /&gt;Order_Details td;&lt;br&gt;&lt;br /&gt;DataRelation ro;&lt;br&gt;&lt;br /&gt;DataRelation rd;&lt;br&gt;&lt;br /&gt;public CustomersDataSet() &lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;tc = new Customers(&amp;quot;Customers&amp;quot;);&lt;br&gt;&lt;br /&gt;Tables.Add(tc);&lt;br&gt;&lt;br /&gt;to = new Orders(&amp;quot;Orders&amp;quot;);&lt;br&gt;&lt;br /&gt;Tables.Add(to);&lt;br&gt;&lt;br /&gt;td = new Order_Details(&amp;quot;Order_Details&amp;quot;);&lt;br&gt;&lt;br /&gt;Tables.Add(td);&lt;br&gt;&lt;br /&gt;ro = new DataRelation(&amp;quot;CustomersOrders&amp;quot;, new DataColumn[] {tc.cID}, new &lt;br /&gt;DataColumn[] {to.columnCustomerID}, false);&lt;br&gt;&lt;br /&gt;Relations.Add(ro);&lt;br&gt;&lt;br /&gt;rd = new DataRelation(&amp;quot;OrdersOrder_Details&amp;quot;, new DataColumn[] {to.columnOrderID}, &lt;br /&gt;new DataColumn[] {td.odID}, false);&lt;br&gt;&lt;br /&gt;Relations.Add(rd);&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;public class Customers : DataTable&lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;public DataColumn cID;&lt;br&gt;&lt;br /&gt;public Customers(string name) : base(name) &lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;cID = new DataColumn(&amp;quot;CustomerID&amp;quot;);&lt;br&gt;&lt;br /&gt;Columns.Add(cID);&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;public class Orders : DataTable&lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;public DataColumn columnOrderID;&lt;br&gt;&lt;br /&gt;public DataColumn columnCustomerID;&lt;br&gt;&lt;br /&gt;public Orders(string name) : base(name) &lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;columnOrderID = new DataColumn(&amp;quot;OrderID &amp;quot;, typeof(int));&lt;br&gt;&lt;br /&gt;Columns.Add(this.columnOrderID);&lt;br&gt;&lt;br /&gt;columnCustomerID = new DataColumn(&amp;quot;CustomerID&amp;quot;, typeof(string));&lt;br&gt;&lt;br /&gt;Columns.Add(this.columnCustomerID);&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;public class Order_Details : DataTable&lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;public DataColumn odID;&lt;br&gt;&lt;br /&gt;public Order_Details(string name) : base(name) &lt;br&gt;&lt;br /&gt;{&lt;br&gt;&lt;br /&gt;odID= new DataColumn(&amp;quot;OrderID&amp;quot;, typeof(int));&lt;br&gt;&lt;br /&gt;Columns.Add(odID);&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;}&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;On executing the program, we witness an empty DataGrid with a star symbol and &lt;br /&gt;the word 'CustomerID' displayed. The status bar perceptibly advises us to click &lt;br /&gt;on the button labeled 'Load'. &lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;&amp;nbsp; 26&amp;nbsp;&amp;nbsp; 27&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;After we have done so, a list of customers is displayed, along with a plus sign. &lt;br /&gt;If we click on the plus sign, a hyperlink on CustomerOrders will be displayed. A &lt;br /&gt;click on this link, would lead to a list of orders placed by this customer. &lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;There has been an addition to the program. A plus sign is displayed with every &lt;br /&gt;order, which expands to a hyperlink OrdersOrder detail. If we click on this &lt;br /&gt;hyperlink, the actual items included in the particular order, are to be &lt;br /&gt;displayed. &lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;&amp;nbsp; 28&amp;nbsp;&amp;nbsp; 29&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;The first line of the DataGrid also reveals both, the customer details, as well &lt;br /&gt;as, the order details. If we click just once on the Back button, it takes us &lt;br /&gt;back to the orders. Clicking on it again, takes us further back to the list of &lt;br /&gt;customers.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;We add a StatusBar, as is customary in most applications, to display user &lt;br /&gt;interface messages. The constructor of the class ccc, performs the same tasks as &lt;br /&gt;explained in the earlier program. Previously, we had created two tables; but &lt;br /&gt;now, we create three tables, i.e. Customers, Orders and Order_Details. &lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;The Customers table has one column called CustomerID; the Orders table has two &lt;br /&gt;columns named CustomerID and OrderID; and the Order_Details table has only one &lt;br /&gt;column called OrderID. We now create two relations. The first one is called &lt;br /&gt;CustomersOrders that relates the CustomerID columns in the two tables, as &lt;br /&gt;before. The second relation is named OrdersOrder_Details, which relates the &lt;br /&gt;OrderID column from the Orders table, to the OrderID column from the &lt;br /&gt;OrderDetails table. Thus, the only difference between this program and its &lt;br /&gt;predecessor is, the inclusion of an additional table, column and relation.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;The DataSource that represents a DataSet is made up of three tables. The &lt;br /&gt;question that is expected to surface in our minds is: Which of the three tables &lt;br /&gt;should be used in the initial display? The DataSource property decides the &lt;br /&gt;initial table. Since we have specified Customers, we see only one column, i.e. &lt;br /&gt;CustomerID. If we comment out the line d.DataMember = &amp;quot;Customers&amp;quot;, only a plus &lt;br /&gt;sign would be displayed without any column name. Clicking on the plus sign would &lt;br /&gt;demonstrate the list of three tables. Finally, when we click on the link that &lt;br /&gt;displays Orders, we shall spot the two columns that we have created. &lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;&amp;nbsp;0&amp;nbsp; 1&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;The property DataSetName can be ignored for the moment. If we click on the &lt;br /&gt;button, the function abc gets called. In this function, we first save the &lt;br /&gt;current cursor in a Cursor object called cu. This current cursor is stored in &lt;br /&gt;the property Current of the Cursor object. Then, we modify the current cursor to &lt;br /&gt;the Wait Cursor, since it is extremely time consuming to write the code, in &lt;br /&gt;order to retrieve data from the database. &lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;We use the Fill function in the similar manner as used before, and then change &lt;br /&gt;the text displayed in the Status Bar, depending upon the table that is being &lt;br /&gt;filled up from the database. It is advisable to place database-handling code in &lt;br /&gt;a try catch statement for error handling. Code placed in the finally clause, &lt;br /&gt;resets user interface widgets like the cursor, status bar, etc. &lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;We can have as many data relations as we yearn for, and we can build as many &lt;br /&gt;logical relations between tables as we covet. In the above program, we have two &lt;br /&gt;levels; but surely, many more levels are realizable !&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;&amp;nbsp;&lt;/p&gt;</description><thr:total xmlns:thr="http://purl.org/syndication/thread/1.0">0</thr:total></item><item><title>C Language  Help, C Language Tutorials, C Language Programming, C Language Tricks  { The C# Language }</title><link>http://swebsolution.blogspot.com/2007/12/controls-c-language.html</link><author>noreply@blogger.com (Unknown)</author><pubDate>Mon, 17 Dec 2007 00:38:00 -0800</pubDate><guid isPermaLink="false">tag:blogger.com,1999:blog-7227425366482465228.post-5509344133397567613</guid><description>&lt;p align="justify"&gt;&lt;b&gt;C Language  Help, C Language Tutorials, C Language Programming, C Language Tricks &lt;/b&gt;&lt;br&gt;&lt;strong&gt;Controls&lt;/strong&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Windows Forms or Winforms is a contemporary Windows-based forms package that&lt;br /&gt;endows the Windows programmer with an innovative methodology for creating&lt;br /&gt;aesthetic user interfaces and interactive applications. We will not ramble on&lt;br /&gt;about the pros and cons of the package, but commence the creation of the&lt;br /&gt;smallest GUI (Graphical User Interface) application.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;a.cs&lt;br /&gt;&lt;br /&gt;public class zzz&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;public static void Main()&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;zzz z = new zzz();&lt;br /&gt;&lt;br /&gt;System.Windows.Forms.Application.Run(z);&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Run the compiler as&lt;br /&gt;&lt;br /&gt;&amp;gt;csc a.cs&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Compiler Error&lt;br /&gt;&lt;br /&gt;a.cs(6,1): error CS1502: The best overloaded method match for&lt;br /&gt;&lt;br /&gt;'System.Windows.Forms.Application.Run(System.Windows.Forms.Form)' has some&lt;br /&gt;invalid arguments&lt;br /&gt;&lt;br /&gt;a.cs(6,38): error CS1503: Argument '1': cannot convert from 'zzz' to 'System.Windows.Forms.Form'&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;An error is generated because, Run, which is a static function in the&lt;br /&gt;Application class of the System.Windows.Forms namespace, requires a Form object.&lt;br /&gt;The error distinctly states its inability to convert a zzz to&lt;br /&gt;System.Windows.Forms.Form, which proves that, an object that looks like Form is&lt;br /&gt;mandatory here, and not zzz.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;a.cs&lt;br /&gt;&lt;br /&gt;using System.Windows.Forms;&lt;br /&gt;&lt;br /&gt;public class zzz : Form&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;public static void Main()&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;Application.Run(new zzz());&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;Screen 4.1&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;This program is not very dissimilar from the previous one. The using keyword is&lt;br /&gt;employed to avoid the inevitability of writing namespace with every object. The&lt;br /&gt;object z has no efficacy here, since we are passing the zzz object directly to&lt;br /&gt;the Run function. As the class zzz is derived from Form, no error is generated.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;When we run the program, a small blank window is displayed. You can click on the&lt;br /&gt;'x' symbol to close it. The output is substantial enough for a single line of&lt;br /&gt;code.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;a.cs&lt;br /&gt;&lt;br /&gt;using System.Windows.Forms;&lt;br /&gt;&lt;br /&gt;public class zzz: Form&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;public static void Main()&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;Application.Run(new zzz());&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;zzz()&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;Text = "Vijay Mukhi";&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Screen 4.2&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;In the constructor of the zzz class, we have initialized a member called Text to&lt;br /&gt;the string value 'Vijay Mukhi'. When we run the program, to our amazement, our&lt;br /&gt;window, which earlier was without a title, now possesses the title 'Vijay Mukhi'.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;This is the introductory concept of Windows Forms programming. The class called&lt;br /&gt;Form has abundant properties such as Text etc., which have specific relevance in&lt;br /&gt;a window. Any modifications to the properties get reflected immediately in the&lt;br /&gt;window. The changes depend upon the properties that we modify. In this case, the&lt;br /&gt;property of Text changes the Caption, text displayed in the title bar.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;A Form represents a window displayed by an application. An application can have&lt;br /&gt;different types of windows, such as a standard, tool bar, borderless or floating&lt;br /&gt;window. The Form class is versatile enough to handle all the above types of&lt;br /&gt;windows, as it is derived from innumerable classes. A Dialog Box, which is used&lt;br /&gt;to accept input from the user, is available in two modes viz. modal and&lt;br /&gt;modeless. Our trustworthy Form class can also handle such Dialog boxes with&lt;br /&gt;equal aplomb. We normally use the Form class as the preliminary class for&lt;br /&gt;building WinForms Applications.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;The Main method calls the Run function and gives it the Form object. In the&lt;br /&gt;constructor, we can modify the properties of the Form Class to give the window a&lt;br /&gt;desired appearance. Since these properties are not static, they cannot be&lt;br /&gt;altered in Main, but can be modified in the constructor or in any other&lt;br /&gt;function.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;a.cs&lt;br /&gt;&lt;br /&gt;using System.Windows.Forms;&lt;br /&gt;&lt;br /&gt;public class zzz&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;public static void Main()&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;Application.Run(new yyy());&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;class yyy : Form&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;public yyy()&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;Text = "Vijay Mukhi";&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Screen 4.3&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;In this program, we have created another class yyy that derives from the Form&lt;br /&gt;class. We have used this object as a parameter to the function Run. The rules do&lt;br /&gt;not impel us to derive the class zzz from Form. However, in all our programs, we&lt;br /&gt;shall follow the first approach since we have decided to steer clear of&lt;br /&gt;controversy and stick to the rules.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;a.cs&lt;br /&gt;&lt;br /&gt;using System.Windows.Forms;&lt;br /&gt;&lt;br /&gt;public class zzz: Form&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;public static void Main()&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;Application.Run(new zzz());&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;zzz()&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;ClientSize = new System.Drawing.Size(300,600);&lt;br /&gt;&lt;br /&gt;Size = new System.Drawing.Size(100,200);&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Screen 4.4&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;The above example sets two properties of the Form class. The first, which is&lt;br /&gt;called ClientSize, is used by Windows.Forms to decide how large our initial&lt;br /&gt;window would be. This property has a default value, which can be overwritten by&lt;br /&gt;specifying the width and height. As we need to furnish two values, we use a&lt;br /&gt;class called Size in the namespace System.Drawing, which accepts two values.&lt;br /&gt;This class does not insist on receiving meaningful values. The constructor is&lt;br /&gt;passed the width and height of the desired window in pixels.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;A graphics screen is divided into small dots or pixels. Depending upon the&lt;br /&gt;configuration of the monitor and graphics card, a computer can handle and&lt;br /&gt;display a certain number of pixels and colors. The higher the configuration, the&lt;br /&gt;larger are the number of pixels and colors that are available.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;The size of the client area of the form is computed as the size of the form&lt;br /&gt;minus the borders and the title bar placed by Windows. They are not of our&lt;br /&gt;concern, since we shall be placing our own controls in our form. ClientSize is a&lt;br /&gt;property with a default value, and it gets updated automatically whenever the&lt;br /&gt;form is resized.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;The next property is Size. The user enjoys the flexibility of altering the size&lt;br /&gt;of the window at run time. Size is initialized in manner similar to ClientSize.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;a.cs&lt;br /&gt;&lt;br /&gt;using System.Windows.Forms;&lt;br /&gt;&lt;br /&gt;using System.Drawing;&lt;br /&gt;&lt;br /&gt;public class zzz: Form&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;Button b;&lt;br /&gt;&lt;br /&gt;public static void Main()&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;Application.Run(new zzz());&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;zzz()&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;b = new Button();&lt;br /&gt;&lt;br /&gt;Controls.Add(b);&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Screen 4.5&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;We now see a small button at the top left hand corner of our Window. How did we&lt;br /&gt;create this button? To do so, we first, create an object b, that looks like a&lt;br /&gt;Button class.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;The Form class has a large number of properties such as ClientSize, Size etc.&lt;br /&gt;One of them is called Controls, which is a read-only property since it contains&lt;br /&gt;only a Get. This property returns a Control.Collection object, whose Add&lt;br /&gt;function adds the control to the Client area of the window. We shall be sprucing&lt;br /&gt;up our button shortly.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;a.cs&lt;br /&gt;&lt;br /&gt;using System.Windows.Forms;&lt;br /&gt;&lt;br /&gt;using System.Drawing;&lt;br /&gt;&lt;br /&gt;public class zzz: Form&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;Button b;&lt;br /&gt;&lt;br /&gt;public static void Main()&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;Application.Run(new zzz());&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;zzz()&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;b = new Button();&lt;br /&gt;&lt;br /&gt;b.Location = new Point(100,200);&lt;br /&gt;&lt;br /&gt;b.Size = new Size(100,50);&lt;br /&gt;&lt;br /&gt;b.Text = "Vijay Mukhi is smart";&lt;br /&gt;&lt;br /&gt;Controls.Add(b);&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Screen 4.6&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Anything that is placed on a form is called a Control or a Widget. Similar to a&lt;br /&gt;Form, a button control, popularly known as a command button, has numerous&lt;br /&gt;properties. One of them is the Location property, which decides the position on&lt;br /&gt;the Client area where the button will be positioned.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Here, we use the Point class and not Size, even though both are objects that&lt;br /&gt;represent two numbers. By convention, a Size object represents a width and a&lt;br /&gt;height and a Point object has an x and y co-ordinate system, starting from the&lt;br /&gt;upper left corner.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Most properties have a default value. Since this fact about default values has&lt;br /&gt;been reiterated numerous times, we shall not repeat it again. The Size property&lt;br /&gt;determines the initial size of the window and the string assigned to the Text&lt;br /&gt;property is displayed on the button.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;a.cs&lt;br /&gt;&lt;br /&gt;using System.Windows.Forms;&lt;br /&gt;&lt;br /&gt;using System.Drawing;&lt;br /&gt;&lt;br /&gt;public class zzz: Form&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;Button b;&lt;br /&gt;&lt;br /&gt;public static void Main() {&lt;br /&gt;&lt;br /&gt;Application.Run(new zzz());&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;zzz()&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;b = new Button();&lt;br /&gt;&lt;br /&gt;b.Location = new Point(100,200);&lt;br /&gt;&lt;br /&gt;b.Size = new Size(100,50);&lt;br /&gt;&lt;br /&gt;b.Text = "Vijay Mukhi is smart";&lt;br /&gt;&lt;br /&gt;b.Click += new System.EventHandler(abc);&lt;br /&gt;&lt;br /&gt;Controls.Add(b);&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;public void abc(object s, System.EventArgs e)&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;MessageBox.Show("Hi");&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Screen 4.7&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;In the earlier example, clicking on the button was an exercise in futility&lt;br /&gt;because the button did not achieve anything. After augmenting the code of the&lt;br /&gt;program, when we click on the button, we see a MessageBox that displays the&lt;br /&gt;greeting 'Hi'. The rationale behind a button control is that, when we click on&lt;br /&gt;it, some code should get executed, some action should take place.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;The button class has an event object called Click, which accepts an object of&lt;br /&gt;type EventHandler. The syntax for events uses the += symbol to add a function&lt;br /&gt;that is to be called when the event handler gets activated. The function name is&lt;br /&gt;given through the EventHandler delegate. This delegate has been specially&lt;br /&gt;created only to handle events that a control will generate.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Thus, the function abc, which is passed as a parameter to the EventHandler&lt;br /&gt;delegate, must have a certain signature. The first parameter is the generic&lt;br /&gt;object that could represent any entity identifying the caller. The second&lt;br /&gt;parameter is an EventArgs object, which we will explain shortly. Thus, each time&lt;br /&gt;we click on the button, the function abc gets called. This function in turn&lt;br /&gt;calls the static function Show from the MessageBox class to display 'Hi'.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;a.cs&lt;br /&gt;&lt;br /&gt;using System.Windows.Forms;&lt;br /&gt;&lt;br /&gt;using System.Drawing;&lt;br /&gt;&lt;br /&gt;public class zzz: Form {&lt;br /&gt;&lt;br /&gt;Button b;&lt;br /&gt;&lt;br /&gt;public static void Main()&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;Application.Run(new zzz());&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;zzz()&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;b = new Button();&lt;br /&gt;&lt;br /&gt;b.Location = new Point(100,200);&lt;br /&gt;&lt;br /&gt;b.Size = new Size(100,50);&lt;br /&gt;&lt;br /&gt;b.Text = "Vijay Mukhi is smart";&lt;br /&gt;&lt;br /&gt;b.Click += new System.EventHandler(abc);&lt;br /&gt;&lt;br /&gt;b.Click += new System.EventHandler(pqr);&lt;br /&gt;&lt;br /&gt;Controls.Add(b);&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;public void abc(object s, System.EventArgs e){&lt;br /&gt;&lt;br /&gt;MessageBox.Show("Hi");&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;public void pqr(object s, System.EventArgs e)&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;MessageBox.Show("Bye");&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Screen 4.8&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;This program reveals the veritable power of events and delegates. Two functions,&lt;br /&gt;abc and pqr, are called whenever the button is clicked. To achieve this, all&lt;br /&gt;that we need to do in the code is to call the Click event again, using the +=&lt;br /&gt;symbol, followed by the name of the new function. The -= symbol is used if we&lt;br /&gt;change our minds. This is a type safe way of calling code in response to an&lt;br /&gt;event.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;a.cs&lt;br /&gt;&lt;br /&gt;using System.Windows.Forms;&lt;br /&gt;&lt;br /&gt;using System.Drawing;&lt;br /&gt;&lt;br /&gt;public class zzz: Form {&lt;br /&gt;&lt;br /&gt;Button b;&lt;br /&gt;&lt;br /&gt;TextBox t;&lt;br /&gt;&lt;br /&gt;public static void Main() {&lt;br /&gt;&lt;br /&gt;Application.Run(new zzz());&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;zzz()&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;b = new Button();&lt;br /&gt;&lt;br /&gt;b.Location = new Point(100,200);&lt;br /&gt;&lt;br /&gt;b.Size = new Size(100,50);&lt;br /&gt;&lt;br /&gt;b.Text = "Vijay Mukhi is smart";&lt;br /&gt;&lt;br /&gt;b.Click += new System.EventHandler(abc);&lt;br /&gt;&lt;br /&gt;t = new TextBox();&lt;br /&gt;&lt;br /&gt;t.Text = "Hell";&lt;br /&gt;&lt;br /&gt;t.Location = new Point(10,20);&lt;br /&gt;&lt;br /&gt;Controls.Add(b);&lt;br /&gt;&lt;br /&gt;Controls.Add(t);&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;public void abc(object s, System.EventArgs e)&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;MessageBox.Show(t.Text + " " + ClientSize );&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Screen 4.9&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;In the Forms Window, we now see two controls: a Button and a TextBox object that&lt;br /&gt;lets us enter some text. The textbox widget also has a large number of&lt;br /&gt;properties associated with it. We shall not be repeating this obvious fact for&lt;br /&gt;all the other controls. The properties Location and Size work in a similar&lt;br /&gt;manner when used with any Control, but the property Text differs, depending upon&lt;br /&gt;the object in use. For a button, it represents the caption, whereas for a text&lt;br /&gt;box, it represents the text that is entered. Thus, some of the properties play&lt;br /&gt;different roles when used in different controls.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Screen 4.10&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Each time the button is clicked, we would like to display the text that has been&lt;br /&gt;entered by the user in the text box. In the eventhandler function abc, the&lt;br /&gt;property Text reveals the text entered into the textbox. The MessageBox class is&lt;br /&gt;used to display the value, along with the size of the client area.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;You can change the size of the window or change the contents of the text box and&lt;br /&gt;observe the contents of the MessageBox changing dynamically.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;a.cs&lt;br /&gt;&lt;br /&gt;using System.Windows.Forms;&lt;br /&gt;&lt;br /&gt;using System.Drawing;&lt;br /&gt;&lt;br /&gt;public class zzz: Form&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;public static void Main()&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;Application.Run(new zzz());&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;public override void Dispose()&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;base.Dispose();&lt;br /&gt;&lt;br /&gt;MessageBox.Show( "hi " + ClientSize );&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;This program displays our ability to invoke code at a specific point in time,&lt;br /&gt;which in this case, is at the stage when the user closes the window or when the&lt;br /&gt;application quits out. It is akin to fulfilling the last wishes of the program.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Screen 4.11&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;As the application is quitting, it calls a function called Dispose. So, if you&lt;br /&gt;ever want code to be called at the point when an application is about to quit&lt;br /&gt;out, you must place it in the Dispose function. This code could be used to close&lt;br /&gt;files or do anything else that the programmer desires.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;It is not mandatory to call Dispose of the base class, but it is always a good&lt;br /&gt;programming practice to call the base class function first, and then augment it&lt;br /&gt;with your own code. In this particular case, it is inconsequential, but under&lt;br /&gt;different circumstances, things may go out of hand if this advice is not heeded.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;a.cs&lt;br /&gt;&lt;br /&gt;using System.Windows.Forms;&lt;br /&gt;&lt;br /&gt;using System.Drawing;&lt;br /&gt;&lt;br /&gt;public class zzz: Form {&lt;br /&gt;&lt;br /&gt;public static void Main()&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;Application.Run(new zzz());&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;Brush b; int ii = 0;&lt;br /&gt;&lt;br /&gt;protected override void OnPaint(PaintEventArgs e)&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;Graphics g = e.Graphics;&lt;br /&gt;&lt;br /&gt;b = new SolidBrush(Color.Blue);&lt;br /&gt;&lt;br /&gt;ii++;&lt;br /&gt;&lt;br /&gt;g.DrawString("Vijay Mukhi " + ii, Font, b ,1,25);&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;In this program, we are overriding a function called OnPaint, which is present&lt;br /&gt;in the Form class. The OnPaint function gets called each time the window has to&lt;br /&gt;be redrawn. Therefore, all code that is to be written to the screen must be&lt;br /&gt;written in this function. This code cannot be placed anywhere else in the&lt;br /&gt;program.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Our next endeavor is to create an object that has functions which display text&lt;br /&gt;on the screen or draw a picture. The class that contains these display functions&lt;br /&gt;is called Graphics. Thus, we create an object g that looks like Graphics. As an&lt;br /&gt;object that looks like Graphics cannot be instantiated, WinForms provides us&lt;br /&gt;with an object of type PaintEventArgs with the OnPaint Function. This class&lt;br /&gt;contains members required for graphical display. Hence, g is initialized to the&lt;br /&gt;Graphics member in e.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;As mentioned earlier, OnPaint gets called whenever our window has to be redrawn.&lt;br /&gt;Whenever OnPaint gets called, it creates an object that looks like&lt;br /&gt;PaintEventArgs and then passes it as a parameter to the function. This object&lt;br /&gt;has a member called Graphics, which contains functions used for drawing in our&lt;br /&gt;client area. The DrawString function requires the text that is to be displayed&lt;br /&gt;and its Font.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;The Form class provides us with the object called Font. Thereafter, the text&lt;br /&gt;color, or to be more precise, the brush is to be specified. Here, we want a&lt;br /&gt;solid Brush like object. So, we create an object b, and give it a color in which&lt;br /&gt;the text should be displayed. There is a static object Blue in the class Color&lt;br /&gt;that stands for the color blue. The spelling of 'color' is as per the American&lt;br /&gt;usage. Finally, the x and y co-ordinates on the screen are specified.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;This positions the text in the window at these specified co-ordinates.&lt;br /&gt;&lt;br /&gt;Thus, the function has a total of 5 parameters:-&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;• The text to be displayed.&lt;br /&gt;&lt;br /&gt;• The font in which the text is to be displayed.&lt;br /&gt;&lt;br /&gt;• The text color or the brush.&lt;br /&gt;&lt;br /&gt;• The x co-ordinate.&lt;br /&gt;&lt;br /&gt;• The y co-ordinate.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Here, we have specified certain values, but every time we use DrawString, we can&lt;br /&gt;conveniently specify different values for these parameters. Thus, the second&lt;br /&gt;DrawString function can display different text and use a different font or&lt;br /&gt;brush. As the system does not have a default brush or font, we call it a&lt;br /&gt;Stateless Model.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Screen 4.12&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Along with 'Vijay Mukhi', we have used a variable called ii, which has been&lt;br /&gt;initialized to 0. In the OnPaint function, we increment this variable by 1.&lt;br /&gt;Before the window is displayed, function OnPaint gets called. Thereafter,&lt;br /&gt;OnPaint gets called whenever the 'minimize' and 'maximize' buttons of the window&lt;br /&gt;are clicked.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;The function OnPaint gets called whenever our client area has to be redrawn due&lt;br /&gt;to any action carried out by the user. This function has to be marked with the&lt;br /&gt;modifier named protected. This is because the original function in the Form&lt;br /&gt;class is tagged with this modifier. We can override a function of the base&lt;br /&gt;class, provided we do not change any of the modifiers. By making OnPaint&lt;br /&gt;protected, only derived classes can use the OnPaint function.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;a.cs&lt;br /&gt;&lt;br /&gt;using System.Windows.Forms;&lt;br /&gt;&lt;br /&gt;using System.Drawing;&lt;br /&gt;&lt;br /&gt;public class zzz: Form {&lt;br /&gt;&lt;br /&gt;public static void Main()&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;Application.Run(new zzz());&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;Brush b; int ii = 0;&lt;br /&gt;&lt;br /&gt;protected override void OnPaint(PaintEventArgs e)&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;Graphics g = e.Graphics;&lt;br /&gt;&lt;br /&gt;b = new SolidBrush(Color.Blue);&lt;br /&gt;&lt;br /&gt;ii++;&lt;br /&gt;&lt;br /&gt;g.DrawString("Vijay Mukhi " + ii, Font, b ,1,25);&lt;br /&gt;&lt;br /&gt;RectangleF r = new RectangleF(20, 60, 100, 25);&lt;br /&gt;&lt;br /&gt;g.FillRectangle(new SolidBrush(Color.Gainsboro), r);&lt;br /&gt;&lt;br /&gt;g.DrawString("Sonal Mukhi", Font, new SolidBrush(Color.Red), r);&lt;br /&gt;&lt;br /&gt;StringFormat f = new StringFormat();&lt;br /&gt;&lt;br /&gt;f.Alignment=StringAlignment.Center;&lt;br /&gt;&lt;br /&gt;RectangleF r1 = new RectangleF(20, 100, 100, 25);&lt;br /&gt;&lt;br /&gt;g.DrawString("Sonal Mukhi", Font, new SolidBrush(Color.Black), r1,f);&lt;br /&gt;&lt;br /&gt;g.RotateTransform(-30);&lt;br /&gt;&lt;br /&gt;g.TranslateTransform(0, 100);&lt;br /&gt;&lt;br /&gt;g.DrawString("vijay mukhi", Font, new SolidBrush(Color.Orange), 20, 40);&lt;br /&gt;&lt;br /&gt;g.ResetTransform();&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Screen 4.13&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;The output of this program is a window with text displayed haphazardly. This&lt;br /&gt;output is nothing to write home about, but is useful in elucidating numerous&lt;br /&gt;concepts.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;A rectangleF structure stores two point objects i.e. it specifies a rectangular&lt;br /&gt;area of the window. We start at one corner, where the x and y co-ordinates are&lt;br /&gt;20 and 60, and the opposite corner where the x and y co-ordinates are 100 and 25&lt;br /&gt;respectively. The function FillRectangle from the Graphics class is used to&lt;br /&gt;create and fill the above rectangular portion of the screen with the color&lt;br /&gt;Gainsboro. The DrawString function is overloaded to take not only x and y as the&lt;br /&gt;last two parameters, but also a rectangular area into which it will draw a&lt;br /&gt;string.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;We would now like to center the above string in the rectangular area. This is&lt;br /&gt;easier said than done, because, it entails creation of an object that looks like&lt;br /&gt;StringFormat with the Alignment property set as Center. The documentation&lt;br /&gt;specifies many more options that can be implemented. The StringFormat object is&lt;br /&gt;passed as the last parameter to the DrawString function, resulting in the string&lt;br /&gt;being shown as centered, instead of being Left aligned, which is the default&lt;br /&gt;setting.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;If we want to rotate the image by 30 degrees, we just have to call a function&lt;br /&gt;named RotateTransform from the Graphics class and pass as a parameter, the&lt;br /&gt;amount of rotation that is required. You can then watch the image get displayed&lt;br /&gt;at the specified angle. Beware, too acute an angle may sprain your neck! The&lt;br /&gt;next function, named TranslateTransform, is optional. It is used to move the&lt;br /&gt;text around in the client area horizontally or vertically. Whenever we transform&lt;br /&gt;something, it stays in the transformed position. But thereafter, if we do not&lt;br /&gt;want the other objects to be in this form, we need to use the function&lt;br /&gt;ResetTranform to undo the transform. However, it is optional.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;a.cs&lt;br /&gt;&lt;br /&gt;using System.Windows.Forms;&lt;br /&gt;&lt;br /&gt;using System.Drawing;&lt;br /&gt;&lt;br /&gt;public class zzz: Form&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;public static void Main()&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;Application.Run(new zzz());&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;Brush b;&lt;br /&gt;&lt;br /&gt;protected override void OnPaint(PaintEventArgs e)&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;Graphics g = e.Graphics;&lt;br /&gt;&lt;br /&gt;b = new SolidBrush(Color.FromArgb(180, Color.Black));&lt;br /&gt;&lt;br /&gt;RectangleF r = new RectangleF(20, 20, 50, 50);&lt;br /&gt;&lt;br /&gt;g.FillRectangle(b, r);&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Screen 4.14&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;The topic of Brushes is so exhaustive that a thesis can well be written on it.&lt;br /&gt;In this program, we use a special brush to fill up a rectangular area on our&lt;br /&gt;screen.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Here, we specify not only a color, but also a number, which is the alpha value&lt;br /&gt;and has a range from 0 to 255. The larger the value, the darker will be the&lt;br /&gt;color. To put it technically, the larger the value, the lesser will be the&lt;br /&gt;translucence and vice-versa.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;a.cs&lt;br /&gt;&lt;br /&gt;using System.Windows.Forms;&lt;br /&gt;&lt;br /&gt;using System.Drawing;&lt;br /&gt;&lt;br /&gt;public class zzz: Form&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;public static void Main()&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;Application.Run(new zzz());&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;Brush b;&lt;br /&gt;&lt;br /&gt;protected override void OnPaint(PaintEventArgs e)&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;Graphics g = e.Graphics;&lt;br /&gt;&lt;br /&gt;b = new SolidBrush(Color.Black);&lt;br /&gt;&lt;br /&gt;Font f = new Font("Times New Roman", 30);&lt;br /&gt;&lt;br /&gt;g.DrawString("Vijay Mukhi " , f , b ,1,25);&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Screen 4.15&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;In this program, we will shed light on Fonts. When you read a newspaper or&lt;br /&gt;magazine, the style of the letters looks different in each of them. This&lt;br /&gt;difference is due to the Font or the Typeface used. There are numerous fonts in&lt;br /&gt;the world of letters.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;While displaying text, we can be very specific about the way in which the&lt;br /&gt;letters look. To enhance their visual appeal, we create an object that looks&lt;br /&gt;like Font. Then, in the constructor, the Name of the font is specified along&lt;br /&gt;with the Size in points. Remember that 72 points make an inch. Thus my name,&lt;br /&gt;Vijay Mukhi, now gets displayed in a size that is bigger than normal.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;a.cs&lt;br /&gt;&lt;br /&gt;using System.Windows.Forms;&lt;br /&gt;&lt;br /&gt;using System.Drawing;&lt;br /&gt;&lt;br /&gt;public class zzz: Form&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;public static void Main()&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;Application.Run(new zzz());&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;protected override void OnPaint(PaintEventArgs e)&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;Graphics g = e.Graphics;&lt;br /&gt;&lt;br /&gt;Image i;&lt;br /&gt;&lt;br /&gt;i = new Bitmap("sample.jpg");&lt;br /&gt;&lt;br /&gt;g.DrawImage(i, 29, 20, 283, 212);&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Screen 4.16&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;The above program merely displays an image. A file with a jpg or a gif extension&lt;br /&gt;contains images or pictures. To display images, we use a class called Image that&lt;br /&gt;can recognize pictures. Even though i is an image object, we initialize it to an&lt;br /&gt;object that looks like Bitmap. An Image class is an abstract class and the class&lt;br /&gt;Bitmap derives from it.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;An Image class could represent a picture, which is not just an image, but could&lt;br /&gt;also be a cursor, icon etc. The DrawImage function accepts an image object as&lt;br /&gt;the first parameter, followed by the screen co-ordinates at which the image has&lt;br /&gt;to be positioned. The above .jpg file is part of the samples offered while&lt;br /&gt;installing the .NET SDK. So, search for the file and copy it to the current&lt;br /&gt;working directory. Like the text sample, this picture can also be rotated,&lt;br /&gt;transformed etc.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;a.cs&lt;br /&gt;&lt;br /&gt;using System.Windows.Forms;&lt;br /&gt;&lt;br /&gt;using System.Drawing;&lt;br /&gt;&lt;br /&gt;public class zzz: Form&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;public static void Main()&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;Application.Run(new zzz());&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;protected override void OnPaint(PaintEventArgs e)&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;Graphics g = e.Graphics;&lt;br /&gt;&lt;br /&gt;Image i = new Bitmap("colorbars.jpg");&lt;br /&gt;&lt;br /&gt;Brush b = new TextureBrush(i);&lt;br /&gt;&lt;br /&gt;g.DrawString("Vijay Mukhi is very smart" , Font, b ,1,25);&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Screen 4.17&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;By combining a Brush and an image, we can create a multicolor brush. In one of&lt;br /&gt;the earlier programs, we had used a Solid brush. Here, we are using a Texture&lt;br /&gt;brush. This brush fills the interiors of a shape with a picture.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Thus, the text gets reflected in a brush, which reminds us of a rainbow. You can&lt;br /&gt;enhance the aesthetic appeal of your applications by using this facility.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;a.cs&lt;br /&gt;&lt;br /&gt;using System.Windows.Forms;&lt;br /&gt;&lt;br /&gt;using System.Drawing;&lt;br /&gt;&lt;br /&gt;using System.Drawing.Drawing2D;&lt;br /&gt;&lt;br /&gt;public class zzz: Form {&lt;br /&gt;&lt;br /&gt;public static void Main()&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;Application.Run(new zzz());&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;protected override void OnPaint(PaintEventArgs e)&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;Graphics g = e.Graphics;&lt;br /&gt;&lt;br /&gt;Pen p = new Pen(Color.FromArgb(150, Color.Purple), 20);&lt;br /&gt;&lt;br /&gt;p.DashStyle = DashStyle.Dash;&lt;br /&gt;&lt;br /&gt;p.StartCap = LineCap.Round;&lt;br /&gt;&lt;br /&gt;Point [] pp = new Point[] {new Point(200, 140),new Point(700, 240),new&lt;br /&gt;Point(500, 340)};&lt;br /&gt;&lt;br /&gt;g.DrawCurve(p,pp);&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Screen 4.18&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;The above program introduces freehand drawing. A pen is like an artist's brush,&lt;br /&gt;which is used to draw any shape that permeates the mind. In our program, we&lt;br /&gt;commence by creating a Pen object p. It is initialized to a particular alpha&lt;br /&gt;color using FromArgb function from the Color class, and to a specified width.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;The constructor can also be provided with other parameters, such as a brush. A&lt;br /&gt;pen is used to draw lines and curves.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;A Pen can also draw a line of a specified width and style. The default DashStyle&lt;br /&gt;is Continuous. If we change the DashStyle to Dash, the starting point becomes a&lt;br /&gt;rounded edge. The default is a Straight Edge. The line drawn by a pen is very&lt;br /&gt;versatile, and can employ a variety of fill styles, colors and textures. The&lt;br /&gt;DrawCurve function paints a pen object that specifies how to draw a curve. It&lt;br /&gt;has an array of points with the individual three point objects specifying where&lt;br /&gt;the curved line should be drawn.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;a.cs&lt;br /&gt;&lt;br /&gt;using System.Windows.Forms;&lt;br /&gt;&lt;br /&gt;using System.Drawing;&lt;br /&gt;&lt;br /&gt;using System.Drawing.Drawing2D;&lt;br /&gt;&lt;br /&gt;public class zzz: Form {&lt;br /&gt;&lt;br /&gt;public static void Main() {&lt;br /&gt;&lt;br /&gt;Application.Run(new zzz());&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;protected override void OnPaint(PaintEventArgs e)&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;Graphics g = e.Graphics;&lt;br /&gt;&lt;br /&gt;Image i= new Bitmap("BoilingPoint.jpg");&lt;br /&gt;&lt;br /&gt;Brush pb = new TextureBrush(i);&lt;br /&gt;&lt;br /&gt;Pen p= new Pen(pb, 75);&lt;br /&gt;&lt;br /&gt;g.DrawLine(p,1,5,150,200);&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Screen 4.19&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;We can use a brush that looks like an image and create a pen that will draw&lt;br /&gt;lines in the garb of a picture. The DrawLine function accepts two sets of&lt;br /&gt;numbers, the x-y co-ordinates of the starting point and the x-y co-ordinates of&lt;br /&gt;the ending point. It draws a thick line joining these two points. Thus, we can&lt;br /&gt;use this function to draw any possible shape.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;a.cs&lt;br /&gt;&lt;br /&gt;using System.Windows.Forms;&lt;br /&gt;&lt;br /&gt;using System.Drawing;&lt;br /&gt;&lt;br /&gt;using System.Drawing.Drawing2D;&lt;br /&gt;&lt;br /&gt;public class zzz: Form&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;public static void Main()&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;Application.Run(new zzz());&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;protected override void OnPaint(PaintEventArgs e)&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;Graphics g = e.Graphics;&lt;br /&gt;&lt;br /&gt;HatchBrush b = new HatchBrush(HatchStyle.ForwardDiagonal, Color.Green,&lt;br /&gt;Color.FromArgb(100, Color.Yellow));&lt;br /&gt;&lt;br /&gt;g.FillEllipse(b, 250, 10, 100, 100);&lt;br /&gt;&lt;br /&gt;Rectangle r = new Rectangle(300, 250, 100, 100);&lt;br /&gt;&lt;br /&gt;LinearGradientBrush lb = new LinearGradientBrush(r, Color.Red,&lt;br /&gt;Color.Yellow,LinearGradientMode.BackwardDiagonal);&lt;br /&gt;&lt;br /&gt;g.FillRectangle(lb, r);&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Screen 4.20&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;On maximizing the screen, we see two figures; one is a filled circle, while the&lt;br /&gt;other is a rectangular block. We are equipped with a large number of brushes&lt;br /&gt;akin to those in the artistic world. One of them is a HatchBrush. The&lt;br /&gt;constructor of HatchBrush accepts a hatch style and two colors, viz. a&lt;br /&gt;background color and a foreground color.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;The first parameter is the hatch style, which can be one of six possible hatch&lt;br /&gt;styles. The foreground color, in this case, green, defines the color of the&lt;br /&gt;lines to be drawn and the background color defines the color for the gaps&lt;br /&gt;between the lines.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;The FillEllipse function in Graphics fills up the shape to display the effect of&lt;br /&gt;the brush. We could have used the Rectangle function also, but as we are trying&lt;br /&gt;to be as akin as possible to the samples provided by Microsoft, we have used the&lt;br /&gt;Ellipse function.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;A LinearGradientBrush can represent color gradients and multi-color gradients. A&lt;br /&gt;gradient represents a transformation from one color to another. A linear&lt;br /&gt;gradient is defined alongside a line that is specified by the width of a&lt;br /&gt;rectangle or by any two points. Thus, a two-color gradient will commence with a&lt;br /&gt;starting color and conclude with the ending color. The blend from one color to&lt;br /&gt;the next can be customized. First, we specify the object that is to be colored,&lt;br /&gt;which is a rectangle in this case. The gradient starts with the left corner and&lt;br /&gt;ends at the lower right corner. Thereafter, we follow with the starting color&lt;br /&gt;followed by the ending color. Finally, the angle measured in degrees in the&lt;br /&gt;clockwise direction is mentioned, starting from the x-axis. This defines the&lt;br /&gt;orientation of the gradient. You can change the angle and witness the&lt;br /&gt;spectacular effects.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Menus&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;a.cs&lt;br /&gt;&lt;br /&gt;using System.Windows.Forms;&lt;br /&gt;&lt;br /&gt;using System.Drawing;&lt;br /&gt;&lt;br /&gt;using System.Drawing.Drawing2D;&lt;br /&gt;&lt;br /&gt;public class zzz: Form&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;public static void Main()&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;Application.Run(new zzz());&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;MainMenu m;&lt;br /&gt;&lt;br /&gt;public zzz()&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;m = new MainMenu();&lt;br /&gt;&lt;br /&gt;MenuItem mi= m.MenuItems.Add("&amp;amp;File");&lt;br /&gt;&lt;br /&gt;Menu = m;&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Screen 4.21&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Let us now build a menu. We have created an object m, which symbolizes the&lt;br /&gt;MainMenu. MainMenu is called a control and represents the menu structure for a&lt;br /&gt;Form. It is the root of the menu.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;A menu consists of various menu items, which are displayed horizontally across&lt;br /&gt;the menu. We want to create a menu item that displays the word 'File For this,&lt;br /&gt;we need another class called MenuItem. A MenuItem can represent either an&lt;br /&gt;individual menu item depicting a command, or it can cascade to another popup of&lt;br /&gt;menu items.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;MenuItems is a read-only property in MainMenu that gives a reference to all the&lt;br /&gt;MenuItems currently available in the menu. We have none so far. This&lt;br /&gt;CollectionObject also has a function called Add, which is used to add menu&lt;br /&gt;items. To do so, the text of the item that is to be displayed must be stated as&lt;br /&gt;the parameter to the Add function. We can also remove any menu item that has&lt;br /&gt;been previously added.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;The variable mi stores the MenuItem object returned by the Add function.&lt;br /&gt;Thereafter, Menu, which is an object of type MainMenu, available in Form, is&lt;br /&gt;initialized to the menu that we desire. The appearance of the menu depends upon&lt;br /&gt;the menu object stored in Menu.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;When we run the program, we see the word File displayed in the top left corner.&lt;br /&gt;At this stage, nothing happens when we click on it. On pressing the Alt key, F&lt;br /&gt;is displayed as underlined since the symbol &amp;amp; underlines the character it is&lt;br /&gt;preceded with.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;a.cs&lt;br /&gt;&lt;br /&gt;using System.Windows.Forms;&lt;br /&gt;&lt;br /&gt;using System.Drawing;&lt;br /&gt;&lt;br /&gt;using System.Drawing.Drawing2D;&lt;br /&gt;&lt;br /&gt;public class zzz: Form {&lt;br /&gt;&lt;br /&gt;public static void Main() {&lt;br /&gt;&lt;br /&gt;Application.Run(new zzz());&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;MainMenu m;&lt;br /&gt;&lt;br /&gt;public zzz() {&lt;br /&gt;&lt;br /&gt;m = new MainMenu();&lt;br /&gt;&lt;br /&gt;MenuItem mi= m.MenuItems.Add("&amp;amp;File");&lt;br /&gt;&lt;br /&gt;mi.MenuItems.Add("Hi");&lt;br /&gt;&lt;br /&gt;mi.MenuItems.Add("-");&lt;br /&gt;&lt;br /&gt;mi.MenuItems.Add("Bye");&lt;br /&gt;&lt;br /&gt;Menu = m;&lt;br /&gt;&lt;br /&gt;} }&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Screen 4.22&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Now things look more visually attractive. When we click on File or use the&lt;br /&gt;accelerator Alt-F, a menu pops up with the word 'Hi', followed by a separator&lt;br /&gt;and then finally by the word 'Bye'.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;A separator is used to logically group menus together. However, when we click on&lt;br /&gt;'hi' or 'bye', nothing happens. This situation needs to be redressed, since a&lt;br /&gt;menu should activate some code.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;a.cs&lt;br /&gt;&lt;br /&gt;using System.Windows.Forms;&lt;br /&gt;&lt;br /&gt;using System.Drawing;&lt;br /&gt;&lt;br /&gt;using System.Drawing.Drawing2D;&lt;br /&gt;&lt;br /&gt;public class zzz: Form&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;public static void Main()&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;Application.Run(new zzz());&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;MainMenu m;&lt;br /&gt;&lt;br /&gt;public zzz()&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;m = new MainMenu();&lt;br /&gt;&lt;br /&gt;MenuItem mi= m.MenuItems.Add("&amp;amp;File");&lt;br /&gt;&lt;br /&gt;MenuItem m1;&lt;br /&gt;&lt;br /&gt;m1 = new MenuItem("Hi", new System.EventHandler(abc), Shortcut.CtrlF11);&lt;br /&gt;&lt;br /&gt;mi.MenuItems.Add(m1);&lt;br /&gt;&lt;br /&gt;mi.MenuItems.Add("Bye");&lt;br /&gt;&lt;br /&gt;Menu = m;&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;void abc(object sender, System.EventArgs e)&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;MessageBox.Show("hell");&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Screen 4.24&lt;br /&gt;&lt;br /&gt;Screen 4.23&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Now, whether you either click on File and then on the word 'Hi', or you press&lt;br /&gt;Control+F11, you will see a message box with the word "hell" displayed in it.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;The MenuItem constructor is overloaded. The first parameter is the text to be&lt;br /&gt;displayed. The second parameter is a delegate that encompasses the function to&lt;br /&gt;be called whenever this menu item is activated. Just as the pen is mightier than&lt;br /&gt;the sword, under some circumstances, the keyboard is certainly mightier than the&lt;br /&gt;mouse. At times, it is faster to use a keyboard shortcut, instead of using the&lt;br /&gt;mouse. Thus, the last parameter is the keyboard shortcut key, which is part of&lt;br /&gt;an enumerator. This MenuItem object is passed to the Add function, that either&lt;br /&gt;accepts a string or a MenuItem object.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;a.cs&lt;br /&gt;&lt;br /&gt;using System.Windows.Forms;&lt;br /&gt;&lt;br /&gt;using System.Drawing;&lt;br /&gt;&lt;br /&gt;using System.Drawing.Drawing2D;&lt;br /&gt;&lt;br /&gt;public class zzz: Form&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;public static void Main()&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;Application.Run(new zzz());&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;MainMenu m;&lt;br /&gt;&lt;br /&gt;public zzz()&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;m = new MainMenu();&lt;br /&gt;&lt;br /&gt;MenuItem mi= m.MenuItems.Add("&amp;amp;File");&lt;br /&gt;&lt;br /&gt;MenuItem a = new MenuItem("One",new System.EventHandler(abc));&lt;br /&gt;&lt;br /&gt;MenuItem b = new MenuItem("two",new System.EventHandler(abc));&lt;br /&gt;&lt;br /&gt;mi.MenuItems.Add("hell",(new MenuItem[]{ a, b })&lt;br /&gt;&lt;br /&gt;);&lt;br /&gt;&lt;br /&gt;Menu = m;&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;void abc(object sender, System.EventArgs e)&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;MessageBox.Show("hell");&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Screen 4.26&lt;br /&gt;&lt;br /&gt;Screen 4.25&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Here, we have a popup within a popup. When you click on File, you will see the&lt;br /&gt;word 'hell' displayed. You will also see an arrow pointing to the right, along&lt;br /&gt;with the menu item. If you move the mouse over the arrow, a popup is displayed,&lt;br /&gt;containing the two menu items 'one' and 'two'. If we click on them, a message&lt;br /&gt;box with the word 'hell' gets displayed.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;In the program, with a single statement, we have created two menu items, a and&lt;br /&gt;b, followed by an array of menu items. This array is then passed as the last&lt;br /&gt;parameter to the Add function. Thus, all the menus become sub-menus. In this&lt;br /&gt;case, the event handler is associated with the submenu options, since clicking&lt;br /&gt;on the menu item displays the sub-menu.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;a.cs&lt;br /&gt;&lt;br /&gt;using System.Windows.Forms;&lt;br /&gt;&lt;br /&gt;using System.Drawing;&lt;br /&gt;&lt;br /&gt;using System.Drawing.Drawing2D;&lt;br /&gt;&lt;br /&gt;public class zzz: Form&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;public static void Main()&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;Application.Run(new zzz());&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;MainMenu m;&lt;br /&gt;&lt;br /&gt;public zzz()&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;m = new MainMenu();&lt;br /&gt;&lt;br /&gt;MenuItem mi= m.MenuItems.Add("&amp;amp;File");&lt;br /&gt;&lt;br /&gt;mi.MenuItems.Add( "hell",new System.EventHandler(abc));&lt;br /&gt;&lt;br /&gt;mi.MenuItems.Add( "Bye",new System.EventHandler(abc));&lt;br /&gt;&lt;br /&gt;Menu = m;&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;void abc(object s, System.EventArgs e)&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;MenuItem m = (MenuItem) s;&lt;br /&gt;&lt;br /&gt;if ( m.Checked)&lt;br /&gt;&lt;br /&gt;m.Checked = false;&lt;br /&gt;&lt;br /&gt;else&lt;br /&gt;&lt;br /&gt;m.Checked = true;&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Screen 4.27 Screen 4.28&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;We add two menu items, 'hell' and 'Bye' to our File menu and assign the same&lt;br /&gt;function abc to handle their events. Clicking on any one of the menu options&lt;br /&gt;results in a call to the function abc. This function takes two parameters. The&lt;br /&gt;first parameter s, represents the menu item that was clicked on. If the first&lt;br /&gt;menu option 'hell' is clicked, then the parameter s is not an object, but a menu&lt;br /&gt;item representing 'hell' and vice versa.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Every MenuItem has an option called Checked, which if True, will display a tick&lt;br /&gt;mark on the menu item. Thus, you can Check or Uncheck a menu option by clicking&lt;br /&gt;on it. You may click on each menu option to observe this effect.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;a.cs&lt;br /&gt;&lt;br /&gt;using System.Windows.Forms;&lt;br /&gt;&lt;br /&gt;using System.Drawing;&lt;br /&gt;&lt;br /&gt;using System.Drawing.Drawing2D;&lt;br /&gt;&lt;br /&gt;public class zzz: Form&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;public static void Main()&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;Application.Run(new zzz());&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;MainMenu m;&lt;br /&gt;&lt;br /&gt;public zzz()&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;m = new MainMenu();&lt;br /&gt;&lt;br /&gt;MenuItem mi= m.MenuItems.Add("&amp;amp;File");&lt;br /&gt;&lt;br /&gt;mi.MenuItems.Add( "hell",new System.EventHandler(abc));&lt;br /&gt;&lt;br /&gt;mi.MenuItems.Add( "Bye",new System.EventHandler(abc));&lt;br /&gt;&lt;br /&gt;Label l = new Label();&lt;br /&gt;&lt;br /&gt;ContextMenu lm;&lt;br /&gt;&lt;br /&gt;lm = new ContextMenu();&lt;br /&gt;&lt;br /&gt;l.ContextMenu = lm;&lt;br /&gt;&lt;br /&gt;l.Text = "Vijay Mukhi";&lt;br /&gt;&lt;br /&gt;lm.MenuItems.Add(mi.CloneMenu());&lt;br /&gt;&lt;br /&gt;Controls.Add(l );&lt;br /&gt;&lt;br /&gt;Menu = m;&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;void abc(object s, System.EventArgs e)&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;MenuItem m = (MenuItem) s ;&lt;br /&gt;&lt;br /&gt;if ( m.Checked)&lt;br /&gt;&lt;br /&gt;m.Checked = false;&lt;br /&gt;&lt;br /&gt;else&lt;br /&gt;&lt;br /&gt;m.Checked = true;&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;The program output will display the same menu - File, as seen in the earlier&lt;br /&gt;program. The text Vijay Mukhi will also be visible. If you place the mouse on&lt;br /&gt;this text and right click the mouse, you will see the same menu as seen with the&lt;br /&gt;File option.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Screen 4.29 Screen 4.30&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;This is called a Context Sensitive Menu. These two menus, however, are&lt;br /&gt;different. If you Check a menu option in this menu by clicking on it, it does&lt;br /&gt;not carry the tick mark to the other menu.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;We first create two objects:&lt;br /&gt;&lt;br /&gt;• The first is lm, which looks like a ContextMenu.&lt;br /&gt;&lt;br /&gt;• The second is l, which looks like a label.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Screen 4.31&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Every label has a member called ContextMenu, wherein we can specify a Context&lt;br /&gt;Sensitive menu. This member is initialized to lm. As we have already created a&lt;br /&gt;menu item mi, we can reuse this menu item.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;However, a menu item cannot be used twice. Hence, calling the function CloneMenu&lt;br /&gt;off MenuItem creates a clone. This clone menu is then passed to the Add function&lt;br /&gt;of MenuItems in the ContextMenu.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Writing Controls&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Let us start by creating the simplest control that money can buy. We create the&lt;br /&gt;following files:&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;c.cs&lt;br /&gt;&lt;br /&gt;using System.Windows.Forms;&lt;br /&gt;&lt;br /&gt;using System.Drawing;&lt;br /&gt;&lt;br /&gt;public class yyy : Control&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;h.cs&lt;br /&gt;&lt;br /&gt;using System.Windows.Forms;&lt;br /&gt;&lt;br /&gt;public class zzz : System.Windows.Forms.Form&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;yyy a;&lt;br /&gt;&lt;br /&gt;public zzz()&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;a = new yyy();&lt;br /&gt;&lt;br /&gt;Controls.Add(a);&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;public static void Main() {&lt;br /&gt;&lt;br /&gt;Application.Run(new zzz());&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;a.bat&lt;br /&gt;&lt;br /&gt;del *.exe&lt;br /&gt;&lt;br /&gt;del *.dll&lt;br /&gt;&lt;br /&gt;csc.exe /t:library c.cs&lt;br /&gt;&lt;br /&gt;csc.exe /r:c.dll h.cs&lt;br /&gt;&lt;br /&gt;h&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;The file c.cs contains our very first custom control. In order to create our own&lt;br /&gt;user-defined control, we create a class yyy and derive it from the Control&lt;br /&gt;class. The Control class implements the basic code required by classes to&lt;br /&gt;implement the behavior of a control or a widget. This code can handle user input&lt;br /&gt;with a keyboard or a pointing device, such as a mouse. Message handling and&lt;br /&gt;security features are also supported.&lt;br /&gt;&lt;br /&gt;At the end of the day, all controls are merely child windows. The Control class&lt;br /&gt;defines the area or bounds of a control along with the fonts, colors and images.&lt;br /&gt;This class allows painting, context menus and anchoring with docking behavior.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Earlier, we had displayed scores of controls in our containers, written by the&lt;br /&gt;Microsoft developers. All these controls were derived from the Control class.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Screen 4.32&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Thus, a user control like yyy is an instance of a Control class, which is added&lt;br /&gt;to the Form using the Add function off the Controls collection. It can't get any&lt;br /&gt;simpler. On running the executable, we see no output. Yet, since no error was&lt;br /&gt;generated, we presume that all went well.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;The major difference between the Microsoft controls and our controls is, the&lt;br /&gt;file in which the code for the control is finally placed. We have placed our&lt;br /&gt;control code in assembly c.dll, whereas, Microsoft controls are placed in&lt;br /&gt;System.Windows.Forms.dll.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;c.cs&lt;br /&gt;&lt;br /&gt;using System.Windows.Forms;&lt;br /&gt;&lt;br /&gt;using System.Drawing;&lt;br /&gt;&lt;br /&gt;public class yyy : Control&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;protected override void OnPaint(PaintEventArgs e)&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;e.Graphics.DrawString(Text,Font, new SolidBrush(ForeColor), ClientRectangle);&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;h.cs&lt;br /&gt;&lt;br /&gt;using System.Windows.Forms;&lt;br /&gt;&lt;br /&gt;public class zzz : System.Windows.Forms.Form{&lt;br /&gt;&lt;br /&gt;yyy a;&lt;br /&gt;&lt;br /&gt;public zzz() {&lt;br /&gt;&lt;br /&gt;a = new yyy();&lt;br /&gt;&lt;br /&gt;a.Size = new System.Drawing.Size(600, 450);&lt;br /&gt;&lt;br /&gt;a.Text = "Vijay Mukhi";&lt;br /&gt;&lt;br /&gt;Controls.Add(a);&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;public static void Main() {&lt;br /&gt;&lt;br /&gt;Application.Run(new zzz());&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Screen 4.33&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;In the above example, we have overridden a function called OnPaint in the&lt;br /&gt;Control class. This function gets called whenever a control is to be redrawn on&lt;br /&gt;the screen. It is passed a PaintEventArgs object as a parameter, from where we&lt;br /&gt;summon the DrawString function to paint a string in a specified font and color,&lt;br /&gt;at a particular location. The first parameter, Text, is a property, which refers&lt;br /&gt;to the string to be displayed. The string 'Vijay Mukhi' is presently displayed&lt;br /&gt;in the window.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;In the container h.cs, we have initialized the property Text contained in the&lt;br /&gt;Control class to 'Vijay Mukhi'. The Size property is also initialized, so that&lt;br /&gt;our control has a specific size in the container.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;h.cs&lt;br /&gt;&lt;br /&gt;using System.Drawing;&lt;br /&gt;&lt;br /&gt;using System.Windows.Forms;&lt;br /&gt;&lt;br /&gt;public class zzz : Form {&lt;br /&gt;&lt;br /&gt;Button b;&lt;br /&gt;&lt;br /&gt;ccc c;&lt;br /&gt;&lt;br /&gt;public zzz() {&lt;br /&gt;&lt;br /&gt;b = new Button();&lt;br /&gt;&lt;br /&gt;c = new ccc();&lt;br /&gt;&lt;br /&gt;b.Anchor = System.Windows.Forms.AnchorStyles.Bottom;&lt;br /&gt;&lt;br /&gt;b.DialogResult = System.Windows.Forms.DialogResult.OK;&lt;br /&gt;&lt;br /&gt;b.FlatStyle = System.Windows.Forms.FlatStyle.Flat;&lt;br /&gt;&lt;br /&gt;b.Size = new System.Drawing.Size(96, 24);&lt;br /&gt;&lt;br /&gt;b.Text = "&amp;amp;Save";&lt;br /&gt;&lt;br /&gt;b.Location = new System.Drawing.Point(8, 328);&lt;br /&gt;&lt;br /&gt;b.Click += new System.EventHandler(abc);&lt;br /&gt;&lt;br /&gt;Text = "Sonal Mukhi";&lt;br /&gt;&lt;br /&gt;AcceptButton = b;&lt;br /&gt;&lt;br /&gt;ClientSize = new System.Drawing.Size(400, 373);&lt;br /&gt;&lt;br /&gt;c.Anchor=AnchorStyles.Top AnchorStyles.Bottom AnchorStyles.Left&lt;br /&gt;AnchorStyles.Right;&lt;br /&gt;&lt;br /&gt;c.AutoScrollMinSize = new System.Drawing.Size(0, 0);&lt;br /&gt;&lt;br /&gt;c.Size = new System.Drawing.Size(400, 310);&lt;br /&gt;&lt;br /&gt;c.Text = "Vijay Mukhi";&lt;br /&gt;&lt;br /&gt;Controls.Add(b);&lt;br /&gt;&lt;br /&gt;Controls.Add(c);&lt;br /&gt;&lt;br /&gt;c.cust = ddd.rrr();&lt;br /&gt;&lt;br /&gt;Size = new Size(400, (373 + SystemInformation.CaptionHeight));&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;void abc(object sender, System.EventArgs e)&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;c.aaa();&lt;br /&gt;&lt;br /&gt;MessageBox.Show("vijay "+ c.cust);&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;public static void Main(string[] args)&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;Application.Run(new zzz());&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;c.cs&lt;br /&gt;&lt;br /&gt;using System;&lt;br /&gt;&lt;br /&gt;using System.Windows.Forms;&lt;br /&gt;&lt;br /&gt;using System.Drawing;&lt;br /&gt;&lt;br /&gt;public class ccc : UserControl&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;TextBox t;&lt;br /&gt;&lt;br /&gt;TextBox ID;&lt;br /&gt;&lt;br /&gt;Label l;&lt;br /&gt;&lt;br /&gt;ddd c;&lt;br /&gt;&lt;br /&gt;public ccc()&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;t = new TextBox();&lt;br /&gt;&lt;br /&gt;l = new Label();&lt;br /&gt;&lt;br /&gt;ID = new TextBox();&lt;br /&gt;&lt;br /&gt;Text = "Vijay Mukhi";&lt;br /&gt;&lt;br /&gt;Size = new System.Drawing.Size(384, 304);&lt;br /&gt;&lt;br /&gt;t.Size = new System.Drawing.Size(88, 20);&lt;br /&gt;&lt;br /&gt;t.Location = new System.Drawing.Point(88, 70);&lt;br /&gt;&lt;br /&gt;l.Size = new System.Drawing.Size(64, 16);&lt;br /&gt;&lt;br /&gt;l.Location = new System.Drawing.Point(8, 32);&lt;br /&gt;&lt;br /&gt;l.Text = "ID:";&lt;br /&gt;&lt;br /&gt;ID.ReadOnly = true;&lt;br /&gt;&lt;br /&gt;ID.Size = new System.Drawing.Size(200, 20);&lt;br /&gt;&lt;br /&gt;ID.Location = new System.Drawing.Point(88, 30);&lt;br /&gt;&lt;br /&gt;ID.Enabled = false;&lt;br /&gt;&lt;br /&gt;Controls.Add(t);&lt;br /&gt;&lt;br /&gt;Controls.Add(ID);&lt;br /&gt;&lt;br /&gt;Controls.Add(l);&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;public ddd cust&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;get&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;return c;&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;set&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;c=value;&lt;br /&gt;&lt;br /&gt;ID.Text = c.ID;&lt;br /&gt;&lt;br /&gt;t.Text = c.ti;&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;public void aaa()&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;c.ti = t.Text;&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;cc.cs&lt;br /&gt;&lt;br /&gt;using System;&lt;br /&gt;&lt;br /&gt;using System.ComponentModel;&lt;br /&gt;&lt;br /&gt;using System.IO;&lt;br /&gt;&lt;br /&gt;public class ddd : Component&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;string i ;&lt;br /&gt;&lt;br /&gt;string t ;&lt;br /&gt;&lt;br /&gt;public static ddd rrr()&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;ddd c = new ddd("111");&lt;br /&gt;&lt;br /&gt;c.ti = "Vijay";&lt;br /&gt;&lt;br /&gt;return c;&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;internal ddd(string s): base()&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;i = s ;&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;public string ID&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;get&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;return i ;&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;public string ti&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;get&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;return t ;&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;set&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;t = value ;&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;public override string ToString()&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;StringWriter sb = new StringWriter() ;&lt;br /&gt;&lt;br /&gt;sb.WriteLine("Sonal \n");&lt;br /&gt;&lt;br /&gt;sb.WriteLine(i);&lt;br /&gt;&lt;br /&gt;sb.Write(t);&lt;br /&gt;&lt;br /&gt;return sb.ToString();&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;a.bat&lt;br /&gt;&lt;br /&gt;del *.exe&lt;br /&gt;&lt;br /&gt;del *.dll&lt;br /&gt;&lt;br /&gt;csc.exe /t:library /out:c.dll c.cs cc.cs&lt;br /&gt;&lt;br /&gt;csc.exe /R:c.dll h.cs&lt;br /&gt;&lt;br /&gt;h&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;This program is a rather protracted one. As usual, we start with the container&lt;br /&gt;in h.cs. In the zzz constructor, we first create a button b and an object c that&lt;br /&gt;is an instance of our user control ccc. The control is present in the assembly&lt;br /&gt;c.dll. What this class presently does is not significant. We begin by&lt;br /&gt;initializing a large number of properties in the button.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;The Anchor property decides as to which edges of the control are to be anchored&lt;br /&gt;with the edges of the container. Here, we have chosen the Bottom edge.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;The DialogResult property is the value that is returned to the parent form when&lt;br /&gt;we click on the button. The value returned is OK.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;The FlatStyle property belongs to the ButtonBase class and is one of the&lt;br /&gt;numerous properties that influence the flat style appearance of the button.&lt;br /&gt;Knowledge of GUI programming implies cognizance and comprehension about all the&lt;br /&gt;facets of making your application more comely and pleasing to the eye. The Size,&lt;br /&gt;Text and Location properties were explained earlier.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Each time we click on the button, the function abc gets called. The Text&lt;br /&gt;property decides on the title of the windows. The AcceptButton property requires&lt;br /&gt;an object that represents a button. Every form has a feature, which associates&lt;br /&gt;the Enter key with a button. The resultant effect is that pressing the Enter key&lt;br /&gt;on the keyboard simulates a click on the associated button. Thus, in the above&lt;br /&gt;form, pressing Enter or clicking on the button would result in a call to the&lt;br /&gt;function abc. The ClientSize property decides the size of the windows.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Our User Control too can initialize properties since they belong to the Control&lt;br /&gt;class. In the program, we have set the Anchor, AutoScrollMinSize, Size and Text&lt;br /&gt;properties of our user-defined control class ccc, even though our control may&lt;br /&gt;not have implemented these properties directly.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Using the Add function, we have then added the button and the control ccc to the&lt;br /&gt;form. Finally, we have called a static function rrr from the class ddd that&lt;br /&gt;initializes a property cust from our user-defined control.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;When we run this program, we see two text boxes, a label and a button. It is&lt;br /&gt;obvious that other than the button, the other widgets were created by the class&lt;br /&gt;ccc. This provides ample credence to our belief that our user-defined control&lt;br /&gt;can do its biding.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Screen 4.35&lt;br /&gt;&lt;br /&gt;Screen 4.34&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;We shall now endeavor to comprehend what the constructor of class ccc in file&lt;br /&gt;c.cs is attempting to do. The constructor contains two text boxes, one called ID&lt;br /&gt;to store the id of the user, and the other called t to store the user's name.&lt;br /&gt;The label l is used to display a simple descriptive message. Since we do not&lt;br /&gt;want the user to change the value contained in ID, we assign the value true to&lt;br /&gt;its ReadOnly property and assign the value false to its Enabled property.&lt;br /&gt;Thereafter, we add these three widgets to the form. Hence, we can now see four&lt;br /&gt;widgets on the screen.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;In the container h.cs, we call a static function, rrr off class ddd. The ddd&lt;br /&gt;class is created in cc.cs and is derived from Component. In the rrr function, we&lt;br /&gt;create an object c, which looks like ddd and pass a value of 111 to the&lt;br /&gt;constructor. The constructor of class ddd initializes an instance variable i to&lt;br /&gt;the value contained in s. The variable i stands for the user id.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Class ddd has a property called ti, which is initialized to my name, Vijay. This&lt;br /&gt;property ti sets another instance variable t to the value 'Vijay'. Thus, we have&lt;br /&gt;initialized two instance members of class ddd to specific values.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;The value returned on calling the rrr function is stored in the cust property of&lt;br /&gt;the control c. The class ccc contains the property having type ddd.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;The property Cust stores the ddd object in the variable c for later use. It also&lt;br /&gt;initializes the Text property of the text boxes to the ID and Name of the user&lt;br /&gt;that the two properties in the class ddd were initialized to. Thus, we see '111'&lt;br /&gt;and 'Vijay' displayed in the text boxes.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;When we click on the button labeled 'Save', the function abc gets called. This&lt;br /&gt;function first calls the function aaa from class ccc using the object c. In aaa,&lt;br /&gt;we initialize the ti property of the control to the value present in the&lt;br /&gt;textbox. The ID property is dimmed out, and hence its value can never be&lt;br /&gt;changed. The object c represents the ddd object in class ccc.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;To display a string, an object, whose data type is not a string, has to call the&lt;br /&gt;ToString function in the datatype. The cust property in class ccc has the type&lt;br /&gt;of ddd, which contains the ToString function. This function uses the&lt;br /&gt;StringWriter class to concatenate the word 'Sonal' with the value of the&lt;br /&gt;instance variables i and t, which eventually get displayed in the MessageBox.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;The above program demonstrates two crucial points:&lt;br /&gt;&lt;br /&gt;(a) All the code that refers to the user has been encapsulated in class ddd.&lt;br /&gt;&lt;br /&gt;(b) The user interface code is entered in class ccc.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;The container is oblivious to these classes and does not bother to verify&lt;br /&gt;whether there are two classes or one. While the class ccc contains code&lt;br /&gt;pertaining to User Interface interaction only, the class ddd contains code&lt;br /&gt;relating to the actual object.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;h.cs&lt;br /&gt;&lt;br /&gt;using System.Drawing;&lt;br /&gt;&lt;br /&gt;using System.Windows.Forms;&lt;br /&gt;&lt;br /&gt;public class zzz : Form {&lt;br /&gt;&lt;br /&gt;RadioButton r1,r2;&lt;br /&gt;&lt;br /&gt;GroupBox g1;&lt;br /&gt;&lt;br /&gt;sss s;&lt;br /&gt;&lt;br /&gt;public zzz()&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;r1 = new System.Windows.Forms.RadioButton();&lt;br /&gt;&lt;br /&gt;r2 = new System.Windows.Forms.RadioButton();&lt;br /&gt;&lt;br /&gt;r1.Location = new System.Drawing.Point(24, 24);&lt;br /&gt;&lt;br /&gt;r1.Size = new System.Drawing.Size(128, 24);&lt;br /&gt;&lt;br /&gt;r1.Text = "Vijay";&lt;br /&gt;&lt;br /&gt;r1.Checked = true;&lt;br /&gt;&lt;br /&gt;r1.CheckedChanged += new System.EventHandler(r1f);&lt;br /&gt;&lt;br /&gt;r2.Location = new System.Drawing.Point(24, 64);&lt;br /&gt;&lt;br /&gt;r2.Size = new System.Drawing.Size(128, 24);&lt;br /&gt;&lt;br /&gt;r2.Text = "Mukhi";&lt;br /&gt;&lt;br /&gt;r2.CheckedChanged += new System.EventHandler(r2f);&lt;br /&gt;&lt;br /&gt;g1 = new System.Windows.Forms.GroupBox();&lt;br /&gt;&lt;br /&gt;g1.Size = new System.Drawing.Size(192, 152);&lt;br /&gt;&lt;br /&gt;g1.Text = "Sonal";&lt;br /&gt;&lt;br /&gt;g1.Location = new System.Drawing.Point(320, 16);&lt;br /&gt;&lt;br /&gt;s = new sss();&lt;br /&gt;&lt;br /&gt;Text = "Control Example";&lt;br /&gt;&lt;br /&gt;ClientSize = new System.Drawing.Size(528, 325);&lt;br /&gt;&lt;br /&gt;s.Size = new System.Drawing.Size(304, 328);&lt;br /&gt;&lt;br /&gt;s.TabIndex = 0;&lt;br /&gt;&lt;br /&gt;s.Anchor = AnchorStyles.Left AnchorStyles.Right;&lt;br /&gt;&lt;br /&gt;s.Font = new System.Drawing.Font("TAHOMA", 16f, System.Drawing.FontStyle.Bold,&lt;br /&gt;System.Drawing.GraphicsUnit.World);&lt;br /&gt;&lt;br /&gt;s.Text = "Simple Control";&lt;br /&gt;&lt;br /&gt;s.dmc += new System.EventHandler(sf);&lt;br /&gt;&lt;br /&gt;Controls.Add(g1);&lt;br /&gt;&lt;br /&gt;Controls.Add(s);&lt;br /&gt;&lt;br /&gt;g1.Controls.Add(r2);&lt;br /&gt;&lt;br /&gt;g1.Controls.Add(r1);&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;void r2f(object sender, System.EventArgs e)&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;if (r2.Checked)&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;s.dm = ddd.a2;&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;void r1f(object sender, System.EventArgs e)&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;if (r1.Checked)&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;s.dm = ddd.a1;&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;void sf(object sender, System.EventArgs e)&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;if (s.dm == ddd.a1)&lt;br /&gt;&lt;br /&gt;MessageBox.Show("hi");&lt;br /&gt;&lt;br /&gt;if (s.dm == ddd.a2)&lt;br /&gt;&lt;br /&gt;MessageBox.Show("bye");&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;public static void Main()&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;Application.Run(new zzz());&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;c.cs&lt;br /&gt;&lt;br /&gt;using System;&lt;br /&gt;&lt;br /&gt;using System.ComponentModel;&lt;br /&gt;&lt;br /&gt;using System.Windows.Forms;&lt;br /&gt;&lt;br /&gt;using System.Drawing;&lt;br /&gt;&lt;br /&gt;[DefaultProperty("dm"),DefaultEvent("dmc"),]&lt;br /&gt;&lt;br /&gt;public class sss : Control&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;ddd d;&lt;br /&gt;&lt;br /&gt;EventHandler eee;&lt;br /&gt;&lt;br /&gt;public sss() :base()&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;d = ddd.a1;&lt;br /&gt;&lt;br /&gt;ccc();&lt;br /&gt;&lt;br /&gt;SetStyle(ControlStyles.ResizeRedraw, true);&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;[Category("Appearance"),Description("Controls how the control&lt;br /&gt;paints"),DefaultValue(ddd.a1),Bindable(true),]&lt;br /&gt;&lt;br /&gt;public ddd dm&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;get&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;return d;&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;set&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;d=value;&lt;br /&gt;&lt;br /&gt;ccc();&lt;br /&gt;&lt;br /&gt;dmf(EventArgs.Empty);&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;protected override void OnPaint(PaintEventArgs e)&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;e.Graphics.FillRectangle(new SolidBrush(BackColor), ClientRectangle);&lt;br /&gt;&lt;br /&gt;Size textSize = e.Graphics.MeasureString(Text, Font).ToSize();&lt;br /&gt;&lt;br /&gt;float x = (ClientRectangle.Width/2) - (textSize.Width/2);&lt;br /&gt;&lt;br /&gt;float y = (ClientRectangle.Height/2) - (textSize.Height/2);&lt;br /&gt;&lt;br /&gt;e.Graphics.DrawString(Text,Font,new SolidBrush(ForeColor),x, y);&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;protected override void OnTextChanged(EventArgs e) {&lt;br /&gt;&lt;br /&gt;base.OnTextChanged(e);&lt;br /&gt;&lt;br /&gt;Invalidate();&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;[Description("Raised when the DrawingMode changes")]&lt;br /&gt;&lt;br /&gt;public event EventHandler dmc&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;add {&lt;br /&gt;&lt;br /&gt;eee += value;&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;remove {&lt;br /&gt;&lt;br /&gt;eee -= value;&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;protected virtual void dmf(EventArgs e)&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;Invalidate();&lt;br /&gt;&lt;br /&gt;if (eee != null)&lt;br /&gt;&lt;br /&gt;eee.Invoke(this, e);&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;void ccc()&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;if ( d == ddd.a1)&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;base.BackColor = Color.Yellow ;&lt;br /&gt;&lt;br /&gt;base.ForeColor = Color.Green ;&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;if ( d == ddd.a2)&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;base.BackColor = Color.LightSlateGray ;&lt;br /&gt;&lt;br /&gt;base.ForeColor = Color.White ;&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;public enum ddd&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;a1 = 0,&lt;br /&gt;&lt;br /&gt;a2 = 1,&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;a.bat&lt;br /&gt;&lt;br /&gt;del *.exe&lt;br /&gt;&lt;br /&gt;del *.dll&lt;br /&gt;&lt;br /&gt;csc /t:library c.cs&lt;br /&gt;&lt;br /&gt;csc h.cs /r:c.dll&lt;br /&gt;&lt;br /&gt;h&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Let us now write a control that is considerably intricate. In the container&lt;br /&gt;h.cs, we start with two radio buttons r1 and r2. Each time we select a radio&lt;br /&gt;button, depending upon the option selected, either of the functions r1f or r2f&lt;br /&gt;will get called. Thereafter, we create a group box called g1. The radio buttons&lt;br /&gt;are added to the group box, and the group box is added to the Controls&lt;br /&gt;collections. Apart from these controls, one more user control named s, which is&lt;br /&gt;an instance of class sss, is added to the Controls collection.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Screen 4.36 Screen 4.37&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Prior to this, we initialize various properties of this control such as&lt;br /&gt;TabIndex, Font, Size, Anchor, Text etc. to some meaningful values. Besides&lt;br /&gt;these, a property called dmc in the user control is initialized to an&lt;br /&gt;EventHandler that calls function sf. Thus, whenever the event represented by dmc&lt;br /&gt;is triggered, the function sf gets called.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;The code implementing our user control s, resides in the file c.cs. The user&lt;br /&gt;control s is an instance of sss and is derived from the Control class. At the&lt;br /&gt;outset, the constructor of class sss calls the constructor of the base class&lt;br /&gt;using the keyword base, even though this is optional, because the base class&lt;br /&gt;constructor invariably gets called.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;The class ddd is an enum, with two members a1 and a2, having values 0 and 1&lt;br /&gt;respectively. We could conveniently have used numbers directly instead of an&lt;br /&gt;enum, but since the original example used an enum, we have also done so. We set&lt;br /&gt;the object d to the value 0 and call function ccc from class sss. The main&lt;br /&gt;objective of placing code in a function is to enable the code to be called&lt;br /&gt;several times.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;In the function ccc, we start by checking the value of the object d. If it is a1&lt;br /&gt;i.e. 0, we change the value of the two properties BackColor and ForeColor to&lt;br /&gt;Yellow and Green respectively. If the object has a value of a2, then another&lt;br /&gt;pair of colors is assigned to these properties. The properties are changed in&lt;br /&gt;the base class using the keyword base. The function SetStyle ensures that the&lt;br /&gt;form gets redrawn when it is resized.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;We have already learnt that the OnPaint function is called whenever the window&lt;br /&gt;needs to be redrawn. In this function, we first use the property BackColor to&lt;br /&gt;fill the form background. Next, we use the width of the currently selected font,&lt;br /&gt;to calculate the midpoint of our screen, and then, we write the value contained&lt;br /&gt;in the text property in the center of the window.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;When the second radio button is selected, function r2f gets called. In this&lt;br /&gt;function, the program checks whether the radio button is already checked. If so,&lt;br /&gt;it initializes the property dm, whose data type is ddd, to a2.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Similarly, when the first radio button is selected, function r1f gets called.&lt;br /&gt;This function first ascertains if the radio button is already checked. If so, it&lt;br /&gt;initializes the property dm to a1.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Now, we shall focus our attention on the property dm. In the set accessor of&lt;br /&gt;property dm, the ddd object named d is initialized to either a1 or a2. Following&lt;br /&gt;this action, a call is made to function ccc, which changes the background and&lt;br /&gt;foreground color, depending on the value contained in d. The effect is observed&lt;br /&gt;when the function OnPaint gets called. A call is made to the function dmf with a&lt;br /&gt;parameter of an Empty event.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;In function dmf, we first call Invalidate, which in turn, calls the OnPaint&lt;br /&gt;function. Just as life offers no guarantees whatsoever, in much the same way,&lt;br /&gt;the calls made to the OnPaint function are unpredictable. The Invalidate&lt;br /&gt;function instantly calls the OnPaint function.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;You may recall that in h.cs, the property dmc was initialized with the name of&lt;br /&gt;the function sf. This property dmc is an event type that stores the EventHandler&lt;br /&gt;or function sf in an instance object eee. So, the value in the object eee is&lt;br /&gt;checked. If the value is not null, the function Invoke is called off the object&lt;br /&gt;eee with two parameters. The first parameter is a reference to itself, i.e.&lt;br /&gt;'this', and the second parameter is a null EventArgs object. The function&lt;br /&gt;Invoke, in turn, calls function sf in the container, h.cs. The function sf&lt;br /&gt;displays a message box, depending upon the value of the ddd object.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;The main idea behind this exercise is to demonstrate that clicking on a radio&lt;br /&gt;button in the container initializes a property in the user control. This in&lt;br /&gt;turn, raises a property changed event, thus resulting in a call to a function&lt;br /&gt;registered with a property of the control. The function resides in the container&lt;br /&gt;and not in the user control.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;This is a circuitous route for accomplishing results. The Invoke function is not&lt;br /&gt;aware of and could not care less about the functions that it is calling.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;All the other attributes in the code can be safely ignored, since they are&lt;br /&gt;mainly meant for external tools or programs that display the metadata.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;h.cs&lt;br /&gt;&lt;br /&gt;using System.Drawing;&lt;br /&gt;&lt;br /&gt;using System.Windows.Forms;&lt;br /&gt;&lt;br /&gt;public class zzz : Form&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;TextBox t;&lt;br /&gt;&lt;br /&gt;Button b;&lt;br /&gt;&lt;br /&gt;hhh h;&lt;br /&gt;&lt;br /&gt;public zzz()&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;h = new hhh();&lt;br /&gt;&lt;br /&gt;b = new Button();&lt;br /&gt;&lt;br /&gt;t = new TextBox();&lt;br /&gt;&lt;br /&gt;b.Size = new System.Drawing.Size(104, 40);&lt;br /&gt;&lt;br /&gt;b.Text = "Vijay";&lt;br /&gt;&lt;br /&gt;b.Location = new System.Drawing.Point(336, 56);&lt;br /&gt;&lt;br /&gt;ClientSize = new System.Drawing.Size(448, 157);&lt;br /&gt;&lt;br /&gt;t.Location = new System.Drawing.Point(80, 16);&lt;br /&gt;&lt;br /&gt;t.Text = "Vijay Mukhi";&lt;br /&gt;&lt;br /&gt;h.Dock = System.Windows.Forms.DockStyle.Bottom;&lt;br /&gt;&lt;br /&gt;h.Size = new System.Drawing.Size(448, 40);&lt;br /&gt;&lt;br /&gt;h.Location = new System.Drawing.Point(0, 117);&lt;br /&gt;&lt;br /&gt;h.Text = "none";&lt;br /&gt;&lt;br /&gt;h.ppp(t, "TextBox selected");&lt;br /&gt;&lt;br /&gt;h.ppp(b, "Button Selected");&lt;br /&gt;&lt;br /&gt;Controls.Add(t);&lt;br /&gt;&lt;br /&gt;Controls.Add(b);&lt;br /&gt;&lt;br /&gt;Controls.Add(h);&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;public static void Main(string[] args)&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;Application.Run(new zzz());&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;c.cs&lt;br /&gt;&lt;br /&gt;using System;&lt;br /&gt;&lt;br /&gt;using System.Collections;&lt;br /&gt;&lt;br /&gt;using System.ComponentModel;&lt;br /&gt;&lt;br /&gt;using System.Drawing;&lt;br /&gt;&lt;br /&gt;using System.Windows.Forms;&lt;br /&gt;&lt;br /&gt;public class hhh : Control&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;Hashtable h;&lt;br /&gt;&lt;br /&gt;Control a;&lt;br /&gt;&lt;br /&gt;public hhh()&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;h = new Hashtable();&lt;br /&gt;&lt;br /&gt;BackColor = SystemColors.Info;&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;[Browsable(false),&lt;br /&gt;DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden) ]&lt;br /&gt;&lt;br /&gt;public override string Text&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;get&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;return base.Text;&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;set&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;base.Text = value;&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;private void ce(object s, EventArgs e)&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;a = (Control)s;&lt;br /&gt;&lt;br /&gt;Invalidate();&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;private void cl(object s, EventArgs e)&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;if (s == a)&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;a = null;&lt;br /&gt;&lt;br /&gt;Invalidate();&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;public void ppp(Control c, string v)&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;if (v == null)&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;v = string.Empty;&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;if (v.Length == 0)&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;h.Remove(c);&lt;br /&gt;&lt;br /&gt;c.Enter -= new EventHandler(ce);&lt;br /&gt;&lt;br /&gt;c.Leave -= new EventHandler(cl);&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;else&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;h[c] = v;&lt;br /&gt;&lt;br /&gt;c.Enter += new EventHandler(ce);&lt;br /&gt;&lt;br /&gt;c.Leave += new EventHandler(cl);&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;if (c == a)&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;Invalidate();&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;protected override void OnPaint(PaintEventArgs pe)&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;base.OnPaint(pe);&lt;br /&gt;&lt;br /&gt;Rectangle rect = ClientRectangle;&lt;br /&gt;&lt;br /&gt;Pen borderPen = new Pen(ForeColor);&lt;br /&gt;&lt;br /&gt;pe.Graphics.DrawRectangle(borderPen, rect);&lt;br /&gt;&lt;br /&gt;borderPen.Dispose();&lt;br /&gt;&lt;br /&gt;if (a != null)&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;string te = (string)h[a];&lt;br /&gt;&lt;br /&gt;if (te != null &amp;amp;&amp;amp; te.Length &amp;gt; 0)&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;rect.Inflate(-2, -2);&lt;br /&gt;&lt;br /&gt;Brush brush = new SolidBrush(ForeColor);&lt;br /&gt;&lt;br /&gt;pe.Graphics.DrawString(te, Font, brush, rect);&lt;br /&gt;&lt;br /&gt;brush.Dispose();&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;In this program, we have three controls: a user control h, contained in class&lt;br /&gt;hhh, a TextBox t and a Button b. The basic properties like Size, Text and&lt;br /&gt;Location for the Button and the Text box are set to the specified initial&lt;br /&gt;values. Thereafter, the properties of the user control h are initialized. The&lt;br /&gt;DockStyle for the Dock property is set to the Bottom of the form, and the Size&lt;br /&gt;and the Location are specified. The Text property is also initialized.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;The user control h has a property called ppp, which accepts two parameters, a&lt;br /&gt;control and a string. We call it twice. When it is called for the first time,&lt;br /&gt;the first parameter passed is a text box control. The next time it is called,&lt;br /&gt;the first parameter passed is a button control. The button and the text box are&lt;br /&gt;displayed on the screen and a yellow colored label with the word 'TextBox&lt;br /&gt;selected' in the bottom pane.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Screen 4.38&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;When we click on the button, the words in the label change to 'Button selected'.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Screen 4.39&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Thus, depending upon the control selected, our user control displays a help&lt;br /&gt;message which we have registered using the property ppp.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;The user control h in file c.cs, has two instance variables, a HashTable h and a&lt;br /&gt;Control a. The HashTable class stores values based on a certain key. This helps&lt;br /&gt;in retrieving the value efficiently on being provided with the key. In the&lt;br /&gt;constructor of class hhh, an instance h of the HashTable is created. The&lt;br /&gt;property BackColor is initialized to a Read Only Color property from the class&lt;br /&gt;SystemColors. This property represents the tool tip background color. On our&lt;br /&gt;machine, it happens to be Yellow, and thus, we see a yellow color label.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;There is a property called Text in the Control class. In order to implement our&lt;br /&gt;property, we must override the existing one. Presently, in our property, we are&lt;br /&gt;merely accessing the original Text in the base class.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;The attributes make interesting reading even though they have little use in the&lt;br /&gt;current example. The Browsable attribute with a parameter of False prevents this&lt;br /&gt;property from showing up in the Property Browser. Also, the value assigned to&lt;br /&gt;the property is not saved on disk. We are not doing anything useful in the Text&lt;br /&gt;property at all.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;The property ppp is called twice in the control, because the container has two&lt;br /&gt;property initialization statements. Good programming style incorporates&lt;br /&gt;comprehensive error checks.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;We first check for a string value in the second parameter v. If it is null, we&lt;br /&gt;initialize the variable v to an empty string. If the string is not empty, we add&lt;br /&gt;the string contained in v to the hash table using the control parameter c as the&lt;br /&gt;key. Thus, in a hash table, any data type can be used as a key to insert or&lt;br /&gt;retrieve values.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;The control class has Events called Enter and Leave. We use the += syntax to&lt;br /&gt;register the function ce whenever the Event Enter gets fired. In the same&lt;br /&gt;manner, the function cl gets called whenever the Event Leave gets fired.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;If the user calls the property without a string, it signifies that the control&lt;br /&gt;has to be removed from the hash table and the list of functions in the events&lt;br /&gt;has to be called. Thus, we use the Remove function of the HashTable class to&lt;br /&gt;remove the key c from the hash table. The -= syntax of the Events and Delegates&lt;br /&gt;is employed to remove the functions registered with the Enter and Leave events.&lt;br /&gt;&lt;br /&gt;In the OnPaint function, we have drawn a label and displayed some text in it.&lt;br /&gt;There is one rule that must never be violated: 'Call the function in the base&lt;br /&gt;class first.' This is because we are unaware of what the overridden function&lt;br /&gt;accomplishes.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;A Pen is created from the color stored in ForeColor. This color is the default&lt;br /&gt;color stored in the Control class. The Background color is initialized in the&lt;br /&gt;constructor of hhh class. A rectangle that is drawn using this pen displays&lt;br /&gt;yellow as the background color, with the size defaulting to the value stored in&lt;br /&gt;the ClientRectangle property in the control class. The pen is then disposed off,&lt;br /&gt;to enable the system to retrieve the resources consumed by it. A check is&lt;br /&gt;thereafter performed on the value in the Control object. It should be null since&lt;br /&gt;the control has not been initialized in the beginning.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Before the OnPaint function gets called, a lot of activities get executed in the&lt;br /&gt;background, i.e. many functions get called and numerous events get triggered. On&lt;br /&gt;entering a field, the event OnControlEnter gets triggered and the function that&lt;br /&gt;is registered with the Enter event is called. In our case, function ce is&lt;br /&gt;called. The first parameter to this function is a handle to the control that&lt;br /&gt;caused the event. In our case, it is the Text Box. We initialize the object a to&lt;br /&gt;the Text Box control. Thus, in OnPaint, the value of control a is a Text Box.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;We then retrieve the string stored in the hash table using the control as the&lt;br /&gt;key. We then ascertain that the value contained in the string te is not null and&lt;br /&gt;its length is greater than zero.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;The Rect structure contains the size of the rectangle. We can then inflate or&lt;br /&gt;deflate the rectangle using the Inflate method. As a result, the position and&lt;br /&gt;the size of the Rectangle change. The X and Y properties are changed by the&lt;br /&gt;amount specified and the Width and Height are modified to twice the amount&lt;br /&gt;specified. The final outcome is that the size of the rectangle is inflated&lt;br /&gt;without moving its geometric center. In our case, since the numbers are&lt;br /&gt;negative, the rectangular will be deflated. Following this, a solid brush is&lt;br /&gt;created using the property ForeColor. A string is drawn using the DrawString&lt;br /&gt;function in the Pen class. Thereafter, we dispose of the brush.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;When we leave the control, the event handler named Leave is called. The function&lt;br /&gt;associated with this eventhandler is cl, which ascertains whether the control in&lt;br /&gt;'a' and the one passed as parameter i.e. 's' is the same. If so, then 'a' is&lt;br /&gt;initialized to a Null value and the OnPaint function is called. The value&lt;br /&gt;contained in 'a' is the deciding factor on whether we are inside a control or&lt;br /&gt;not.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;h.cs&lt;br /&gt;&lt;br /&gt;using System.Drawing;&lt;br /&gt;&lt;br /&gt;using System.Windows.Forms;&lt;br /&gt;&lt;br /&gt;public class zzz : Form&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;fff f;&lt;br /&gt;&lt;br /&gt;public zzz()&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;f = new fff();&lt;br /&gt;&lt;br /&gt;f.Dock = System.Windows.Forms.DockStyle.Fill;&lt;br /&gt;&lt;br /&gt;f.ForeColor = System.Drawing.Color.White;&lt;br /&gt;&lt;br /&gt;f.BackColor = System.Drawing.Color.Black;&lt;br /&gt;&lt;br /&gt;f.Size = new System.Drawing.Size(600, 450);&lt;br /&gt;&lt;br /&gt;f.vvv = 73;&lt;br /&gt;&lt;br /&gt;f.Text = "Vijay Mukhi";&lt;br /&gt;&lt;br /&gt;ClientSize = new System.Drawing.Size(600, 450);&lt;br /&gt;&lt;br /&gt;Controls.Add(f);&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;public static void Main()&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;Application.Run(new zzz());&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;c.cs&lt;br /&gt;&lt;br /&gt;using System;&lt;br /&gt;&lt;br /&gt;using System.Drawing;&lt;br /&gt;&lt;br /&gt;using System.Drawing.Drawing2D;&lt;br /&gt;&lt;br /&gt;using System.Windows.Forms;&lt;br /&gt;&lt;br /&gt;public class fff : Control {&lt;br /&gt;&lt;br /&gt;int vvv1 = 0;&lt;br /&gt;&lt;br /&gt;public int Min = 0;&lt;br /&gt;&lt;br /&gt;public int Max = 100;&lt;br /&gt;&lt;br /&gt;public bool ShowValue = false;&lt;br /&gt;&lt;br /&gt;int dv = 0;&lt;br /&gt;&lt;br /&gt;bool d = false;&lt;br /&gt;&lt;br /&gt;public Color cs = Color.Red;&lt;br /&gt;&lt;br /&gt;public Color ce = Color.LimeGreen;&lt;br /&gt;&lt;br /&gt;Brush bb = null;&lt;br /&gt;&lt;br /&gt;Brush bd = null;&lt;br /&gt;&lt;br /&gt;public int vvv&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;get&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;if (d)&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;return dv;&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;return vvv1;&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;set&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;vvv1 = value;&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;protected override void OnPaint(PaintEventArgs e)&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;base.OnPaint(e);&lt;br /&gt;&lt;br /&gt;bb=new LinearGradientBrush(new Point(0, 0),&lt;br /&gt;&lt;br /&gt;new Point(ClientSize.Width, 0),cs,ce);&lt;br /&gt;&lt;br /&gt;bd = new SolidBrush(Color.FromArgb(200, Color.Black));&lt;br /&gt;&lt;br /&gt;e.Graphics.FillRectangle(bb, ClientRectangle);&lt;br /&gt;&lt;br /&gt;Rectangle r = ClientRectangle;&lt;br /&gt;&lt;br /&gt;float p = ((float)vvv / ((float)Max - (float)Min));&lt;br /&gt;&lt;br /&gt;int a = (int)(p * (float)r.Width);&lt;br /&gt;&lt;br /&gt;r.X += a;&lt;br /&gt;&lt;br /&gt;r.Width -= a;&lt;br /&gt;&lt;br /&gt;e.Graphics.FillRectangle(bd, r);&lt;br /&gt;&lt;br /&gt;e.Graphics.Flush();&lt;br /&gt;&lt;br /&gt;RectangleF r1 = new RectangleF();&lt;br /&gt;&lt;br /&gt;SizeF ts = e.Graphics.MeasureString(Text, Font);&lt;br /&gt;&lt;br /&gt;r1.Width = ts.Width;&lt;br /&gt;&lt;br /&gt;r1.Height = ts.Height;&lt;br /&gt;&lt;br /&gt;r1.X = (ClientRectangle.Width - r1.Width) / 2;&lt;br /&gt;&lt;br /&gt;r1.Y = (ClientRectangle.Height - r1.Height) / 2;&lt;br /&gt;&lt;br /&gt;e.Graphics.DrawString(Text, Font, new SolidBrush(ForeColor), r1);&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;protected override void OnMouseDown(MouseEventArgs e)&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;base.OnMouseDown(e);&lt;br /&gt;&lt;br /&gt;Capture = true;&lt;br /&gt;&lt;br /&gt;d = true;&lt;br /&gt;&lt;br /&gt;sss(new Point(e.X, e.Y));&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;protected override void OnMouseMove(MouseEventArgs e)&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;base.OnMouseMove(e);&lt;br /&gt;&lt;br /&gt;if ( !d)&lt;br /&gt;&lt;br /&gt;return;&lt;br /&gt;&lt;br /&gt;sss(new Point(e.X, e.Y));&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;protected override void OnMouseUp(MouseEventArgs e)&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;base.OnMouseUp(e);&lt;br /&gt;&lt;br /&gt;if ( !d)&lt;br /&gt;&lt;br /&gt;return;&lt;br /&gt;&lt;br /&gt;Capture = false;&lt;br /&gt;&lt;br /&gt;d = false;&lt;br /&gt;&lt;br /&gt;vvv = dv;&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;void sss(Point l) {&lt;br /&gt;&lt;br /&gt;Rectangle r = ClientRectangle;&lt;br /&gt;&lt;br /&gt;float p = (float)l.X / (float)r.Width;&lt;br /&gt;&lt;br /&gt;int a = (int)(p * (float)(Max - Min));&lt;br /&gt;&lt;br /&gt;int o = dv;&lt;br /&gt;&lt;br /&gt;dv = a;&lt;br /&gt;&lt;br /&gt;float op = ((float)o / ((float)Max - (float)Min));&lt;br /&gt;&lt;br /&gt;int ol = (int)(op * (float)r.Width);&lt;br /&gt;&lt;br /&gt;float np = ((float)dv / ((float)Max - (float)Min));&lt;br /&gt;&lt;br /&gt;int nl = (int)(np * (float)r.Width);&lt;br /&gt;&lt;br /&gt;int mi = Math.Min(ol, nl);&lt;br /&gt;&lt;br /&gt;int ma = Math.Max(ol, nl);&lt;br /&gt;&lt;br /&gt;Rectangle r1 = new Rectangle(r.X + mi, r.Y, ma - mi, r.Height);&lt;br /&gt;&lt;br /&gt;Invalidate(r1);&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;When we run the above program, we see a myriad of colors such as red, green and&lt;br /&gt;black. We also see 'Vijay Mukhi' displayed in the center. On clicking the mouse,&lt;br /&gt;more of the color black will be introduced upto the point of the mouse click on&lt;br /&gt;the screen, whereas dragging the mouse will extend or diminish the colors&lt;br /&gt;accordingly. This certainly is a sight for sore eyes!&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Screen 4.40 Screen 4.41&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;We have to admit that the folks at Microsoft who worked on this sample did a&lt;br /&gt;truly marvelous job. Our task here is to explain the program that they have&lt;br /&gt;written, in absolute layman terms.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;The h.cs file contains code that is already explained in the earlier program. As&lt;br /&gt;always, along with the properties that are present in Control, we have also&lt;br /&gt;initialized one of our own properties, vvv for control fff. The user-defined&lt;br /&gt;property vvv is assigned a value of 73. The value contained in the Text property&lt;br /&gt;is displayed at the center of the screen.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;We now focus the spotlight on the actual code of our control fff that resides in&lt;br /&gt;the file c.cs.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;The container sets the value of the property vvv to 73. Therefore, in the&lt;br /&gt;control, the property vvv gets called initially. The set accessor initializes&lt;br /&gt;the int variable vvv1 to 73.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;The function OnPaint is responsible for drawing the rainbow of colors on the&lt;br /&gt;screen. In this function, we first call the original OnPaint function. Then, we&lt;br /&gt;create two brushes to obtain the desired effect. The first brush is a&lt;br /&gt;LinearGradientBrush, which can be accessed using the Brush object bb. The last&lt;br /&gt;parameter to the constructor of this brush is the starting color cs, which has&lt;br /&gt;been initialized to Red, and an ending color ce that is initialized to a value&lt;br /&gt;of LimeGreen. The second brush is a SolidBrush bd, which is created to fill up&lt;br /&gt;any shape. The function FromArgb of the Color class creates a new color, where&lt;br /&gt;the first parameter is an alpha value 200 and the second parameter is the base&lt;br /&gt;color.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;The Graphics property of PaintEventArgs merely returns a Graphics object when we&lt;br /&gt;call function FillRectangle. The two parameters to this function are : a Brush&lt;br /&gt;that represents a gradient and the Size of the window whose value is stored in&lt;br /&gt;the ClientRectangle property. If we stop at this stage, the red and the green&lt;br /&gt;gradients will stand out glaringly.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Now, let us draw the black rectangle separating the red and green colors. We&lt;br /&gt;create a temporary variable r to store the dimensions of the window.&lt;br /&gt;&lt;br /&gt;Two instance variables, Max and Min having a value of 100 and 0 respectively,&lt;br /&gt;are used to decide the factor by which the property vvv should be divided. Since&lt;br /&gt;we need to retrieve the value for the property, the get accessor is called. The&lt;br /&gt;value contained in d decides on the value to be assigned to the property.&lt;br /&gt;Initially d is false, so the value 73 contained in vvv1 is returned.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;The float variable p now has a value of .73. The Width of the window in our case&lt;br /&gt;is 600 pixels. Thus, the variable 'a' is assigned a value of 438. You can use&lt;br /&gt;the WriteLine function to display these values. The Width of the rectangle is&lt;br /&gt;reduced by this amount and the X position is shifted by 438 pixels. Next, we&lt;br /&gt;draw the black rectangle using the function FillRectangle by providing it with a&lt;br /&gt;black brush and the new Rectangle object r. Modifying the value of the property&lt;br /&gt;vvv from 73 to 3 will result in displaying the entire screen in black color.&lt;br /&gt;Thus, the vvv parameter decides the size of the black rectangle; the smaller its&lt;br /&gt;value, the larger will be the size of the rectangle. The Flush function ensures&lt;br /&gt;that all pending graphic operations on the stack are executed immediately. If we&lt;br /&gt;stop here, the gradient and the rectangle will be displayed, but no text will be&lt;br /&gt;displayed in the center of the screen.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;The Rectangular structure r1 stores the size and location of the rectangular&lt;br /&gt;region that our string requires. The MeasureString function takes two&lt;br /&gt;parameters:&lt;br /&gt;&lt;br /&gt;• The first is a string value that is stored in our property Text.&lt;br /&gt;&lt;br /&gt;• The second is a Font object.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;The default Font property is supplied for the second parameter. The function&lt;br /&gt;returns a SizeF object, whose Width and Height determine the display region that&lt;br /&gt;the string requires. The width and height of rectangle r1 is initialized to the&lt;br /&gt;Width and Height of the string. As the string is to be displayed in the center&lt;br /&gt;of the form, objects X and Y, which are the members of rectangle r1, are given&lt;br /&gt;the following values:&lt;br /&gt;&lt;br /&gt;• The Width of the form = the width of the string divided by 2.&lt;br /&gt;&lt;br /&gt;• The Height of the form = the height of the string divided by 2.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;We then use the DrawString function to display the text. This function takes the&lt;br /&gt;following parameters in the specified order:&lt;br /&gt;&lt;br /&gt;• A string that is stored in the Text property.&lt;br /&gt;&lt;br /&gt;• The default font.&lt;br /&gt;&lt;br /&gt;• The color of the brush that is stored in the property ForeColor.&lt;br /&gt;&lt;br /&gt;• The region, which is stored in object r1.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;We have three mouse events that trigger off three functions. They are as&lt;br /&gt;follows:&lt;br /&gt;&lt;br /&gt;• OnMouseMove: When we move the mouse.&lt;br /&gt;&lt;br /&gt;• OnMouseDown: When we click the left mouse button, with the mouse cursor placed&lt;br /&gt;on the form.&lt;br /&gt;&lt;br /&gt;• OnMouseUp: When we release the left mouse button.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;If we move the mouse around, nothing happens. This is because the 'if' statement&lt;br /&gt;results in a value of true, as the variable d has a value of false. So, the&lt;br /&gt;function practically achieves nothing. Clicking the left mouse button toggles&lt;br /&gt;the value of the variable d to true. Thus, if the variable d is True, we know&lt;br /&gt;that the user has clicked the left mouse button, and when it reverts back to&lt;br /&gt;false, we know that the left mouse button has been released. The OnMouseMove&lt;br /&gt;function does something constructive and gainful only when the left mouse button&lt;br /&gt;is depressed.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;The Capture property reveals whether the control has been able to capture the&lt;br /&gt;mouse or not. This property is optional. When we click within the window, the&lt;br /&gt;framework is informed about our victory over the mouse, and the variable d is&lt;br /&gt;set to true. The next most pertinent action is to call function sss with a Point&lt;br /&gt;object. This object stores the position at which the mouse has been clicked. The&lt;br /&gt;MouseEventArgs parameter e, has two members X and Y, which identify the current&lt;br /&gt;position of the mouse.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;The function sss is called yet again when the mouse is moved. The OnMouseUp&lt;br /&gt;function changes the value of Capture, and sets the value of the boolean&lt;br /&gt;variable d to false. It also initializes the value of a property vvv to dv.&lt;br /&gt;&lt;br /&gt;The net achievement of the function sss is that, it calls the function OnPaint&lt;br /&gt;using the Invalidate function. The OnPaint function does not draw or paint the&lt;br /&gt;entire form or window, but only the specific part that has been invalidated.&lt;br /&gt;This is done to achieve optimum efficiency, since it is preposterous to waste&lt;br /&gt;effort in redrawing areas that have not been invalidated. The Invalidate&lt;br /&gt;function is also passed a Rect object as a parameter, which takes a decision on&lt;br /&gt;the area of the client rectangle that is to be re-drawn.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Function sss is therefore, given a smaller Rect object as a parameter, which&lt;br /&gt;informs the OnPaint function about the specific part of the form that should be&lt;br /&gt;invalidated. The Point parameter is employed to determine the area of the form,&lt;br /&gt;that must be redrawn.&lt;br /&gt;&lt;br /&gt;The co-ordinates of the entire screen are stored in ClientRectangle. We also&lt;br /&gt;calculate a percentage p, depending upon our current position, i.e. l.X, divided&lt;br /&gt;by the Width of the screen. Next, we multiply this percentage by 100 (the&lt;br /&gt;difference between Max and Min). The value of dv is stored in variable o, since&lt;br /&gt;we are initializing dv to 'a' in the next line.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Two new percentage values, op and np have been calculated. They are merely the&lt;br /&gt;values of o and dv divided by 100. Thereafter, we calculate the minimum and&lt;br /&gt;maximum values of ol and nl. The new rectangle created is the Client Rectangle,&lt;br /&gt;where the minimum value is added to X, and Y is left untouched. The Width is set&lt;br /&gt;to the difference of ma and mi, and the Height is left unaltered.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;The difference between variables ma and mi is negligible. Correspondingly, the&lt;br /&gt;width of the invalidated region too is insignificantly small. Further, mi is&lt;br /&gt;used to decide the position at which the X of the black rectangle should&lt;br /&gt;commence. Thus, if we replace r1 with ClientRectangle, everything would be hunky&lt;br /&gt;dory, but the screen will flicker a great deal, while the mouse is in motion.&lt;br /&gt;This is because the same screen has to be displayed when the movement ceases.&lt;br /&gt;Thus, to thwart this flicker, we redraw only the specific part of the screen&lt;br /&gt;that has been invalidated.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;ScrollBar Control&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;a.cs&lt;br /&gt;&lt;br /&gt;using System.Drawing;&lt;br /&gt;&lt;br /&gt;using System.Windows.Forms;&lt;br /&gt;&lt;br /&gt;public class zzz : Form&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;VScrollBar v;&lt;br /&gt;&lt;br /&gt;HScrollBar h;&lt;br /&gt;&lt;br /&gt;PictureBox p;&lt;br /&gt;&lt;br /&gt;Label l2;&lt;br /&gt;&lt;br /&gt;Label l1;&lt;br /&gt;&lt;br /&gt;bool d = false;&lt;br /&gt;&lt;br /&gt;int ox, oy;&lt;br /&gt;&lt;br /&gt;float vm;&lt;br /&gt;&lt;br /&gt;float vp;&lt;br /&gt;&lt;br /&gt;float hm;&lt;br /&gt;&lt;br /&gt;float hp;&lt;br /&gt;&lt;br /&gt;public zzz()&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;ClientSize = new System.Drawing.Size(520, 277);&lt;br /&gt;&lt;br /&gt;l1 = new Label();&lt;br /&gt;&lt;br /&gt;l1.Location = new System.Drawing.Point(408, 160);&lt;br /&gt;&lt;br /&gt;l2 = new Label();&lt;br /&gt;&lt;br /&gt;l2.Location = new Point(408, 184);&lt;br /&gt;&lt;br /&gt;v = new VScrollBar();&lt;br /&gt;&lt;br /&gt;v.Location = new Point(200, 24);&lt;br /&gt;&lt;br /&gt;v.Size = new Size(16, 152);&lt;br /&gt;&lt;br /&gt;v.Scroll += new ScrollEventHandler(vScroll);&lt;br /&gt;&lt;br /&gt;v.Minimum = -100;&lt;br /&gt;&lt;br /&gt;h = new HScrollBar();&lt;br /&gt;&lt;br /&gt;h.Location = new Point(16, 176);&lt;br /&gt;&lt;br /&gt;h.Size = new Size(184, 16);&lt;br /&gt;&lt;br /&gt;h.Scroll += new ScrollEventHandler(hScroll);&lt;br /&gt;&lt;br /&gt;h.Minimum = -100;&lt;br /&gt;&lt;br /&gt;p = new PictureBox();&lt;br /&gt;&lt;br /&gt;Bitmap b = new Bitmap("water.bmp");&lt;br /&gt;&lt;br /&gt;p.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;&lt;br /&gt;&lt;br /&gt;p.Location = new Point(64, 32);&lt;br /&gt;&lt;br /&gt;p.Size = new Size(96, 96);&lt;br /&gt;&lt;br /&gt;p.Image = (Image)b;&lt;br /&gt;&lt;br /&gt;p.MouseDown += new MouseEventHandler(pDown);&lt;br /&gt;&lt;br /&gt;p.MouseUp += new MouseEventHandler(pUp);&lt;br /&gt;&lt;br /&gt;p.MouseMove += new MouseEventHandler(pMove);&lt;br /&gt;&lt;br /&gt;Controls.Add(l2);&lt;br /&gt;&lt;br /&gt;Controls.Add(l1);&lt;br /&gt;&lt;br /&gt;Controls.Add(h);&lt;br /&gt;&lt;br /&gt;Controls.Add(v);&lt;br /&gt;&lt;br /&gt;Controls.Add(p);&lt;br /&gt;&lt;br /&gt;v.LargeChange = 20;&lt;br /&gt;&lt;br /&gt;h.LargeChange = 20;&lt;br /&gt;&lt;br /&gt;v.SmallChange = 1;&lt;br /&gt;&lt;br /&gt;h.SmallChange = 1;&lt;br /&gt;&lt;br /&gt;vmf();&lt;br /&gt;&lt;br /&gt;hmf();&lt;br /&gt;&lt;br /&gt;l1.Text = h.Value.ToString();&lt;br /&gt;&lt;br /&gt;l2.Text = v.Value.ToString();&lt;br /&gt;&lt;br /&gt;p.Cursor = Cursors.SizeAll;&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;void vmf()&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;float hsb = (float)(v.Height - p.Height);&lt;br /&gt;&lt;br /&gt;float ticks = (float)(v.Maximum - v.Minimum);&lt;br /&gt;&lt;br /&gt;vm = hsb / ticks;&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;void hmf()&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;float hsb = (float)(h.Width - p.Width) ;&lt;br /&gt;&lt;br /&gt;float ticks = (float)(h.Maximum - h.Minimum) ;&lt;br /&gt;&lt;br /&gt;hm = hsb / ticks ;&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;void vScroll(object sender, ScrollEventArgs e)&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;l2.Text = v.Value.ToString() ;&lt;br /&gt;&lt;br /&gt;vp = (float)(v.Value- v.Minimum);&lt;br /&gt;&lt;br /&gt;p.Top = v.Bottom - (int)(vm * vp) - p.Height;&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;void hScroll(object sender, ScrollEventArgs e)&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;l1.Text = h.Value.ToString() ;&lt;br /&gt;&lt;br /&gt;hp = (float)(h.Value - h.Minimum);&lt;br /&gt;&lt;br /&gt;p.Left = h.Right - (int)(hm * hp) - p.Width;&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;void pDown(object s, MouseEventArgs e)&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;d = true;&lt;br /&gt;&lt;br /&gt;ox = e.X;&lt;br /&gt;&lt;br /&gt;oy = e.Y;&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;void pMove(object s, MouseEventArgs e)&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;if (d)&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;int minY = v.Minimum;&lt;br /&gt;&lt;br /&gt;int maxY = v.Maximum;&lt;br /&gt;&lt;br /&gt;int minX = h.Minimum;&lt;br /&gt;&lt;br /&gt;int maxX = h.Maximum;&lt;br /&gt;&lt;br /&gt;int value = (int)(v.Value - (e.Y - oy)/vm);&lt;br /&gt;&lt;br /&gt;if (value &amp;lt; minY)&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;v.Value = minY;&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;else if (value &amp;gt; maxY - v.LargeChange + 1)&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;v.Value = maxY - v.LargeChange + 1;&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;else&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;v.Value = value;&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;value = (int)(h.Value - (e.X - ox)/hm);&lt;br /&gt;&lt;br /&gt;if (value &amp;lt; minX)&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;h.Value = minX;&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;else if (value &amp;gt; maxX - h.LargeChange + 1)&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;h.Value = maxX - h.LargeChange + 1;&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;else&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;h.Value = value;&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;l1.Text = h.Value.ToString() ;&lt;br /&gt;&lt;br /&gt;l2.Text = v.Value.ToString() ;&lt;br /&gt;&lt;br /&gt;value = p.Top + (e.Y - oy);&lt;br /&gt;&lt;br /&gt;if (value &amp;lt; v.Top)&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;value = v.Top;&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;else if (value &amp;gt; v.Bottom - p.Height)&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;value = v.Bottom - p.Height;&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;p.Top = value;&lt;br /&gt;&lt;br /&gt;value = p.Left + (e.X - ox);&lt;br /&gt;&lt;br /&gt;if (value &amp;lt; h.Left)&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;value = h.Left;&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;else if (value &amp;gt; h.Right - p.Width)&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;value = h.Right - p.Width;&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;p.Left = value;&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;void pUp(object s, MouseEventArgs e)&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;d = false;&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;public static void Main()&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;Application.Run(new zzz());&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&amp;gt;csc a.cs&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Before running the program, copy the file water.bmp into the current directory.&lt;br /&gt;This file is provided along with the samples in the installation program. The&lt;br /&gt;singular methodology to comprehend large programs is by first examining their&lt;br /&gt;output, because the output will motivate you to grasp the program.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;We see two scrollbars, one vertical and the other horizontal, with a picture&lt;br /&gt;within them. Clicking on the scrollbars will move the picture in the direction&lt;br /&gt;of the scrollbar that is clicked. Let us understand how this is achieved.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Screen 4.42&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;We start by examining the constructor of class zzz. The default client size is&lt;br /&gt;modified to the desired size. Then, there are two labels l1 and l2, which&lt;br /&gt;display the current values of the two scroll bars. A vertical scrollbar v is an&lt;br /&gt;instance of a class VScrollBar, and a horizontal scrollbar h is an instance of a&lt;br /&gt;class HScrollBar. As is customary, we provide a Location and a Size to the&lt;br /&gt;scrollbar. The Minimum and Maximum properties decide the range of values that&lt;br /&gt;the user can select. Most controls such as a Text box or a Combo box already&lt;br /&gt;have a scroll bar built into them. Hence, they do not require this control. The&lt;br /&gt;Scroll event gets triggered whenever the scroll button moves. The button can be&lt;br /&gt;moved either by using a mouse or by the keyboard. Moving the vertical scroll&lt;br /&gt;button will call the vScroll function, whereas moving the horizontal scroll&lt;br /&gt;button will call the hScroll function.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;After setting the properties of the Scroll Bar, we create an object p, which is&lt;br /&gt;an instance of a PictureBox class. Next, we create a Bitmap object, and in the&lt;br /&gt;constructor, we pass the file water.bmp to it. The SizeMode decides on the&lt;br /&gt;display of the image in the Picture Box. The enum value of StretchImage&lt;br /&gt;stretches the image to fit it into the PictureBox, whereas, the enum value of&lt;br /&gt;Normal places it in the upper left corner.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;The image property is assigned to the picture or the bitmap that is to be&lt;br /&gt;displayed. Three event handlers are attached to the image:&lt;br /&gt;&lt;br /&gt;• pDown : This is called each time we click in the picture.&lt;br /&gt;&lt;br /&gt;• pUp : This is called when we release the mouse button.&lt;br /&gt;&lt;br /&gt;• pMove : This is called when we move the mouse within the picture.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Finally, all the controls i.e. two labels, two scroll bars and one image, are&lt;br /&gt;appended to the Control class.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;The property of LargeChange in the scrollbar control decides on the magnitude of&lt;br /&gt;the change in the value of the scroll bar, when the scroll bar is clicked. The&lt;br /&gt;SmallChange property is associated with the arrows on the scroll bar. We have&lt;br /&gt;set the LargeChange property to 20 and the SmallChange to 1.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;To dispel the banality of the somber explanation given above, let us digress&lt;br /&gt;slightly to present a small, albeit important, elucidation on how Windows&lt;br /&gt;handles the art of scrolling.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Screen 4.43&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;The value of the scrollbar ranges between the minimum and the maximum. The&lt;br /&gt;maximum value is LargeChange+1. The rationale behind this is that, the scrollbar&lt;br /&gt;has a property called Value, which represents the position of the top of the&lt;br /&gt;thumb. The size of the scrollbar's thumb is equal to the page value or&lt;br /&gt;LargeChange. When we reach the last page, or when the thumb reaches the end of&lt;br /&gt;the scroll bar, whereupon, we cannot scroll any further, the value property will&lt;br /&gt;always be shown less than the maximum.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;We shall now beguile you with the concepts of the vertical scroll bars. The&lt;br /&gt;explanation for the horizontal scroll bars is much the same.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;The function vmf, which is called from the constructor, initializes 3 variables.&lt;br /&gt;The Height of the Vertical scroll bar is 152 pixels because the Size property of&lt;br /&gt;the scrollbar is initialized to this value. The Height of the picture is 96&lt;br /&gt;pixels because the Size property of the picture is initialized to this value.&lt;br /&gt;The difference between these two heights, which is 56 pixels, is stored in the&lt;br /&gt;variable hsb. The variable named ticks stores a value, which is within a range&lt;br /&gt;that the vertical scroll bar can handle.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;A Tick happens to be the smallest increment that a scrollbar can make. In our&lt;br /&gt;case, as the Minimum is -100 and the Maximum is 100, the range of value stored&lt;br /&gt;in ticks becomes 200. This variable represents the ticks that the scroll bar&lt;br /&gt;needs in order to move from one end to the other. In the same vein, the variable&lt;br /&gt;hsb denotes the amount of pixels the image needs to be relocated from one end of&lt;br /&gt;the scroll bar to another. This is calculated by subtracting the height of the&lt;br /&gt;image from the height of the scroll bar.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Screen 4.44&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Dividing 56 (the number of pixels to be moved) by 200 (the total number of ticks&lt;br /&gt;available), gives us a value of .28. Thus, every tick moved by the scroll bar&lt;br /&gt;moves the image by .28 pixels. This value, known as the 'pixels per tick', is&lt;br /&gt;stored in vm. A similar routine is followed for the hmf function while&lt;br /&gt;implementing a horizontal scroll bar.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;The labels initially display zero, since this is the default value stored in the&lt;br /&gt;property value. The cursor property of the picture is changed to SizeAll. As a&lt;br /&gt;result of this, whenever the cursor moves into the picture, it's shape changes&lt;br /&gt;into a four-headed monster.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;At the outset, we want to move the picture downwards. So, we click on the&lt;br /&gt;vertical scroll bar. This movement calls the vScroll function. In this function,&lt;br /&gt;we initialize the Text property of the label l2 to the Value property of the&lt;br /&gt;vertical scroll bar v. Then, variable vp is calculated, whose value is the&lt;br /&gt;current value of the scroll bar, i.e. the value of the Minimum property of the&lt;br /&gt;scroll bar.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Every downward movement of the scroll bar increments the Value property by 1.&lt;br /&gt;This is because, the value of property SmallChange has been initialized to 1.&lt;br /&gt;Thus, the value of vp will commence at 100 and will keep escalating thereafter.&lt;br /&gt;The value stored in the Bottom property of the scroll bar is 176 and the Height&lt;br /&gt;of the picture is 96 pixels. Thus, the picture will start at 52 pixels, and&lt;br /&gt;then, the distance between the picture and the bottom property will start&lt;br /&gt;reducing.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;The formula for computing the top position where the picture should be placed is&lt;br /&gt;as follows: the Bottom of the scroll bar, minus the pixels per tick, multiplied&lt;br /&gt;by the current value of the property Value, minus the Height of the picture,&lt;br /&gt;plus the value of Minimum.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Now, let us do the reverse i.e. let us move the picture around and see how the&lt;br /&gt;scroll bars behave. Also, let us observe the corresponding transformation in the&lt;br /&gt;values. To move the picture, we first have to click on it with the left mouse&lt;br /&gt;button. This calls function pDown. In this function, the value of variable d,&lt;br /&gt;which is a boolean, is set to true. Simultaneously, in the function pUp, the&lt;br /&gt;value of variable d is set to false.&lt;br /&gt;&lt;br /&gt;The parameter e of MouseEventArgs has two members X and Y, which furnish the&lt;br /&gt;co-ordinates of the mouse pointer with regard to the picture, rather than the&lt;br /&gt;window or form. We save these values in the variables ox and oy, which will be&lt;br /&gt;utilized to calculate the distance that the picture has been dragged.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;The function pMove is the focus of all attention since, this is where the real&lt;br /&gt;excitement action lies! We place all the code in a large if statement, which&lt;br /&gt;results in true when the variable d is true. This occurs only when the mouse&lt;br /&gt;button is depressed. Each time the function is called, the minimum and maximum&lt;br /&gt;values of the scroll bars are stored in 4 variables. This is futile, since the&lt;br /&gt;values always remain constant.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;We calculate a variable called Value as follows:&lt;br /&gt;&lt;br /&gt;The initial Y position of the mouse before its dragging commenced, minus the&lt;br /&gt;current Y position of the mouse. The result is then divided by the multiplier to&lt;br /&gt;convert the pixels into scroll bar ticks. This simulates the scroll bar&lt;br /&gt;scrolling in the opposite direction.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;If the new value of variable Value is less than the minimum value allowed for&lt;br /&gt;the scroll bar, the property of the scroll bar v is set to the minimum possible.&lt;br /&gt;If the value is larger than the maximum permissible value, then it is&lt;br /&gt;initialized to the largest possible value. This concept has been explained in&lt;br /&gt;the small note we earlier presented on scrolling. If none of the above hold&lt;br /&gt;true, then we merely change the property Value of the scroll bar to the variable&lt;br /&gt;Value. These are simple error checks. The labels l1 and l2 are also updated.&lt;br /&gt;&lt;br /&gt;The above explanation is also relevant for the horizontal scroll bar.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;The property Top of the picture has to be updated to take into account the new&lt;br /&gt;position. This is also stored in the variable value and computed as follows:&lt;br /&gt;&lt;br /&gt;The original Top property value, plus the current position of the mouse, minus&lt;br /&gt;the position of the mouse before the dragging commenced.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Like before, we make sure that we do not exceed the Top and Bottom limits of the&lt;br /&gt;scroll bar. The picture has to be contained within the scroll bars. If it is&lt;br /&gt;smaller than the Top property of the scroll bar, we change the value to that of&lt;br /&gt;the Top property. The same holds true for the Bottom. Finally, we initialize the&lt;br /&gt;Top property of the picture to Value and do the same for the Left property.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Up Down Control&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;a.cs&lt;br /&gt;&lt;br /&gt;using System;&lt;br /&gt;&lt;br /&gt;using System.Drawing;&lt;br /&gt;&lt;br /&gt;using System.Windows.Forms;&lt;br /&gt;&lt;br /&gt;public class zzz : Form {&lt;br /&gt;&lt;br /&gt;DomainUpDown u;&lt;br /&gt;&lt;br /&gt;NumericUpDown n;&lt;br /&gt;&lt;br /&gt;DomainUpDown a;&lt;br /&gt;&lt;br /&gt;public zzz() {&lt;br /&gt;&lt;br /&gt;ClientSize = new Size(504, 352);&lt;br /&gt;&lt;br /&gt;n = new NumericUpDown();&lt;br /&gt;&lt;br /&gt;n.Location = new Point(132, 132);&lt;br /&gt;&lt;br /&gt;n.Maximum = new System.Decimal(100d);&lt;br /&gt;&lt;br /&gt;n.Minimum = new System.Decimal(0d);&lt;br /&gt;&lt;br /&gt;n.DecimalPlaces = 2;&lt;br /&gt;&lt;br /&gt;n.Text = "0.00";&lt;br /&gt;&lt;br /&gt;Controls.Add(n);&lt;br /&gt;&lt;br /&gt;a = new DomainUpDown();&lt;br /&gt;&lt;br /&gt;a.Location = new Point(152, 32);&lt;br /&gt;&lt;br /&gt;a.Size = new Size(120, 23);&lt;br /&gt;&lt;br /&gt;a.SelectedItemChanged += new EventHandler(abc);&lt;br /&gt;&lt;br /&gt;Controls.Add(a);&lt;br /&gt;&lt;br /&gt;u = new DomainUpDown();&lt;br /&gt;&lt;br /&gt;u.Location = new Point(152, 64);&lt;br /&gt;&lt;br /&gt;u.SelectedItemChanged += new EventHandler(pqr);&lt;br /&gt;&lt;br /&gt;Controls.Add(u);&lt;br /&gt;&lt;br /&gt;a.Items.Add(new yyy("Center",(int) HorizontalAlignment.Center));&lt;br /&gt;&lt;br /&gt;a.Items.Add(new yyy("Left",(int)HorizontalAlignment.Left));&lt;br /&gt;&lt;br /&gt;a.Items.Add(new yyy("Right",(int)HorizontalAlignment.Right));&lt;br /&gt;&lt;br /&gt;u.Items.Add(new yyy("Left",(int)LeftRightAlignment.Left));&lt;br /&gt;&lt;br /&gt;u.Items.Add(new yyy("Right",(int)LeftRightAlignment.Right));&lt;br /&gt;&lt;br /&gt;u.SelectedIndex = 1;&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;void pqr(object s, EventArgs e)&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;yyy c = (yyy)(u.Items[u.SelectedIndex]) ;&lt;br /&gt;&lt;br /&gt;n.UpDownAlign = (LeftRightAlignment)(c.i);&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;void abc(object s, EventArgs e)&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;yyy c = (yyy)(a.Items[a.SelectedIndex]) ;&lt;br /&gt;&lt;br /&gt;n.TextAlign = (HorizontalAlignment)(c.i);&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;public static void Main()&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;Application.Run(new zzz());&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;class yyy&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;public string s;&lt;br /&gt;&lt;br /&gt;public int i;&lt;br /&gt;&lt;br /&gt;public yyy(string sz, int n)&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;s=sz;&lt;br /&gt;&lt;br /&gt;i=n;&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;public override string ToString()&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;return s;&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Screen 4.45&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;In the above program, we have a NumericUpDown control called n. A NumericUpDown&lt;br /&gt;control is used when we have a single numeric value that needs to be incremented&lt;br /&gt;or decremented.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Screen 4.46&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;This can be done by clicking on the Up and Down buttons of the control&lt;br /&gt;respectively. This control has a property called ReadOnly, which has a value of&lt;br /&gt;False by default.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;This allows entry of a value directly in the control if the user finds it too&lt;br /&gt;bothersome to click on the buttons to do so. From this perspective, it operates&lt;br /&gt;akin to a text box. The properties Minimum and Maximum specify the minimum and&lt;br /&gt;maximum values that the control can accept. Under no circumstances will the&lt;br /&gt;control permit us to exceed the range specified by the above two properties.&lt;br /&gt;Thus, error checking has been built into the control.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;A NumericUpDown control has a large number of properties such as DecimalPlaces,&lt;br /&gt;Hexadecimal, ThousandsSeparator etc. These properties format the value that is&lt;br /&gt;displayed using the Text property.&lt;br /&gt;&lt;br /&gt;Their roles are as follows:&lt;br /&gt;&lt;br /&gt;• The DecimalPlaces property controls the number of decimal places.&lt;br /&gt;&lt;br /&gt;• The Hexadecimal property displays numbers in hexadecimal format.&lt;br /&gt;&lt;br /&gt;• The ThousandsSeparator property decides on the character to be used to&lt;br /&gt;separate the 1000s. This is because the comma is not the universally accepted&lt;br /&gt;separator.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;The Increment property of the control which has a default value of 1 decides on&lt;br /&gt;the amount of increase/decrease in the number. each time we click on the up or&lt;br /&gt;down buttons. n.Increment=10; will increase the number by 10. The two functions&lt;br /&gt;ParseEditText and UpdateEditText get called with every change in the number.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Thereafter, two DomainUpDown controls, which behave in a manner similar to a&lt;br /&gt;NumericUpDown control, are created. These controls display a string instead of a&lt;br /&gt;number. Thus, the value passed to this control can be of any class, since all&lt;br /&gt;classes are derived from object. The user can also type in text directly. The&lt;br /&gt;value typed in must obviously match an item in the collection. The ToString&lt;br /&gt;function of the object is called, to display the value in the up-down control.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;The DomainUpDown control has a property called Items, that returns a&lt;br /&gt;DomainUpDown.DomainUpDownItems object. This object represents the object&lt;br /&gt;collection. Thus, we can use the Add or Remove methods from the above collection&lt;br /&gt;to add or remove the items individually. The Sort property sorts the collection.&lt;br /&gt;We pass an object like yyy to the Add function, which accepts a string and an&lt;br /&gt;enum called HorizontalAlignment. The enum value is stored in the variable i to&lt;br /&gt;facilitate its retrieval at a later stage.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Each time we click on the control, function abc gets called. This is because the&lt;br /&gt;SelectedItemChanged event has been initialized to this function. In this&lt;br /&gt;function, we use the property SelectedIndex, which returns a number depicting&lt;br /&gt;the item that has been selected. The property is used as an array index. It&lt;br /&gt;returns the yyy object at that index. We store this object in c and then, access&lt;br /&gt;the variable i stored in the object yyy.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Thus, the entire object yyy is stored as part of the collection. The string and&lt;br /&gt;the int can both be accessed together. If the SelectedIndex property is not set,&lt;br /&gt;it will not display any value in the control.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;DateTimePicker Control&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;a.cs&lt;br /&gt;&lt;br /&gt;using System;&lt;br /&gt;&lt;br /&gt;using System.Drawing;&lt;br /&gt;&lt;br /&gt;using System.Windows.Forms;&lt;br /&gt;&lt;br /&gt;public class zzz : Form {&lt;br /&gt;&lt;br /&gt;DateTimePicker d;&lt;br /&gt;&lt;br /&gt;public zzz() {&lt;br /&gt;&lt;br /&gt;ClientSize = new Size(504, 293);&lt;br /&gt;&lt;br /&gt;d = new DateTimePicker();&lt;br /&gt;&lt;br /&gt;d.Location = new Point(24, 24);&lt;br /&gt;&lt;br /&gt;d.CalendarFont = new Font("Times New Roman", 8f);&lt;br /&gt;&lt;br /&gt;d.Size = new Size(200, 20);&lt;br /&gt;&lt;br /&gt;d.CalendarForeColor = System.Drawing.SystemColors.WindowText;&lt;br /&gt;&lt;br /&gt;d.ShowCheckBox = true;&lt;br /&gt;&lt;br /&gt;d.ForeColor = System.Drawing.SystemColors.WindowText;&lt;br /&gt;&lt;br /&gt;d.Format = System.Windows.Forms.DateTimePickerFormat.Custom;&lt;br /&gt;&lt;br /&gt;d.BackColor = System.Drawing.SystemColors.Window;&lt;br /&gt;&lt;br /&gt;d.CustomFormat = "\'Date : \'yy MM d - HH\':\'mm\':\'s ddd";&lt;br /&gt;&lt;br /&gt;d.Anchor = AnchorStyles.TopAnchorStyles.Right AnchorStyles.Left;&lt;br /&gt;&lt;br /&gt;DateTime now = DateTime.Now;&lt;br /&gt;&lt;br /&gt;d.Value = now;&lt;br /&gt;&lt;br /&gt;Controls.Add(d);&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;public static void Main()&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;Application.Run(new zzz());&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Screen 4.47 Screen 4.48&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;On running the above program, we see a listbox displaying a date. This date is&lt;br /&gt;the system date. You may wonder as to what is the big deal about displaying a&lt;br /&gt;simple date. But, when you click on the down arrow of the list box, a&lt;br /&gt;fine-looking calendar springs up! The current date is shown highlighted in red.&lt;br /&gt;If you click on any date, it will be instantly displayed in the list box. Once&lt;br /&gt;the date has been chosen, the calendar vanishes. You can change the month by&lt;br /&gt;clicking on the arrows, which are on the left and right of the month.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Now, let us get behind the scenes and unravel the mysteries of this program. We&lt;br /&gt;first create an object d as an instance of class DateTimePicker, thus&lt;br /&gt;encapsulating the standard Windows date time picker control. The control has all&lt;br /&gt;the standard properties like Location, Size, Color etc. The default size, in the&lt;br /&gt;case of the DateTimePicker, is a width of 200 pixels and a height of 23 pixels.&lt;br /&gt;The colors of the control can be changed using CalendarForeColor and ForeColor.&lt;br /&gt;The property CalendarFont decides on the font to be used to display the dates.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Screen 4.49&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;The ShowCheckBox property , which has the default value of false, is set to&lt;br /&gt;true, in order to display a check box on the extreme left of the date displayed&lt;br /&gt;in the control. The checkbox presently is checked.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;If the checkbox is checked, the date is valid. If not, the date is said to be&lt;br /&gt;unset.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;The Format property of the control, which is initialized to Custom, decides the&lt;br /&gt;display format of the date and time in the control. This property can be&lt;br /&gt;initialized to any four of the following enums: Custom, Short, Long and Time.&lt;br /&gt;The last three values use the operating system's format options.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Since the Format property has been given the value of Custom, the property&lt;br /&gt;CustomFormat decides on the display format of the date in the control. This&lt;br /&gt;property is initialized as follows: The word Date:, followed by the year, month&lt;br /&gt;and day separated by a space, followed by a minus sign and finally, the time.&lt;br /&gt;The three ‘ddd’ represent the day of the week in words.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;The Now property of the DateTime class, that returns the current day, is finally&lt;br /&gt;assigned to the Value property of the picker class, which then gets displayed in&lt;br /&gt;the control.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;LinkLabel Control:&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;a.cs&lt;br /&gt;&lt;br /&gt;using System;&lt;br /&gt;&lt;br /&gt;using System.Windows.Forms;&lt;br /&gt;&lt;br /&gt;using System.Drawing;&lt;br /&gt;&lt;br /&gt;public class zzz : Form {&lt;br /&gt;&lt;br /&gt;PropertyGrid pg;&lt;br /&gt;&lt;br /&gt;LinkLabel l;&lt;br /&gt;&lt;br /&gt;Panel p;&lt;br /&gt;&lt;br /&gt;GroupBox g;&lt;br /&gt;&lt;br /&gt;public zzz() {&lt;br /&gt;&lt;br /&gt;l = new LinkLabel();&lt;br /&gt;&lt;br /&gt;l.DisabledLinkColor = (Color)System.Drawing.Color.Blue;&lt;br /&gt;&lt;br /&gt;l.ForeColor = (Color)System.Drawing.Color.Gainsboro;&lt;br /&gt;&lt;br /&gt;l.Location = new System.Drawing.Point(32, 128);&lt;br /&gt;&lt;br /&gt;l.BackColor = (Color)System.Drawing.Color.Transparent;&lt;br /&gt;&lt;br /&gt;l.LinkArea = new LinkArea(13, 28);&lt;br /&gt;&lt;br /&gt;l.Font = new System.Drawing.Font("Tahoma", 12f, System.Drawing.FontStyle.Bold,&lt;br /&gt;System.Drawing.GraphicsUnit.World);&lt;br /&gt;&lt;br /&gt;l.Text = "please click on sonal to see a message box";&lt;br /&gt;&lt;br /&gt;l.Size = new System.Drawing.Size(136, 96);&lt;br /&gt;&lt;br /&gt;l.LinkClicked += new LinkLabelLinkClickedEventHandler(abc);&lt;br /&gt;&lt;br /&gt;ClientSize = new System.Drawing.Size(504, 445);&lt;br /&gt;&lt;br /&gt;pg = new PropertyGrid();&lt;br /&gt;&lt;br /&gt;pg.Dock = System.Windows.Forms.DockStyle.Fill;&lt;br /&gt;&lt;br /&gt;pg.Location = new System.Drawing.Point(3, 16);&lt;br /&gt;&lt;br /&gt;pg.CommandsVisibleIfAvailable = true;&lt;br /&gt;&lt;br /&gt;pg.Text = "propertyGrid1";&lt;br /&gt;&lt;br /&gt;pg.Size = new System.Drawing.Size(242, 405);&lt;br /&gt;&lt;br /&gt;pg.SelectedObject = l ;&lt;br /&gt;&lt;br /&gt;g = new GroupBox();&lt;br /&gt;&lt;br /&gt;g.Location = new System.Drawing.Point(248, 16);&lt;br /&gt;&lt;br /&gt;g.Anchor = AnchorStyles.TopAnchorStyles.Right AnchorStyles.Left;&lt;br /&gt;&lt;br /&gt;g.Text = "LinkLabel Properties";&lt;br /&gt;&lt;br /&gt;g.Size = new System.Drawing.Size(248, 424);&lt;br /&gt;&lt;br /&gt;g.Controls.Add(pg);&lt;br /&gt;&lt;br /&gt;Controls.Add(g);&lt;br /&gt;&lt;br /&gt;p = new Panel();&lt;br /&gt;&lt;br /&gt;p.Size = new System.Drawing.Size(200, 320);&lt;br /&gt;&lt;br /&gt;p.Location = new System.Drawing.Point(24, 40);&lt;br /&gt;&lt;br /&gt;p.BackgroundImage = (Bitmap) new Bitmap("hikingboot.bmp");&lt;br /&gt;&lt;br /&gt;p.Controls.Add(l);&lt;br /&gt;&lt;br /&gt;Controls.Add(p);&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;void abc(object sender, LinkLabelLinkClickedEventArgs e)&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;MessageBox.Show("hi") ;&lt;br /&gt;&lt;br /&gt;l.LinkVisited = true ;&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;public static void Main() {&lt;br /&gt;&lt;br /&gt;Application.Run(new zzz());&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;To avoid any exception from being thrown, you should copy the file&lt;br /&gt;hikingboot.bmp to the current directory. Though the above program is not very&lt;br /&gt;sizeable, its achievements are substantial. The output of this program shows our&lt;br /&gt;screen divided into two parts as follows:&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;(a) The left pane has a picture in the background with some text that behaves&lt;br /&gt;like a hyperlink. Clicking on the hyperlink displays a message box with the&lt;br /&gt;message 'hi' and changes the color of the hyper link.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Screen 4.50&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;(b) On the right hand side, we come across a large number of properties. When we&lt;br /&gt;click on the plus sign in front of the font property, yet more properties get&lt;br /&gt;displayed. On varying the Font, the font of the text displayed in the window on&lt;br /&gt;the left side changes. You can experiment with the other properties too.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Thus, you can achieve a lot without writing tons of code!&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;l is an instance of LinkLabel, which relates to text that can be displayed as a&lt;br /&gt;hyper link. The DisabledLinkColor property of LinkLabel decides the color of the&lt;br /&gt;hyperlink when it is disabled. The ForeColor and the BackColor specify the&lt;br /&gt;foreground and the background color of the control respectively.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;We may not intend to display the entire string as a hyperlink. So, we use the&lt;br /&gt;LinkArea property to ascertain the text that is to be hyper linked. The default&lt;br /&gt;position begins at 0,0.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;The GraphicsUnit enumeration in the Font property specifies a unit of&lt;br /&gt;measurement. The value displayed uses 1/75 of an inch, since the unit of measure&lt;br /&gt;selected is World. We could also use any of the other 6 values in the enum, such&lt;br /&gt;as pixel, millimeter etc.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Screen 4.51&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;The Text property is the most significant property, as it decides the text to be&lt;br /&gt;displayed. The LinkClicked is the event property that will call function abc&lt;br /&gt;each time we click on the link. In this function, we display a Message Box and&lt;br /&gt;change the LinkVisited property to True, which results in a change in the color&lt;br /&gt;of the hyper link. You may observe that this LinkLabel instance l is not added&lt;br /&gt;to the form. Next, we create an instance of a PropertyGrid class and store it in&lt;br /&gt;pg.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;The Dock property decides on the edge of the container that this property will&lt;br /&gt;be docked to. The usage of the Fill style allows docking of the property on all&lt;br /&gt;sides.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;The property CommandsVisibleIfAvailable displays the command pane only for those&lt;br /&gt;objects that expose verbs. As always, the Text property is initialized to some&lt;br /&gt;text that does not get displayed. The property of SelectedObject is assigned the&lt;br /&gt;LinkLabel control, thereby linking it into the grid and facilitating browsing of&lt;br /&gt;the LinkLable control properties. This property also allows us to browse&lt;br /&gt;multiple objects. Finally, we add this object to the Form, thereby indirectly&lt;br /&gt;adding the hyperlink also.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;A group box is merely an anthology of other controls. The Group box control is&lt;br /&gt;the one that contains the PropertyGrid object. We also create an instance of a&lt;br /&gt;Panel object, which, like a GroupBox class, contains other controls. If the&lt;br /&gt;Enabled property in the panels is set to False, all the controls within it will&lt;br /&gt;be disabled. The panel control, by default, is drawn without any Borders. The&lt;br /&gt;BorderStyle property provides us with two-dimensional or three-dimensional&lt;br /&gt;borders, to distinguish a panel from other areas of the form. The Panel class&lt;br /&gt;can also contain scrollbars.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;The Panel class has a property called BackgroundImage that selects the picture&lt;br /&gt;to be displayed in the panel. This Panel class has the LinkLabel added to it, to&lt;br /&gt;which we add the Panel. Thus, we have two controls that have been added directly&lt;br /&gt;to the form, the Panel that has the LinkLabel and the GroupBox that has the&lt;br /&gt;PropertyGrid.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;ListBox Control&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;a.cs&lt;br /&gt;&lt;br /&gt;using System;&lt;br /&gt;&lt;br /&gt;using System.Drawing;&lt;br /&gt;&lt;br /&gt;using System.Windows.Forms;&lt;br /&gt;&lt;br /&gt;public class zzz : Form&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;sss p;&lt;br /&gt;&lt;br /&gt;ListBox l;&lt;br /&gt;&lt;br /&gt;Button b;&lt;br /&gt;&lt;br /&gt;ColorDialog c;&lt;br /&gt;&lt;br /&gt;ImageList i;&lt;br /&gt;&lt;br /&gt;public zzz()&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;b = new Button();&lt;br /&gt;&lt;br /&gt;b.Location = new System.Drawing.Point(16, 200);&lt;br /&gt;&lt;br /&gt;b.Text = "Color";&lt;br /&gt;&lt;br /&gt;b.Size = new System.Drawing.Size(75, 23);&lt;br /&gt;&lt;br /&gt;b.Click += new EventHandler(abc);&lt;br /&gt;&lt;br /&gt;Controls.Add(b);&lt;br /&gt;&lt;br /&gt;c = new ColorDialog();&lt;br /&gt;&lt;br /&gt;p = new sss();&lt;br /&gt;&lt;br /&gt;p.Location = new System.Drawing.Point(64, 90);&lt;br /&gt;&lt;br /&gt;p.Size = new System.Drawing.Size(64, 168);&lt;br /&gt;&lt;br /&gt;Controls.Add(p);&lt;br /&gt;&lt;br /&gt;i = new ImageList();&lt;br /&gt;&lt;br /&gt;i.ImageSize = new Size(24, 22);&lt;br /&gt;&lt;br /&gt;i.Images.Add(new Bitmap("club.bmp"));&lt;br /&gt;&lt;br /&gt;i.Images.Add(new Bitmap("diamond.bmp"));&lt;br /&gt;&lt;br /&gt;i.Images.Add(new Bitmap("heart.bmp"));&lt;br /&gt;&lt;br /&gt;i.Images.Add(new Bitmap("spade.bmp"));&lt;br /&gt;&lt;br /&gt;Size = new System.Drawing.Size(512, 320);&lt;br /&gt;&lt;br /&gt;l = new ListBox();&lt;br /&gt;&lt;br /&gt;l.ForeColor = (Color)System.Drawing.SystemColors.WindowText;&lt;br /&gt;&lt;br /&gt;l.Location = new System.Drawing.Point(8, 24);&lt;br /&gt;&lt;br /&gt;l.IntegralHeight = false;&lt;br /&gt;&lt;br /&gt;l.Size = new System.Drawing.Size(232, 60);&lt;br /&gt;&lt;br /&gt;l.ColumnWidth = 144;&lt;br /&gt;&lt;br /&gt;l.SelectedIndexChanged += new EventHandler(pqr);&lt;br /&gt;&lt;br /&gt;l.Items.AddRange (new object[] {"a1", "a2", "a3", "a4"});&lt;br /&gt;&lt;br /&gt;l.SelectionMode = SelectionMode.MultiSimple;&lt;br /&gt;&lt;br /&gt;Controls.Add(l);&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;void abc(object sender, EventArgs e)&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;if (c.ShowDialog() == DialogResult.OK)&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;l.ForeColor = c.Color;&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;void pqr(object sender, EventArgs e)&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;p.ci();&lt;br /&gt;&lt;br /&gt;int[] se = new int[l.SelectedIndices.Count];&lt;br /&gt;&lt;br /&gt;l.SelectedIndices.CopyTo(se, 0);&lt;br /&gt;&lt;br /&gt;for (int i=0; i&amp;lt;se.Length; i++)&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;int ind = se[i];&lt;br /&gt;&lt;br /&gt;object it = l.Items[ind];&lt;br /&gt;&lt;br /&gt;string s = it.ToString();&lt;br /&gt;&lt;br /&gt;Image im = aaa(s);&lt;br /&gt;&lt;br /&gt;p.ai(im);&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;p.Invalidate();&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;Image aaa(string b)&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;if (b.Equals("a1"))&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;return i.Images[0];&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;else if (b.Equals("a2"))&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;return i.Images[1];&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;else if (b.Equals("a3"))&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;return i.Images[2];&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;else if (b.Equals("a4"))&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;return i.Images[3];&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;else&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;return null;&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;public static void Main()&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;Application.Run(new zzz());&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;public class sss : Panel&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;Image[] i = new Image[4];&lt;br /&gt;&lt;br /&gt;int Cnt=0;&lt;br /&gt;&lt;br /&gt;public virtual void ai(Image img)&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;i[Cnt++] = img;&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;protected override void OnPaint(PaintEventArgs pe)&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;base.OnPaint(pe);&lt;br /&gt;&lt;br /&gt;for (int j=0; j&amp;lt; Cnt; j++)&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;pe.Graphics.DrawImage(i[j], new System.Drawing.Point(0, 30 * j + 5));&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;public virtual void ci()&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;Cnt = 0;&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Before running the executable file, the 4 files with the 'bmp' extension must be&lt;br /&gt;copied from one of the sample directories into the current directory.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Screen 4.52 Screen 4.53&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;When we run the above program, we see a list box with 4 values and a button&lt;br /&gt;labeled 'Color'. When we click on the button, it displays a color dialog box,&lt;br /&gt;which permits us to choose from amongst a plethora of colors. We can select a&lt;br /&gt;color and then, click on the OK button. As a result, the color of the list box&lt;br /&gt;items changes to the selected color. We are also allowed to create our own&lt;br /&gt;custom colors in the dialog box.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Screen 4.54 Screen 4.55&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Each time we select an item from the list box, an image representing the item is&lt;br /&gt;displayed. If we reselect an item that we had selected earlier, the currently&lt;br /&gt;selected image gets replaced by the latest selected one.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;In the program, a Button b is created. It calls function abc whenever it is&lt;br /&gt;clicked. Then, an instance c of class ColorDialog is created. We have created&lt;br /&gt;the class sss. It is derived from the class Panel. It has a member i, which is&lt;br /&gt;an array of 4 images and an int variable named Cnt. The variable Cnt is used as&lt;br /&gt;an index to access the images in the array, and to store the count of the number&lt;br /&gt;of images. When we add the sss object to our Form, nothing gets displayed in the&lt;br /&gt;window. This is because, there are no controls in the panel.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Next, we create an ImageList object called i. This class stores a collection of&lt;br /&gt;images that can be used by other controls such as the Toolbar or ListView.&lt;br /&gt;Bitmaps or Icons are added to this class so that they are available for&lt;br /&gt;exploitation by other controls. Images is a property of type&lt;br /&gt;ImageList.ImageCollection, whose Add method is used to add a bitmap to the&lt;br /&gt;collection. We add a total of 4 bitmaps to our ImageList object.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;The ListBox object l has a property ColumnWidth, that decides on the width, in&lt;br /&gt;pixels, of each column in the ListBox. In a multi-column ListBox, the&lt;br /&gt;ColumnWidth property refers to the width of each column. A value of zero is&lt;br /&gt;connotative of the fact that each column has a default width, taking into&lt;br /&gt;account the data displayed in them. The property IntegralHeight decides whether&lt;br /&gt;partial items can be displayed or not. If the value is set to true, then only&lt;br /&gt;complete items are displayed. In this case, the ListBox will be resized to&lt;br /&gt;ensure that partial items are not displayed.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Each time we select an item, the Event SelectedIndexChanged gets kick started.&lt;br /&gt;This event, in turn, calls function pqr. Every list box has a property called&lt;br /&gt;Items, which is a collection of items displayed in the list box. The data type&lt;br /&gt;of Items is ListBox.ObjectCollection. It has a method called AddRange, which&lt;br /&gt;accepts an array of Child Controls present in the list, sorted on the index.&lt;br /&gt;Since we want the list box to display the text a1, a2, a3 and a4, we initialize&lt;br /&gt;the object array to this array of strings.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;The property SelectionMode decides on the number of items that can be chosen or&lt;br /&gt;selected concomitantly.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Four options available:&lt;br /&gt;&lt;br /&gt;• None : This means that no items can be selected. In effect, it disables the&lt;br /&gt;ListBox.&lt;br /&gt;&lt;br /&gt;• One : This allows us to select only one item at a time,&lt;br /&gt;&lt;br /&gt;• MultiSimple : This lets us choose multiple items at the same time&lt;br /&gt;&lt;br /&gt;• MultiExtended : This enables us to choose multiple items at a time and allows&lt;br /&gt;us to use keys like SHIFT and CONTROL combinations to select the multiple&lt;br /&gt;options.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;We finally add the list box to the Form. Clicking on the button labeled Color&lt;br /&gt;calls ShowDialog from the ColorDialog class, which displays a dialog box with a&lt;br /&gt;zillion colors. Until we click on the OK button or the Cancel button, we cannot&lt;br /&gt;leave the dialog box.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;The button returns one of two values:&lt;br /&gt;&lt;br /&gt;• DialogResult.OK if we click on OK button.&lt;br /&gt;&lt;br /&gt;• DialogResult.CANCEL if we click on the Cancel button.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;If the user opts to click on the OK button, the color selected is assigned to&lt;br /&gt;the ForeColor property of the ListBox.&lt;br /&gt;&lt;br /&gt;The function pqr is the vortex of action. The function is called when any item&lt;br /&gt;is selected or unselected from the list box. We first call function ci from the&lt;br /&gt;class sss, which initializes the variable Cnt to zero.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;The SelectedIndices property returns a collection object named&lt;br /&gt;ListBox.SelectedIndicesCollection, which lists all the items that are currently&lt;br /&gt;selected in the list box. This is so because we can select multiple items from&lt;br /&gt;the list box. First, an array called se of type int is created, depending on the&lt;br /&gt;number of items selected. Then, the selected indices are copied to the array.&lt;br /&gt;The variable ind in the for statement refers to each index selected. The Items&lt;br /&gt;array returns an object that stores the value of each index. As we need the&lt;br /&gt;string representation of the selected item, function ToString is used to convert&lt;br /&gt;the object into a string. This facilitates the possibility of having a list box&lt;br /&gt;containing pictures.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;We then call the function aaa that accepts a string representing the ListBox&lt;br /&gt;item selected, which can be either a1, a2, a3 or a4. It returns an image&lt;br /&gt;representing the string. It then checks the value of the string passed and&lt;br /&gt;returns an image stored in the ImageList object. The function ai merely uses the&lt;br /&gt;variable Cnt to index the array of images with a new picture. Thus, if we select&lt;br /&gt;three images, the array i will contain three pictures. When the for loop&lt;br /&gt;terminates, we call the function Invalidate.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;As we had learnt earlier, function OnPaint displays all the pictures. This is&lt;br /&gt;achieved using the Cnt variable that contains the number of images stored in the&lt;br /&gt;Array. Thus, we use the ImageList class to store the images and the array of&lt;br /&gt;images in the sss class to store the images that need to be displayed each time.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;StatusBar control&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;a.cs&lt;br /&gt;&lt;br /&gt;using System;&lt;br /&gt;&lt;br /&gt;using System.Drawing;&lt;br /&gt;&lt;br /&gt;using System.Windows.Forms;&lt;br /&gt;&lt;br /&gt;public class zzz : Form {&lt;br /&gt;&lt;br /&gt;System.ComponentModel.Container c;&lt;br /&gt;&lt;br /&gt;StatusBar s;&lt;br /&gt;&lt;br /&gt;StatusBarPanel s1;&lt;br /&gt;&lt;br /&gt;StatusBarPanel s2;&lt;br /&gt;&lt;br /&gt;StatusBarPanel s3;&lt;br /&gt;&lt;br /&gt;Timer t;&lt;br /&gt;&lt;br /&gt;public zzz() {&lt;br /&gt;&lt;br /&gt;c = new System.ComponentModel.Container();&lt;br /&gt;&lt;br /&gt;s2 = new StatusBarPanel();&lt;br /&gt;&lt;br /&gt;t = new Timer(c);&lt;br /&gt;&lt;br /&gt;s = new StatusBar();&lt;br /&gt;&lt;br /&gt;s3 = new StatusBarPanel();&lt;br /&gt;&lt;br /&gt;s1 = new StatusBarPanel();&lt;br /&gt;&lt;br /&gt;s2.AutoSize = (StatusBarPanelAutoSize)2;&lt;br /&gt;&lt;br /&gt;s2.Alignment = HorizontalAlignment.Right;&lt;br /&gt;&lt;br /&gt;s2.Width = 76;&lt;br /&gt;&lt;br /&gt;s3.AutoSize = StatusBarPanelAutoSize.Contents;&lt;br /&gt;&lt;br /&gt;s3.Width = 20;&lt;br /&gt;&lt;br /&gt;s1.BorderStyle = StatusBarPanelBorderStyle.None;&lt;br /&gt;&lt;br /&gt;s1.Icon = new Icon("status.ico");&lt;br /&gt;&lt;br /&gt;s1.AutoSize = StatusBarPanelAutoSize.Contents;&lt;br /&gt;&lt;br /&gt;s1.Width = 62;&lt;br /&gt;&lt;br /&gt;s1.Text = "sonal";&lt;br /&gt;&lt;br /&gt;Size = new System.Drawing.Size(512, 320);&lt;br /&gt;&lt;br /&gt;KeyUp += new KeyEventHandler(abc);&lt;br /&gt;&lt;br /&gt;t.Interval = 1000;&lt;br /&gt;&lt;br /&gt;t.Enabled = true;&lt;br /&gt;&lt;br /&gt;t.Tick += new EventHandler(pqr);&lt;br /&gt;&lt;br /&gt;s.Size = new Size(212, 20);&lt;br /&gt;&lt;br /&gt;s.Location = new Point(0, 216);&lt;br /&gt;&lt;br /&gt;s.BackColor = (Color)SystemColors.Control;&lt;br /&gt;&lt;br /&gt;s.Text = "Vijay Mukhi";&lt;br /&gt;&lt;br /&gt;s.ShowPanels = true;&lt;br /&gt;&lt;br /&gt;s.Panels.AddRange((StatusBarPanel[])new StatusBarPanel[] {s1, s2, s3});&lt;br /&gt;&lt;br /&gt;Controls.Add(s);&lt;br /&gt;&lt;br /&gt;s3.Text = "OVR";&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;void abc(object se, KeyEventArgs e)&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;if (e.KeyCode == Keys.Insert)&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;string s = this.s3.Text;&lt;br /&gt;&lt;br /&gt;if (s.Equals("INS"))&lt;br /&gt;&lt;br /&gt;s3.Text = "OVR";&lt;br /&gt;&lt;br /&gt;else&lt;br /&gt;&lt;br /&gt;s3.Text = "INS";&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;void pqr(object se, EventArgs e)&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;DateTime t = DateTime.Now;&lt;br /&gt;&lt;br /&gt;string s = t.ToLongTimeString() ;&lt;br /&gt;&lt;br /&gt;s2.Text = s ;&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;public static void Main()&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;Application.Run(new zzz());&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Every Windows application displays a StatusBar control. This program is going to&lt;br /&gt;introduce the status bar in our window. In order to accomplish this, the file&lt;br /&gt;status.ico needs to be copied from one of the sample directories into the&lt;br /&gt;current directory.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;The above program merely presents a blank window with a status bar at the&lt;br /&gt;bottom. The status bar has the following contents:&lt;br /&gt;&lt;br /&gt;• an icon.&lt;br /&gt;&lt;br /&gt;• the word 'sonal'.&lt;br /&gt;&lt;br /&gt;• the time ticking away in the middle.&lt;br /&gt;&lt;br /&gt;• the status of the Insert key.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Screen 4.56 Screen 4.57&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Each time we use the insert key, the text toggles between INS and OVR.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;The class Container encapsulates zero to one or more components. We create three&lt;br /&gt;status bar objects s1, s2 and s3, which are instances of class StatusBarPanel.&lt;br /&gt;This class, in turn, is derived from class Component. It stores the StatusBar&lt;br /&gt;control panel information. The object s is an instance of class StatusBar, which&lt;br /&gt;represents a Window status bar control. This control has no panels by default.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;The property AutoSize in the StatusBarPanel class regulates the changes&lt;br /&gt;occurring in the panel of a status bar, whenever the status bar is resized. The&lt;br /&gt;values are obtained from the enumeration StatusBarPanelAutoSize that has the&lt;br /&gt;following three values:&lt;br /&gt;&lt;br /&gt;• Contents: As is evident from the name, the contents of the status bar decide&lt;br /&gt;how its size will change.&lt;br /&gt;&lt;br /&gt;• None: The status bar panel does not change whenever the status bar is resized.&lt;br /&gt;&lt;br /&gt;• Spring: The panel shares the available space with all other panels having a&lt;br /&gt;setting of Spring, after yielding space to the panels having either the Contents&lt;br /&gt;or None settings.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Text can be aligned in a status bar panel in 3 ways with reference to the status&lt;br /&gt;bar, i.e. Left, Right and Center. The Alignment property, by default, has a&lt;br /&gt;value of Left. For the status bar panel s2, we have set it to Right. The default&lt;br /&gt;width is 100 pixels for a status bar panel. An Icon can be specified along with&lt;br /&gt;the text that is displayed. To do so, we initialize the Icon property to a .ico&lt;br /&gt;file. An .ico file is a small transparent bitmap image.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;The KeyUp Event is fired whenever a key is released in the focused control. In&lt;br /&gt;our case, function pqr is called. The Timer class, that implements a Windows&lt;br /&gt;timer, merely activates an event at a particular time interval. This timer,&lt;br /&gt;which can be used in a window, is designed for a single threaded environment&lt;br /&gt;only, where User Interface threads are being executed. The Interval property&lt;br /&gt;decides the time, in milliseconds, between timer ticks. The Enabled property&lt;br /&gt;sets the timer On. The garbage collector does not interfere with the timer while&lt;br /&gt;it is running. The Event Tick will call the function pqr whenever the time&lt;br /&gt;interval set in the timer elapses. In our case, it is occurs after one second.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;The StatusBar is made up of panels. The ShowPanels property displays all the&lt;br /&gt;panels that are added using the AddRange method, i.e. s1, s2 and s3. Finally,&lt;br /&gt;the text of the last panel is set to OVR and the StatusBar object is added to&lt;br /&gt;the form.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;The function abc gets called each time we press a key in our form. This function&lt;br /&gt;is called with the parameter e of KeyEventArgs, which has a member KeyCode,&lt;br /&gt;which contains a number corresponding to the key pressed. As it is difficult to&lt;br /&gt;remember the numbers assigned to every key, the enumeration Keys is used to&lt;br /&gt;represent the keys. If the Insert key is pressed, the current Text displayed in&lt;br /&gt;the status bar panel s3 is retrieved and the value gets toggled from OVR to INS.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;On completion of the timer interval, we use the Now property of the DateTime&lt;br /&gt;class to provide us with the current time. This is then supplied to s2.Text,&lt;br /&gt;which updates the status bar panel with the current time every second. What you&lt;br /&gt;can display in a status bar is limited by your imagination. It is common to&lt;br /&gt;display the status of keys and the time on the status bar.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Tab Control&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;a.cs&lt;br /&gt;&lt;br /&gt;using System;&lt;br /&gt;&lt;br /&gt;using System.Drawing;&lt;br /&gt;&lt;br /&gt;using System.Windows.Forms;&lt;br /&gt;&lt;br /&gt;public class zzz : Form {&lt;br /&gt;&lt;br /&gt;GroupBox g2;&lt;br /&gt;&lt;br /&gt;GroupBox g1;&lt;br /&gt;&lt;br /&gt;ImageList i;&lt;br /&gt;&lt;br /&gt;TabPage t1;&lt;br /&gt;&lt;br /&gt;TabPage t2;&lt;br /&gt;&lt;br /&gt;TabControl t;&lt;br /&gt;&lt;br /&gt;public zzz()&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;ClientSize = new Size(546, 293);&lt;br /&gt;&lt;br /&gt;g1 = new GroupBox();&lt;br /&gt;&lt;br /&gt;g1.Location = new Point(12, 16);&lt;br /&gt;&lt;br /&gt;g1.Text = "Sonal";&lt;br /&gt;&lt;br /&gt;g1.Size = new Size(202, 144);&lt;br /&gt;&lt;br /&gt;g2 = new GroupBox();&lt;br /&gt;&lt;br /&gt;g2.Location = new Point(12, 16);&lt;br /&gt;&lt;br /&gt;g2.Text = "Vijay mukhi";&lt;br /&gt;&lt;br /&gt;g2.Size = new Size(202, 128);&lt;br /&gt;&lt;br /&gt;i = new ImageList();&lt;br /&gt;&lt;br /&gt;t1 = new TabPage();&lt;br /&gt;&lt;br /&gt;t2 = new TabPage();&lt;br /&gt;&lt;br /&gt;t = new TabControl();&lt;br /&gt;&lt;br /&gt;t1.Text = "Mukhi";&lt;br /&gt;&lt;br /&gt;t1.Size = new Size(224, 193);&lt;br /&gt;&lt;br /&gt;t1.ImageIndex = 0;&lt;br /&gt;&lt;br /&gt;t1.TabIndex = 0;&lt;br /&gt;&lt;br /&gt;t2.Text = "Vijay";&lt;br /&gt;&lt;br /&gt;t2.ImageIndex = 1;&lt;br /&gt;&lt;br /&gt;t2.TabIndex = 1;&lt;br /&gt;&lt;br /&gt;t.Location = new Point(24, 32);&lt;br /&gt;&lt;br /&gt;t.Size = new Size(232, 220);&lt;br /&gt;&lt;br /&gt;t.SelectedIndex = 0;&lt;br /&gt;&lt;br /&gt;t.ImageList = i;&lt;br /&gt;&lt;br /&gt;i.Images.Add((Bitmap)new Bitmap("calendar.bmp"));&lt;br /&gt;&lt;br /&gt;i.Images.Add((Bitmap)new Bitmap("note.bmp"));&lt;br /&gt;&lt;br /&gt;t.ImageList = i;&lt;br /&gt;&lt;br /&gt;Controls.Add(t);&lt;br /&gt;&lt;br /&gt;t1.Controls.Add(g1);&lt;br /&gt;&lt;br /&gt;t2.Controls.Add(g2);&lt;br /&gt;&lt;br /&gt;t.Controls.Add(t1);&lt;br /&gt;&lt;br /&gt;t.Controls.Add(t2);&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;public static void Main()&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;Application.Run(new zzz());&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;This program requires two bitmaps called note.bmp and calendar.bmp.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;We start by creating two GroupBox controls named g1 and g2, and change the&lt;br /&gt;properties of Locations, Size and Text. The ImageList i stores a list of images.&lt;br /&gt;We next create two TabPage objects t1 and t2. A TabPage class implements a&lt;br /&gt;single page of the TabControl class. It is a panel class having the properties&lt;br /&gt;of a TabItem.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Screen 4.58 Screen 4.59&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;The string assigned to the Text property is displayed as the tab page text. We&lt;br /&gt;can set it to a certain size. The ImageIndex property is an index into the&lt;br /&gt;ImageList list object, which is associated with the TabControl. The TabPage&lt;br /&gt;objects are added to the TabControl using the Controls collection. The ImageList&lt;br /&gt;property is initialized to the list of images denoted by the ImageList class.&lt;br /&gt;Thus, the ImageIndex member decides on the image that will be displayed along&lt;br /&gt;with the Text in the TabControl.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;A TabControl shows a list of TabPages. Clicking on the tab activates them. A&lt;br /&gt;TabControl, in other words, is like a series of Dialog boxes containing&lt;br /&gt;controls, organized in a logical fashion. The controls that would earlier have&lt;br /&gt;appeared in one large dialog box, are now placed in separate dialog boxes.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;As mentioned earlier, we add the GroupBox to the Control collection of the&lt;br /&gt;individual TabPage objects t1 and t2. The controls that we require are added to&lt;br /&gt;the GroupBox. The GroupBox, in turn, gets added to the TabPage. From then on,&lt;br /&gt;the TabControl takes over and we can flick between TabPages with ease.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;ToolTip Control&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;a.cs&lt;br /&gt;&lt;br /&gt;using System;&lt;br /&gt;&lt;br /&gt;using System.Drawing;&lt;br /&gt;&lt;br /&gt;using System.Windows.Forms;&lt;br /&gt;&lt;br /&gt;public class zzz : Form {&lt;br /&gt;&lt;br /&gt;System.ComponentModel.Container c;&lt;br /&gt;&lt;br /&gt;ToolTip t;&lt;br /&gt;&lt;br /&gt;PictureBox p1;&lt;br /&gt;&lt;br /&gt;PictureBox p2;&lt;br /&gt;&lt;br /&gt;public zzz()&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;c = new System.ComponentModel.Container();&lt;br /&gt;&lt;br /&gt;t = new ToolTip(c);&lt;br /&gt;&lt;br /&gt;t.Active = true;&lt;br /&gt;&lt;br /&gt;t.ShowAlways = true;&lt;br /&gt;&lt;br /&gt;t.AutomaticDelay = 100;&lt;br /&gt;&lt;br /&gt;t.AutoPopDelay = 100;&lt;br /&gt;&lt;br /&gt;t.InitialDelay = 100;&lt;br /&gt;&lt;br /&gt;t.ReshowDelay = 100;&lt;br /&gt;&lt;br /&gt;p1 = new PictureBox();&lt;br /&gt;&lt;br /&gt;p2 = new PictureBox();&lt;br /&gt;&lt;br /&gt;Size = new Size(512, 300);&lt;br /&gt;&lt;br /&gt;p1.Location = new Point(8, 7);&lt;br /&gt;&lt;br /&gt;p1.Size = new Size(20, 20);&lt;br /&gt;&lt;br /&gt;p1.Image = new Bitmap("open.bmp");&lt;br /&gt;&lt;br /&gt;t.SetToolTip(p1, "vijay");&lt;br /&gt;&lt;br /&gt;Controls.Add(p1);&lt;br /&gt;&lt;br /&gt;p2.Location = new Point(28, 7);&lt;br /&gt;&lt;br /&gt;p2.Size = new Size(20, 20);&lt;br /&gt;&lt;br /&gt;p2.Image = new Bitmap("new.bmp");&lt;br /&gt;&lt;br /&gt;t.SetToolTip(p2, "mukhi");&lt;br /&gt;&lt;br /&gt;Controls.Add(p2);&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;public static void Main()&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;Application.Run(new zzz());&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;All of us need help at some point in time. The ToolTip control is just the&lt;br /&gt;antidote for all maladies. Before running the executable, copy the two bitmaps&lt;br /&gt;open.bmp and new.bmp to the current directory.These two bitmaps will be&lt;br /&gt;displayed as two separate images on our form. Whenever we move our cursor close&lt;br /&gt;to them, a yellow colored help text comes into sight.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;A ToolTip class gives us a small window that contains a single line of text that&lt;br /&gt;describes what a control stands for. The real productivity of Bitmaps and Icons&lt;br /&gt;can be fully harnessed only if we are able to discern their functionality. The&lt;br /&gt;Active property, if set to false, disables the ToolTip control. The ShowAlways&lt;br /&gt;property will show a ToolTip despite the fact that the control it is assigned&lt;br /&gt;to, is currently disabled.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Screen 4.60 Screen 4.61&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;The following four properties have been set for the Tooltip control:&lt;br /&gt;&lt;br /&gt;• AutomaticDelay: It denotes the time in milliseconds that elapses before the&lt;br /&gt;tool tip appears. The default is 500 milliseconds.&lt;br /&gt;&lt;br /&gt;• AutoPopDelay: It is the time in milliseconds for which the ToolTip remains&lt;br /&gt;visible to the user while the mouse is stationary on the control. Its default&lt;br /&gt;value is 10 times the value of the property AutomaticDelay.&lt;br /&gt;&lt;br /&gt;• InitialDelay: It is the time in milliseconds for which the mouse must remain&lt;br /&gt;fixed within the control, before the tool tip becomes visible again. The default&lt;br /&gt;value is equal to that of property AutomaticDelay.&lt;br /&gt;&lt;br /&gt;• ReshowDelay: It is the time in milliseconds that elapses, as the mouse moves&lt;br /&gt;from one control to another. The default is 1/5 of the property AutomaticDelay.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Any amendment made to the property AutomaticDelay has an impact on the above&lt;br /&gt;properties. Thus, it is advisable to first change the AutomaticDelay property,&lt;br /&gt;followed by the other properties.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Two PictureBox controls have been added in our window. We add these two pictures&lt;br /&gt;to the form. This class can display all types of images, including bitmaps,&lt;br /&gt;icons, JPEG, GIF and many more. The Image property decides on the image that&lt;br /&gt;gets displayed. By default, the image has no borders and can be clipped too.&lt;br /&gt;&lt;br /&gt;The ToolTip class has a method called SetToolTip, which accepts a control as the&lt;br /&gt;first parameter and the text of the tool tip as the second. Henceforth, whenever&lt;br /&gt;the mouse is positioned on that control, the tool tip will be displayed.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;TrackBar Control&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;a.cs&lt;br /&gt;&lt;br /&gt;using System;&lt;br /&gt;&lt;br /&gt;using System.Drawing;&lt;br /&gt;&lt;br /&gt;using System.Windows.Forms;&lt;br /&gt;&lt;br /&gt;public class zzz : Form&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;TrackBar t;&lt;br /&gt;&lt;br /&gt;Label l;&lt;br /&gt;&lt;br /&gt;public zzz() {&lt;br /&gt;&lt;br /&gt;Size = new Size(512, 320);&lt;br /&gt;&lt;br /&gt;t = new TrackBar();&lt;br /&gt;&lt;br /&gt;t.Location = new Point(8, 24);&lt;br /&gt;&lt;br /&gt;t.Size = new Size(200, 42);&lt;br /&gt;&lt;br /&gt;t.ValueChanged += new EventHandler(abc);&lt;br /&gt;&lt;br /&gt;t.TickFrequency = 5;&lt;br /&gt;&lt;br /&gt;t.Minimum = 0;&lt;br /&gt;&lt;br /&gt;t.Maximum = 100;&lt;br /&gt;&lt;br /&gt;t.SmallChange = 5;&lt;br /&gt;&lt;br /&gt;t.LargeChange = 25;&lt;br /&gt;&lt;br /&gt;t.Orientation = Orientation.Horizontal;&lt;br /&gt;&lt;br /&gt;t.TickStyle = TickStyle.Both;&lt;br /&gt;&lt;br /&gt;l = new Label();&lt;br /&gt;&lt;br /&gt;l.Location = new Point(112, 192);&lt;br /&gt;&lt;br /&gt;l.Text = t.Value.ToString();&lt;br /&gt;&lt;br /&gt;Controls.Add(t);&lt;br /&gt;&lt;br /&gt;Controls.Add(l);&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;void abc(object source, EventArgs e)&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;l.Text = t.Value.ToString();&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;public static void Main() {&lt;br /&gt;&lt;br /&gt;Application.Run(new zzz());&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;The TrackBar class resembles a scroll bar in many respects, but its interaction&lt;br /&gt;with the user is distinct. We can configure values that the TrackBar represents&lt;br /&gt;and also define increments for off-button clicks. The TrackBar can be positioned&lt;br /&gt;horizontally or vertically. The number of ticks that are displayed can also be&lt;br /&gt;configured.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Each time that the value of the TrackBar changes, the Event property&lt;br /&gt;ValueChanged calls the function abc. The property TickFrequency specifies the&lt;br /&gt;number of ticks that will be drawn. The lower limit of the range of the TrackBar&lt;br /&gt;is confined to a value decided by the Minimum property, which in our case is 0.&lt;br /&gt;The upper limit of the range of the TrackBar is confined to a value determined&lt;br /&gt;by the Maximum property, which in our case is 100.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Screen 4.62 Screen 4.63&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;We may not want to draw 100 ticks to represent all the values. So, we specify a&lt;br /&gt;value such as 5 as the TickFrequency. This will draw a tick mark at a spacing of&lt;br /&gt;5, resulting in a total of 20 ticks. Thus, each tick will represent 5 units of&lt;br /&gt;the TrackBar's range of values.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;The property SmallChange decides on the magnitude of change that occurs in the&lt;br /&gt;TrackBar, whenever the user presses the Up arrow key or the Down arrow key to&lt;br /&gt;move the TrackBar thumb. The value assigned to the LargeChange property is used&lt;br /&gt;whenever we click on the side, or use the Page Up and Page Down keys.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;The Orientation property can take only two values i.e. horizontal or vertical.&lt;br /&gt;The TickStyle property uses an enumeration that can have four values:&lt;br /&gt;&lt;br /&gt;• None: for no tick marks.&lt;br /&gt;&lt;br /&gt;• Both: for tick marks on both sides of the control.&lt;br /&gt;&lt;br /&gt;• BottomRight: for tick marks at the bottom for a horizontal control, and on the&lt;br /&gt;right for a vertical control .&lt;br /&gt;&lt;br /&gt;• TopLeft: this is the reverse of BottomRight.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;We then use a label to display the value of the track bar on the form. This&lt;br /&gt;value is stored in the property Value of the label class. Finally, we add the&lt;br /&gt;label and TrackBar. Function abc merely updates the label to depict the current&lt;br /&gt;position of the TrackBar.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Tree-Node Control&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;a.cs&lt;br /&gt;&lt;br /&gt;using System;&lt;br /&gt;&lt;br /&gt;using System.IO;&lt;br /&gt;&lt;br /&gt;using System.Resources;&lt;br /&gt;&lt;br /&gt;using System.ComponentModel;&lt;br /&gt;&lt;br /&gt;using System.Drawing;&lt;br /&gt;&lt;br /&gt;using System.Windows.Forms;&lt;br /&gt;&lt;br /&gt;using System.Runtime.InteropServices;&lt;br /&gt;&lt;br /&gt;public class zzz : Form&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;TreeView d;&lt;br /&gt;&lt;br /&gt;ImageList i;&lt;br /&gt;&lt;br /&gt;public zzz()&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;ClientSize = new System.Drawing.Size(502, 293);&lt;br /&gt;&lt;br /&gt;i = new ImageList();&lt;br /&gt;&lt;br /&gt;i.Images.Add(new Bitmap("clsdfold.bmp"));&lt;br /&gt;&lt;br /&gt;i.Images.Add(new Bitmap("openfold.bmp"));&lt;br /&gt;&lt;br /&gt;d = new TreeView();&lt;br /&gt;&lt;br /&gt;d.ImageList = (ImageList)i;&lt;br /&gt;&lt;br /&gt;d.ForeColor = (Color)System.Drawing.SystemColors.WindowText;&lt;br /&gt;&lt;br /&gt;d.Location = new System.Drawing.Point(24, 16);&lt;br /&gt;&lt;br /&gt;d.AllowDrop = true;&lt;br /&gt;&lt;br /&gt;d.Indent = 19;&lt;br /&gt;&lt;br /&gt;d.Text = "treeView1";&lt;br /&gt;&lt;br /&gt;d.SelectedImageIndex = 1;&lt;br /&gt;&lt;br /&gt;d.Size = new System.Drawing.Size(200, 264);&lt;br /&gt;&lt;br /&gt;d.AfterSelect += new TreeViewEventHandler(sss);&lt;br /&gt;&lt;br /&gt;d.BeforeExpand += new TreeViewCancelEventHandler(eee);&lt;br /&gt;&lt;br /&gt;Controls.Add(d);&lt;br /&gt;&lt;br /&gt;string[] dr = Environment.GetLogicalDrives();&lt;br /&gt;&lt;br /&gt;for (int ii = 0; ii &amp;lt; dr.Length; ii++)&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;if (PlatformInvokeKernel32.GetDriveType(dr[ii]) ==&lt;br /&gt;PlatformInvokeKernel32.DRIVE_FIXED)&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;TreeNode c = new TreeNode(dr[ii]);&lt;br /&gt;&lt;br /&gt;d.Nodes.Add(c);&lt;br /&gt;&lt;br /&gt;ddd(c);&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;void ddd(TreeNode n)&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;DirectoryInfo dir = new DirectoryInfo(ppp(n));&lt;br /&gt;&lt;br /&gt;DirectoryInfo[] e = dir.GetDirectories ();&lt;br /&gt;&lt;br /&gt;for (int i = 0; i &amp;lt; e.Length; i++)&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;string na = e[i].Name;&lt;br /&gt;&lt;br /&gt;if (!na.Equals(".") &amp;amp;&amp;amp; !na.Equals(".."))&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;n.Nodes.Add(new TreeNode(na));&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;void sss(object source, TreeViewEventArgs e)&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;Text = "Windows.Forms File Explorer - " + e.Node.Text;&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;void eee(object source, TreeViewCancelEventArgs e)&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;TreeNode n = (TreeNode)e.Node;&lt;br /&gt;&lt;br /&gt;for (int i = 0; i &amp;lt; n.Nodes.Count; i++)&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;ddd(n.Nodes[i]);&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;string ppp(TreeNode node)&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;if (node.Parent == null)&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;return node.Text;&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;return Path.Combine(ppp(node.Parent), node.Text);&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;[STAThread]&lt;br /&gt;&lt;br /&gt;public static void Main(string[] args) {&lt;br /&gt;&lt;br /&gt;Application.Run(new zzz());&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;public class PlatformInvokeKernel32&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;[DllImport("KERNEL32", CharSet=System.Runtime.InteropServices.CharSet.Auto)]&lt;br /&gt;&lt;br /&gt;public static extern int GetDriveType(string lpRootPathName);&lt;br /&gt;&lt;br /&gt;public const int DRIVE_FIXED = 3;&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Copy the two bmp files clsdfold.bmp and openfold.bmp required for this program,&lt;br /&gt;from the samples directory into your current directory.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Here, we first create a TreeView control called d. A TreeView displays a list of&lt;br /&gt;items or nodes in a hierarchical manner. A node consists of a caption and a&lt;br /&gt;bitmap; the bitmap is optional. The user can select a node and collapse it or&lt;br /&gt;expand it by clicking on the plus or minus sign, displayed alongside,&lt;br /&gt;respectively.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;We have an ImageList control i that represents two images. We initialize the&lt;br /&gt;ImageList property of the TreeView control to the Image List. Thus, the TreeView&lt;br /&gt;can now use the images stored in the ImageList. The AllowDrop property&lt;br /&gt;facilitates the use of the control for events and Drag and Drop operations.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;The property Indent decides the indent in the pixels. The SelectedImageIndex&lt;br /&gt;determines as to which picture is to be used from the ImageList when the user&lt;br /&gt;selects an item in the control.&lt;br /&gt;&lt;br /&gt;There are a large number of events that get triggered off, but we are capturing&lt;br /&gt;only the following two:&lt;br /&gt;&lt;br /&gt;• AfterSelect, that calls function sss&lt;br /&gt;&lt;br /&gt;• BeforeExapnd, that calls functions eee.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;The EventHandler functions use different delegate types.&lt;br /&gt;&lt;br /&gt;The class Environment has a static function GetLogicalDrives that returns an&lt;br /&gt;array of strings, which denotes the logical drives on our machine. If machine&lt;br /&gt;has three drives, C:\, D:\and E:\, the for loop is repeated thrice.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Using the Platform invoke API, we can call any code in any dll. So, we first&lt;br /&gt;call the external function GetDriveType and compare it with the value retrieved&lt;br /&gt;for DRIVE_FIXED, which is 3 in this case.&lt;br /&gt;&lt;br /&gt;If there are 3 fixed drives then the if statement is true for 3 of them,&lt;br /&gt;resulting in the display of 3 nodes only. If you have only one fixed drive i.e.&lt;br /&gt;C, you will see only one node on your screen.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Screen 4.64 Screen 4.65&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;A TreeNode object is created, which is used in the TreeView. The constructor is&lt;br /&gt;passed the drive name to enable it to be displayed as a label for the node. A&lt;br /&gt;TreeView is made up of Nodes. It has a property called Nodes, which is of type&lt;br /&gt;TreeNodeCollection. Using the Add member of the collection, we add the tree&lt;br /&gt;node. Thereafter, function ddd is called with this newly created and empty tree&lt;br /&gt;node object.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Function ppp is called with an empty tree node. If the node has no parent, the&lt;br /&gt;text associated with the node is returned. But if a parent exists, we use the&lt;br /&gt;static function Combine, of the Path class, to combine the two strings together.&lt;br /&gt;The Combine function calls the ppp function recursively.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;To begin with, the value of ppp(n) is C:\. The object dir represents directories&lt;br /&gt;in the C drive. To obtain all the directories present on the drive, we call&lt;br /&gt;function GetDirectories, which returns an array of DirectoryInfo objects. The&lt;br /&gt;Name member returns the name of the directory. If the return value is not a . or&lt;br /&gt;.., we add the directory name to the list of Nodes.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;In the AfterSelect event, function sss gets called. In this function, we change&lt;br /&gt;the Text of the form to the node selected. This is possible because the node&lt;br /&gt;label is passed as a parameter to TreeViewEventArgs. The function eee recovers&lt;br /&gt;the current active node from the parameter passed to it. Using the Count&lt;br /&gt;property, we calculate the number of nodes contained in the active node and&lt;br /&gt;thereafter, iterate through each of these nodes. The same function ddd is called&lt;br /&gt;with every node.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;This is how we can display the sub-directories contained within directories and&lt;br /&gt;the files displayed within the directories. Data is sent to the control, which&lt;br /&gt;merely returns the same data in a tree like form. When we click on the first&lt;br /&gt;plus (+) sign of C:\, the function ppp(n) returns the entire path name of each&lt;br /&gt;directory in a recursive fashion.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Docking&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;a.cs&lt;br /&gt;&lt;br /&gt;using System;&lt;br /&gt;&lt;br /&gt;using System.Drawing;&lt;br /&gt;&lt;br /&gt;using System.Windows.Forms;&lt;br /&gt;&lt;br /&gt;public class zzz :Form {&lt;br /&gt;&lt;br /&gt;Panel p1;&lt;br /&gt;&lt;br /&gt;Panel p2;&lt;br /&gt;&lt;br /&gt;GroupBox g;&lt;br /&gt;&lt;br /&gt;Button b;&lt;br /&gt;&lt;br /&gt;RadioButton r1;&lt;br /&gt;&lt;br /&gt;RadioButton r2;&lt;br /&gt;&lt;br /&gt;RadioButton r3;&lt;br /&gt;&lt;br /&gt;RadioButton r4;&lt;br /&gt;&lt;br /&gt;RadioButton r5;&lt;br /&gt;&lt;br /&gt;RadioButton r6;&lt;br /&gt;&lt;br /&gt;RadioButton rs;&lt;br /&gt;&lt;br /&gt;Splitter s;&lt;br /&gt;&lt;br /&gt;public zzz () {&lt;br /&gt;&lt;br /&gt;g = new GroupBox();&lt;br /&gt;&lt;br /&gt;rs = new RadioButton();&lt;br /&gt;&lt;br /&gt;r5 = new RadioButton();&lt;br /&gt;&lt;br /&gt;s = new Splitter();&lt;br /&gt;&lt;br /&gt;b = new Button();&lt;br /&gt;&lt;br /&gt;r6 = new RadioButton();&lt;br /&gt;&lt;br /&gt;r4 = new RadioButton();&lt;br /&gt;&lt;br /&gt;r1 = new RadioButton();&lt;br /&gt;&lt;br /&gt;r3 = new RadioButton();&lt;br /&gt;&lt;br /&gt;p1 = new Panel();&lt;br /&gt;&lt;br /&gt;p2 = new Panel();&lt;br /&gt;&lt;br /&gt;r2 = new RadioButton();&lt;br /&gt;&lt;br /&gt;Location = new Point(100, 100);&lt;br /&gt;&lt;br /&gt;SizeGripStyle = SizeGripStyle.Show;&lt;br /&gt;&lt;br /&gt;ClientSize = new Size(448, 400);&lt;br /&gt;&lt;br /&gt;g.Location = new Point(16, 152);&lt;br /&gt;&lt;br /&gt;g.TabStop = false;&lt;br /&gt;&lt;br /&gt;g.Text = "&amp;amp;Dock";&lt;br /&gt;&lt;br /&gt;g.Size = new Size(88, 176);&lt;br /&gt;&lt;br /&gt;s.BackColor = Color.Blue;&lt;br /&gt;&lt;br /&gt;s.Dock = DockStyle.Right;&lt;br /&gt;&lt;br /&gt;s.Location = new Point(325, 0);&lt;br /&gt;&lt;br /&gt;s.Size = new Size(3, 400);&lt;br /&gt;&lt;br /&gt;b.BackColor = SystemColors.Control;&lt;br /&gt;&lt;br /&gt;b.FlatStyle = FlatStyle.Popup;&lt;br /&gt;&lt;br /&gt;b.Anchor = AnchorStyles.None;&lt;br /&gt;&lt;br /&gt;b.Text = "Demo Button";&lt;br /&gt;&lt;br /&gt;rs.Text = "rs";&lt;br /&gt;&lt;br /&gt;rs.Size = new Size(100, 23);&lt;br /&gt;&lt;br /&gt;r5.Location = new Point(8, 120);&lt;br /&gt;&lt;br /&gt;r5.Text = "&amp;amp;Right";&lt;br /&gt;&lt;br /&gt;r5.Size = new Size(72, 24);&lt;br /&gt;&lt;br /&gt;r5.Click += new System.EventHandler(abc);&lt;br /&gt;&lt;br /&gt;r6.Location = new Point(8, 144);&lt;br /&gt;&lt;br /&gt;r6.Text = "&amp;amp;Fill";&lt;br /&gt;&lt;br /&gt;r6.Size = new Size(72, 24);&lt;br /&gt;&lt;br /&gt;r6.Click += new System.EventHandler(abc);&lt;br /&gt;&lt;br /&gt;r4.Location = new Point(8, 96);&lt;br /&gt;&lt;br /&gt;r4.Text = "&amp;amp;Bottom";&lt;br /&gt;&lt;br /&gt;r4.Size = new Size(72, 24);&lt;br /&gt;&lt;br /&gt;r4.Click += new System.EventHandler(abc);&lt;br /&gt;&lt;br /&gt;r1.Checked = true;&lt;br /&gt;&lt;br /&gt;r1.Location = new Point(8, 24);&lt;br /&gt;&lt;br /&gt;r1.Text = "&amp;amp;None";&lt;br /&gt;&lt;br /&gt;r1.Size = new Size(72, 24);&lt;br /&gt;&lt;br /&gt;r1.Click += new System.EventHandler(abc);&lt;br /&gt;&lt;br /&gt;r3.Location = new Point(8, 72);&lt;br /&gt;&lt;br /&gt;r3.Text = "&amp;amp;Left";&lt;br /&gt;&lt;br /&gt;r3.Size = new Size(72, 24);&lt;br /&gt;&lt;br /&gt;r3.Click += new System.EventHandler(abc);&lt;br /&gt;&lt;br /&gt;p1.Text = "ButtonPanel";&lt;br /&gt;&lt;br /&gt;p1.Size = new Size(325, 400);&lt;br /&gt;&lt;br /&gt;p1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;&lt;br /&gt;&lt;br /&gt;p1.Dock = DockStyle.Fill;&lt;br /&gt;&lt;br /&gt;p1.BackColor = Color.Green;&lt;br /&gt;&lt;br /&gt;p2.Location = new Point(328, 0);&lt;br /&gt;&lt;br /&gt;p2.Text = "ControlsPanel";&lt;br /&gt;&lt;br /&gt;p2.Size = new Size(120, 400);&lt;br /&gt;&lt;br /&gt;p2.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;&lt;br /&gt;&lt;br /&gt;p2.Dock = DockStyle.Right;&lt;br /&gt;&lt;br /&gt;r2.Location = new Point(8, 48);&lt;br /&gt;&lt;br /&gt;r2.Text = "&amp;amp;Top";&lt;br /&gt;&lt;br /&gt;r2.Click += new System.EventHandler(abc);&lt;br /&gt;&lt;br /&gt;Controls.Add(p2);&lt;br /&gt;&lt;br /&gt;Controls.Add(s);&lt;br /&gt;&lt;br /&gt;Controls.Add(p1);&lt;br /&gt;&lt;br /&gt;p1.Controls.Add(b);&lt;br /&gt;&lt;br /&gt;p2.Controls.Add(g);&lt;br /&gt;&lt;br /&gt;g.Controls.Add(r4);&lt;br /&gt;&lt;br /&gt;g.Controls.Add(r3);&lt;br /&gt;&lt;br /&gt;g.Controls.Add(r1);&lt;br /&gt;&lt;br /&gt;g.Controls.Add(r5);&lt;br /&gt;&lt;br /&gt;g.Controls.Add(r6);&lt;br /&gt;&lt;br /&gt;g.Controls.Add(r2);&lt;br /&gt;&lt;br /&gt;rs = r1;&lt;br /&gt;&lt;br /&gt;aaa();&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;void aaa() {&lt;br /&gt;&lt;br /&gt;if (rs == r1)&lt;br /&gt;&lt;br /&gt;b.Dock = DockStyle.None;&lt;br /&gt;&lt;br /&gt;else if (rs == r2)&lt;br /&gt;&lt;br /&gt;b.Dock = DockStyle.Top;&lt;br /&gt;&lt;br /&gt;else if (rs == r3)&lt;br /&gt;&lt;br /&gt;b.Dock = DockStyle.Left;&lt;br /&gt;&lt;br /&gt;else if (rs == r4)&lt;br /&gt;&lt;br /&gt;b.Dock = DockStyle.Bottom;&lt;br /&gt;&lt;br /&gt;else if (rs == r5)&lt;br /&gt;&lt;br /&gt;b.Dock = DockStyle.Right;&lt;br /&gt;&lt;br /&gt;else&lt;br /&gt;&lt;br /&gt;b.Dock = DockStyle.Fill;&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;protected void abc(object s, EventArgs e)&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;rs = (RadioButton)s;&lt;br /&gt;&lt;br /&gt;aaa();&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;public static void Main() {&lt;br /&gt;&lt;br /&gt;Application.Run(new zzz());&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;This program may be generously proportioned, however it is very simple to&lt;br /&gt;comprehend. We have created seven radio buttons, one GroupBox, two panels, one&lt;br /&gt;splitter and one button. The SizeGripStyle property can have three values:&lt;br /&gt;&lt;br /&gt;• Show: that always shows the sizing grip.&lt;br /&gt;&lt;br /&gt;• Hide : that always hides the sizing grip.&lt;br /&gt;&lt;br /&gt;• Auto : that shows and hides the sizing grip as necessary.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;The sizing handle is always shown at the right hand corner of the form.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;The button has a FlatStyle of Popup that determines the appearance of the&lt;br /&gt;button. Clicking on any of the Radio Buttons results in a call to the function&lt;br /&gt;abc. This is as a consequence of the Event property Click having been set to&lt;br /&gt;abc. Next, we add all the controls to the Form. The repetitive code for adding&lt;br /&gt;the controls results in a lengthy program.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Screen 4.66 Screen 4.67&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;The function abc gets called. Its first parameter is an object type. The object&lt;br /&gt;stands for the control that calls the function. In function abc, the RadioButton&lt;br /&gt;that was clicked on, is represented by s. The value contained in s is stored in&lt;br /&gt;another RadioButton called rs. The function aaa is called thereafter. It&lt;br /&gt;compares the value contained in rs with the 5 radio buttons r1 to r5. Depending&lt;br /&gt;upon the value that matches, the Dock property of the button changes.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Screen 4.68 Screen 4.69&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;When a control is docked to the edge of a container, it will always take up a&lt;br /&gt;position flush against the edge, if the control is resized.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;The Left, Right, Top and Bottom styles position the control towards the&lt;br /&gt;respective edge of the container. Fill will fill upto all the sides of the&lt;br /&gt;control.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;The control is resized to fill the container's edges. The First dock style named&lt;br /&gt;None, ensures that the control does not get docked at all.&lt;/p&gt;</description><thr:total xmlns:thr="http://purl.org/syndication/thread/1.0">0</thr:total></item><item><title>C Language  Help, C Language Tutorials, C Language Programming, C Language Tricks  { The C# Language }</title><link>http://swebsolution.blogspot.com/2007/12/remoting-c.html</link><author>noreply@blogger.com (Unknown)</author><pubDate>Fri, 14 Dec 2007 22:57:00 -0800</pubDate><guid isPermaLink="false">tag:blogger.com,1999:blog-7227425366482465228.post-1230803622978208984</guid><description>&lt;p&gt;&lt;br /&gt;&lt;script type="text/javascript"&gt;&lt;!-- google_ad_client = "pub-5854532072070328"; //728x90, created 15/12/07 google_ad_slot = "5241129658"; google_ad_width = 728; google_ad_height = 90; //--&gt;&lt;/script&gt;&lt;br /&gt;&lt;script src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript"&gt;&lt;br /&gt;&lt;/script&gt;&lt;/p&gt;&lt;br /&gt;&lt;p align="justify"&gt;&lt;b&gt;C Language  Help, C Language Tutorials, C Language Programming, C Language Tricks &lt;/b&gt;&lt;br&gt;Remoting&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;We embark upon this chapter by initiating the concept of 'remoting' and attempting to unravel it&lt;br /&gt;&lt;br /&gt;by using a diminutive example. In 'remoting', there exists a 'client' program on one machine,&lt;br /&gt;&lt;br /&gt;which may be located anywhere in the world, and a 'server' program on another machine. The&lt;br /&gt;&lt;br /&gt;client program thereafter, calls a function in the server program. The server, after having&lt;br /&gt;executed the function, delivers the return value of the function, back to the&lt;br /&gt;client.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;To effect and bring the abovementioned scenario to fruition, we have employed three programs&lt;br /&gt;&lt;br /&gt;named o.cs, s.cs and c.cs. Furthermore, to avoid calling the compiler command for each one of&lt;br /&gt;&lt;br /&gt;them individually, we have placed the compile commands in a batch file named a.bat.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;o.cs&lt;br /&gt;&lt;br /&gt;using System;&lt;br /&gt;&lt;br /&gt;using System.Runtime.Remoting;&lt;br /&gt;&lt;br /&gt;using System.Runtime.Remoting.Channels;&lt;br /&gt;&lt;br /&gt;using System.Runtime.Remoting.Channels.Tcp;&lt;br /&gt;&lt;br /&gt;public class zzz : MarshalByRefObject&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;public zzz() {&lt;br /&gt;&lt;br /&gt;Console.WriteLine("Constructor");&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;~zzz() {&lt;br /&gt;&lt;br /&gt;Console.WriteLine("Destructor");&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;public String abc() {&lt;br /&gt;&lt;br /&gt;Console.WriteLine("Vijay Mukhi2");&lt;br /&gt;&lt;br /&gt;return "Sonal";&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;s.cs&lt;br /&gt;&lt;br /&gt;using System;&lt;br /&gt;&lt;br /&gt;using System.Runtime.Remoting;&lt;br /&gt;&lt;br /&gt;using System.Runtime.Remoting.Channels;&lt;br /&gt;&lt;br /&gt;using System.Runtime.Remoting.Channels.Tcp;&lt;br /&gt;&lt;br /&gt;public class sss {&lt;br /&gt;&lt;br /&gt;public static void Main()&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;TcpChannel c = new TcpChannel(8085);&lt;br /&gt;&lt;br /&gt;ChannelServices.RegisterChannel(c);&lt;br /&gt;&lt;br /&gt;RemotingConfiguration.RegisterWellKnownServiceType(&lt;br /&gt;&lt;br /&gt;Type.GetType("zzz,o"), "eee", WellKnownObjectMode.SingleCall);&lt;br /&gt;&lt;br /&gt;System.Console.WriteLine("Press &amp;lt;enter&amp;gt; to exit...");&lt;br /&gt;&lt;br /&gt;System.Console.ReadLine();&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;c.cs&lt;br /&gt;&lt;br /&gt;using System;&lt;br /&gt;&lt;br /&gt;using System.Runtime.Remoting;&lt;br /&gt;&lt;br /&gt;using System.Runtime.Remoting.Channels;&lt;br /&gt;&lt;br /&gt;using System.Runtime.Remoting.Channels.Tcp;&lt;br /&gt;&lt;br /&gt;public class ccc&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;public static void Main()&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;TcpChannel c = new TcpChannel();&lt;br /&gt;&lt;br /&gt;ChannelServices.RegisterChannel(c);&lt;br /&gt;&lt;br /&gt;zzz a = (zzz)Activator.GetObject(typeof(zzz), "tcp://localhost:8085/eee");&lt;br /&gt;&lt;br /&gt;if (a == null)&lt;br /&gt;&lt;br /&gt;System.Console.WriteLine("Could not locate server");&lt;br /&gt;&lt;br /&gt;else&lt;br /&gt;&lt;br /&gt;Console.WriteLine(a.abc());&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;a.bat&lt;br /&gt;&lt;br /&gt;csc.exe /t:library o.cs&lt;br /&gt;&lt;br /&gt;csc.exe s.cs&lt;br /&gt;&lt;br /&gt;csc.exe /r:o.dll c.cs&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Output in server dos box&lt;br /&gt;&lt;br /&gt;Press &amp;lt;enter&amp;gt; to exit...&lt;br /&gt;&lt;br /&gt;Constructor&lt;br /&gt;&lt;br /&gt;Constructor&lt;br /&gt;&lt;br /&gt;Vijay Mukhi2&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Destructor&lt;br /&gt;&lt;br /&gt;Destructor&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Output in client dos box&lt;br /&gt;&lt;br /&gt;Sonal&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;We simulate the 'remoting' behaviour by bringing two dos boxes into play. We run the client&lt;br /&gt;&lt;br /&gt;program in one box and the server program in the other box.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;The file o.cs is created with a class zzz, which derives from the class MarshalByRefObject. This&lt;br /&gt;&lt;br /&gt;abstract class, belonging to the System namespace, cannot be used in isolation. Hence, it&lt;br /&gt;&lt;br /&gt;requires a class that derives from it. A class of this kind permits remote objects to call code&lt;br /&gt;&lt;br /&gt;from it. There exist a constructor and destructor, which furnish information regarding the time&lt;br /&gt;&lt;br /&gt;of creation and destruction of an object.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;A function called abc is created, which displays 'Vijay Mukhi2' by using the WriteLine function&lt;br /&gt;&lt;br /&gt;and then, returns the name of 'Sonal'. Other programs can call the function abc, merely by&lt;br /&gt;&lt;br /&gt;deriving a class from MarshalByRefObject. The file o.cs is compiled into a dll, using the&lt;br /&gt;&lt;br /&gt;/target:library option. Understanding the abovementioned explanation does not require the genius&lt;br /&gt;&lt;br /&gt;of a rocket scientist.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;We shall now go ahead and write a simple server program named s.cs.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;c is an object, which is an instance of class TcpChannel, in the namespace&lt;br /&gt;&lt;br /&gt;System.Runtime.Remoting.Channels.Tcp. The constructor is assigned a port number, on which, the&lt;br /&gt;&lt;br /&gt;other machine can establish communication with it. The protocol used here is known as TCP/IP,&lt;br /&gt;&lt;br /&gt;where TCP stands for Transmission Control Protocol and IP implies Internet Protocol.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;The port number facilitates the creation of a channel between two machines. The number that we&lt;br /&gt;&lt;br /&gt;have selected for the port is 8085. This is because all the samples provided by Microsoft use&lt;br /&gt;&lt;br /&gt;this number. It must obviously be a lucky mascot for that Company. You may select any other&lt;br /&gt;&lt;br /&gt;number, as long the same number is used for the client and the server. Hereinafter, whenever a&lt;br /&gt;&lt;br /&gt;client connects on port 8085, the TCP/IP stack or the Windows Internet Software keeps the server&lt;br /&gt;&lt;br /&gt;apprised about the connection. The server, in turn, keeps a listening vigil on port 8085.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;The namespace, System.Runtime.Remoting contains a class ChannelServices, which has a static&lt;br /&gt;&lt;br /&gt;function RegisterChannel. This function accepts an IChannel interface as a parameter and&lt;br /&gt;&lt;br /&gt;thereafter, registers the Channel object with the channel services. The main activities of this&lt;br /&gt;&lt;br /&gt;function are not known. It suffices to say that, without this function, the TCPChannel object&lt;br /&gt;&lt;br /&gt;that has been created, would not be effective. The class TCPChannel derives from a large number&lt;br /&gt;&lt;br /&gt;of interfaces, such as IChannelSender, IChannel, IChannelReceiver, IDictionary, ICollection and&lt;br /&gt;&lt;br /&gt;IEnumerable.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;The most imperative function that the server has to call is, the one that registers the function&lt;br /&gt;&lt;br /&gt;abc in class zzz, contained in the file o.dll. The name of this static function is&lt;br /&gt;RegisterWellKnownServiceType, of the class RemotingConfiguration, belonging to&lt;br /&gt;the namespace&lt;br /&gt;&lt;br /&gt;System.Runtime.Remoting.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;This function takes three parameters, beginning with a type object that represents the name of&lt;br /&gt;&lt;br /&gt;the assembly that contains the object to be registered. Our object is located in a dll. It could&lt;br /&gt;&lt;br /&gt;even be located in an executable. The name 'o' of the dll file is passed as the second parameter&lt;br /&gt;&lt;br /&gt;to the GetType Object. The first parameter to this function is the name of the class that&lt;br /&gt;&lt;br /&gt;represents the object. In case the class is located in a namespace, the full name has to be&lt;br /&gt;&lt;br /&gt;furnished. The GetType object returns a Type object.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;The second parameter of the function RegisterWellKnownServiceType, represents a URI, or a&lt;br /&gt;&lt;br /&gt;Universal Resource Identifier. It could be any original word that helps in identification of&lt;br /&gt;&lt;br /&gt;functions in the class zzz. This is referred to as an 'endpoint', where the object shall be&lt;br /&gt;&lt;br /&gt;published. The client does not connect to the class zzz directly, but to an endpoint. In our&lt;br /&gt;&lt;br /&gt;case, the URI is eee. It points indirectly to the class zzz, which is the object that needs to&lt;br /&gt;&lt;br /&gt;be remoted. Any string may be used, provided the same string is used by the client too. Had we&lt;br /&gt;&lt;br /&gt;wished to connect using ASP.Net, we would have been compelled to use zzz.soap, as the endpoint.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;The last parameter specifies the mode, which may assume any of the two values,i.e. SingleCall&lt;br /&gt;&lt;br /&gt;or Singleton. The data type is an enum named WellKnownObjectMode, which only embodies the two&lt;br /&gt;&lt;br /&gt;abovementioned values. The mode specifies the 'lifetime' of the object, i.e. the frequency of&lt;br /&gt;&lt;br /&gt;its creation. If we specify the mode as SingleCall, a new instance of the object zzz will be&lt;br /&gt;&lt;br /&gt;created each time a message is received. If it is assigned the value of Singleton, an instance&lt;br /&gt;&lt;br /&gt;of zzz will be created only once, and all the clients shall interact with it.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;On compiling s.cs, an executable named s.exe will be generated.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;The client code, which calls code from the remote object is placed in the file c.cs. In this&lt;br /&gt;&lt;br /&gt;file, we commence with the 'using' statement, in order to introduce the namespaces for the&lt;br /&gt;&lt;br /&gt;classes. Thereafter, a TcpChannel is created, but unlike a server, it is devoid of a port&lt;br /&gt;&lt;br /&gt;number. The port number assigned to the client is of no significance, since it is not binding on&lt;br /&gt;&lt;br /&gt;the client to listen to any request. Therefore, a random value is assigned as the port number.&lt;br /&gt;&lt;br /&gt;RegisterChannel registers the TcpChannel c.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Next, we use the static function GetObject from the Activator class in the system namespace.&lt;br /&gt;&lt;br /&gt;This function accepts two parameters:&lt;br /&gt;&lt;br /&gt;• The first parameter is of data type Type, which represents the class that we wish to&lt;br /&gt;&lt;br /&gt;instantiate.&lt;br /&gt;&lt;br /&gt;• The second parameter is the URI of the object, which is to be instantiated.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;The URI follows a specific sequence, which is as follows:&lt;br /&gt;&lt;br /&gt;• It commences with the protocol that is required for communication. In our case, it is&lt;br /&gt;&lt;br /&gt;tcp.&lt;br /&gt;&lt;br /&gt;• This is followed by the machine name on which the server resides.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;In our case, everything is located on a single machine. Hence, we use the machine name&lt;br /&gt;&lt;br /&gt;'localhost'. The machine name is by suffixed with a colon.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;• This is succeeded by the port number.&lt;br /&gt;&lt;br /&gt;• Finally, the endpoint created in the server is to be specified. In this case, it is eee.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;The abovementioned sequence has to be maintained, and all the above items are mandatory.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;This function returns an object that is an instance of class zzz, which we store in object 'a'.&lt;br /&gt;&lt;br /&gt;If the object 'a' has a value of null, it generates an error and displays a suitable error&lt;br /&gt;&lt;br /&gt;message. Otherwise, the function abc of the object 'a' is called, and the return value is&lt;br /&gt;&lt;br /&gt;displayed using the WriteLine function.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;On compiling c.cs, an error is generated. So, we provide a reference to the dll named o.dll,&lt;br /&gt;&lt;br /&gt;since the class zzz is present in it.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;In one of the dos boxes, we first run the server 's'. On the surface of it, it appears as though&lt;br /&gt;&lt;br /&gt;only the string "Press &amp;lt;enter&amp;gt; to exit" has been displayed. But in the background, the server&lt;br /&gt;&lt;br /&gt;has been registered as a sink for all those functions that need to call the function abc, from&lt;br /&gt;its endpoint eee.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;If we merely press the Enter key, the ReadLine function, whose raison d'être is to prevent our&lt;br /&gt;&lt;br /&gt;server from quitting out, actually exits.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;When we run the client in a separate dos box, the server dos box displays 'Constructor' twice.&lt;br /&gt;&lt;br /&gt;This establishes the fact that the server has located the class zzz in the assembly o.dll and&lt;br /&gt;&lt;br /&gt;instantiated it. The server instantiates the object that is to be remoted, to enable the&lt;br /&gt;&lt;br /&gt;framework to read the metadata present in the assembly. This is part of its registration&lt;br /&gt;&lt;br /&gt;process.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Thereafter, it displays the string 'VijayMukhi 2' in the server dos box, since the client has&lt;br /&gt;&lt;br /&gt;called the function abc. The client dos box displays 'Sonal', which is the return value of the&lt;br /&gt;&lt;br /&gt;function.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;The client program quits out gracefully, whereas, the server cools its heels, waiting for other&lt;br /&gt;&lt;br /&gt;calls. The server destroys the current object, and the framework keeps a listening vigil for&lt;br /&gt;&lt;br /&gt;other clients, which may be eager to connect on previously registered channels. When the Enter&lt;br /&gt;&lt;br /&gt;key is pressed, it results in the termination of the server program. Thus, the destructor gets&lt;br /&gt;&lt;br /&gt;called. We are, however, not certain whether an object perishes or survives when a program quits&lt;br /&gt;&lt;br /&gt;out. In any case, as far as the C# language is concerned, this is not in our control.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;The Constructor gets called twice:&lt;br /&gt;&lt;br /&gt;• On the first occasion, the framework reads the metadata .&lt;br /&gt;&lt;br /&gt;• On the second occurrence, the client interacts with an instance of the object zzz.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;The Destructor also gets called twice, since the framework brazenly destroys the two zzz objects&lt;br /&gt;&lt;br /&gt;which had been created.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;The client has to initially locate the server, and thereafter, connect to it, so that the zzz&lt;br /&gt;&lt;br /&gt;object gets instantiated. The client requires a function to execute this task and to return an&lt;br /&gt;&lt;br /&gt;object, which is an instance of zzz. However, the actual object dwells on another computer,&lt;br /&gt;&lt;br /&gt;which could be ensconced at a geographically dispersed location. Thereafter, the function&lt;br /&gt;&lt;br /&gt;GetObject returns a simulation of the zzz object, which it instantiates. For all practical&lt;br /&gt;&lt;br /&gt;purposes, it is under the delusion that it is the real zzz object, though in reality, the object&lt;br /&gt;&lt;br /&gt;has been indirectly created on another machine.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Thus, GetObject returns a 'proxy' for the remote object. The literal meaning of the word 'proxy'&lt;br /&gt;&lt;br /&gt;is 'a surrogate of the original'. This proxy then directs the call to the original object&lt;br /&gt;&lt;br /&gt;residing on an alternative machine. The return value of the GetObject function is indicative of&lt;br /&gt;&lt;br /&gt;whether the object was successfully created on the remote machine or not. A null value indicates&lt;br /&gt;&lt;br /&gt;an error. The function abc is called from the remote zzz object, after the object has been&lt;br /&gt;&lt;br /&gt;successfully instantiated. However, a.abc() actually interacts with the proxy object, which in&lt;br /&gt;&lt;br /&gt;turn, forwards the call to the remote machine. The remotely located program executes the&lt;br /&gt;&lt;br /&gt;function abc, and thereafter, dispatches the return value over to the client.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Two other programs named 'proxy' and 'stub' are also brought into play, in order to accomplish&lt;br /&gt;&lt;br /&gt;the task of 'remoting'.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Let us now analyze this process from a fresh perspective.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;At the outset, we need to create two sub-directories, r2 and r3.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;c:\csharp&amp;gt;md r2&lt;br /&gt;&lt;br /&gt;c:\csharp&amp;gt;md r3&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Next, we are required to copy the server file s.cs and o.cs to the sub-directory&lt;br /&gt;r2, and the&lt;br /&gt;&lt;br /&gt;client file c.cs to the sub-directory r3.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;C:\csharp&amp;gt;copy s.cs r2&lt;br /&gt;&lt;br /&gt;C:\csharp&amp;gt;copy o.cs r2&lt;br /&gt;&lt;br /&gt;C:\csharp&amp;gt;copy c.cs r3&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;C:\csharp\r2&amp;gt; csc s.cs&lt;br /&gt;&lt;br /&gt;C:\csharp\r2&amp;gt; csc /t:library o.cs&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;C:\csharp\r3&amp;gt; csc c.cs&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;c.cs(11,7): error CS0246: The type or namespace name 'zzz' could not be found (are you missing a&lt;br /&gt;&lt;br /&gt;using directive or an assembly reference?)&lt;br /&gt;&lt;br /&gt;c.cs(12,11): error CS0103: The name 'a' does not exist in the class or namespace 'ccc'&lt;br /&gt;&lt;br /&gt;c.cs(15,19): error CS0246: The type or namespace name 'a' could not be found (are you missing a&lt;br /&gt;&lt;br /&gt;using directive or an assembly reference?)&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;On compiling the two files placed in the sub-directory r2, no errors are generated. However, on&lt;br /&gt;&lt;br /&gt;compiling the client program in the sub-directory r3, an error is generated.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;We hit a roadblock at this juncture, since the file c.cs requires the assembly file in which,&lt;br /&gt;&lt;br /&gt;the remoted object, o.dll resides. You may argue that the prerequisite of placing the dll in the&lt;br /&gt;&lt;br /&gt;client's sub-directory defeats the very purpose of 'remoting'. However, the presence of the file&lt;br /&gt;&lt;br /&gt;is obligatory only for the metadata of the class zzz. The motivation for doing this will be&lt;br /&gt;elucidated shortly.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;C:\csharp\r3&amp;gt; copy c:\csharp\r2\o.dll&lt;br /&gt;&lt;br /&gt;C:\csharp\r3&amp;gt; csc c.cs /r:o.dll&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Therefore, we copy the assembly file o.dll from the sub-directory r2, into the sub-directory r3,&lt;br /&gt;&lt;br /&gt;and thereafter, recompile it. Now, we add a tangy dash of lime to the mundane and bland routine&lt;br /&gt;&lt;br /&gt;given above, to give it a slight twist. The string VijayMukhi2 is modified to VijayMukhi3. The&lt;br /&gt;&lt;br /&gt;library is recompiled to produce a dll file in the sub-directory r2.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;C:\csharp\r2&amp;gt;edit o.cs&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;public String abc()&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;Console.WriteLine("Vijay Mukhi3");&lt;br /&gt;&lt;br /&gt;return "Sonal";&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;C:\csharp\r2&amp;gt;csc /t:library o.cs&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;As a consequence, the copies of the function abc, which reside in the two subdirectories r2 and&lt;br /&gt;&lt;br /&gt;r3, become dissimilar.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Output in server dos box&lt;br /&gt;&lt;br /&gt;Press &amp;lt;enter&amp;gt; to exit...&lt;br /&gt;&lt;br /&gt;Constructor&lt;br /&gt;&lt;br /&gt;Constructor&lt;br /&gt;&lt;br /&gt;Vijay Mukhi3&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Destructor&lt;br /&gt;&lt;br /&gt;Destructor&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Output in client dos box&lt;br /&gt;&lt;br /&gt;Sonal&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;The server program and the client program are run in their respective directories. On doing so,&lt;br /&gt;&lt;br /&gt;it becomes evident that, even though the client had a copy of the dll in its own sub-directory,&lt;br /&gt;&lt;br /&gt;it still called the function abc from o.dll, which was resident in the sub-directory of the&lt;br /&gt;&lt;br /&gt;server.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Therefore, the output displayed is 'Vijay Mukhi3', and not 'Vijay Mukhi2'.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;In real life, the code placed in the directories r2 and r3 would actually be dwelling within&lt;br /&gt;&lt;br /&gt;geographically dispersed machines. The metadata of the object should be provided to the client,&lt;br /&gt;&lt;br /&gt;for the sole purpose of compilation.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;o.cs&lt;br /&gt;&lt;br /&gt;using System;&lt;br /&gt;&lt;br /&gt;namespace nnn&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;public class zzz : MarshalByRefObject&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;public zzz()&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;Console.WriteLine("Constructor");&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;~zzz()&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;Console.WriteLine("Destructor");&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;public String abc()&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;Console.WriteLine("Vijay Mukhi2");&lt;br /&gt;&lt;br /&gt;return "Sonal " ;&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;public String pqr(string s)&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;Console.WriteLine("Sonal Mukhi2");&lt;br /&gt;&lt;br /&gt;return "VMCI " + s ;&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;s.cs&lt;br /&gt;&lt;br /&gt;using System;&lt;br /&gt;&lt;br /&gt;using System.Runtime.Remoting;&lt;br /&gt;&lt;br /&gt;using System.Runtime.Remoting.Channels;&lt;br /&gt;&lt;br /&gt;using System.Runtime.Remoting.Channels.Tcp;&lt;br /&gt;&lt;br /&gt;public class sss&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;public static void Main()&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;TcpChannel c = new TcpChannel(8085);&lt;br /&gt;&lt;br /&gt;ChannelServices.RegisterChannel(c);&lt;br /&gt;&lt;br /&gt;RemotingConfiguration.RegisterWellKnownServiceType(Type.GetType(&lt;br /&gt;&lt;br /&gt;"nnn.zzz,o"), "eee", WellKnownObjectMode.SingleCall);&lt;br /&gt;&lt;br /&gt;System.Console.WriteLine("Press &amp;lt;enter&amp;gt; to exit...");&lt;br /&gt;&lt;br /&gt;System.Console.ReadLine();&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;c.cs&lt;br /&gt;&lt;br /&gt;using System;&lt;br /&gt;&lt;br /&gt;using System.Runtime.Remoting;&lt;br /&gt;&lt;br /&gt;using System.Runtime.Remoting.Channels;&lt;br /&gt;&lt;br /&gt;using System.Runtime.Remoting.Channels.Tcp;&lt;br /&gt;&lt;br /&gt;using nnn;&lt;br /&gt;&lt;br /&gt;public class ccc&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;public static void Main()&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;TcpChannel c = new TcpChannel();&lt;br /&gt;&lt;br /&gt;ChannelServices.RegisterChannel(c);&lt;br /&gt;&lt;br /&gt;zzz a = (zzz)Activator.GetObject(typeof(zzz), "tcp://localhost:8085/eee");&lt;br /&gt;&lt;br /&gt;if (a == null)&lt;br /&gt;&lt;br /&gt;System.Console.WriteLine("Could not locate server");&lt;br /&gt;&lt;br /&gt;else&lt;br /&gt;&lt;br /&gt;Console.WriteLine(a.abc());&lt;br /&gt;&lt;br /&gt;Console.WriteLine(a.pqr("hell"));&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Output Client&lt;br /&gt;&lt;br /&gt;Sonal&lt;br /&gt;&lt;br /&gt;VMCI hell&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Output in server dos box&lt;br /&gt;&lt;br /&gt;Press &amp;lt;enter&amp;gt; to exit...&lt;br /&gt;&lt;br /&gt;Constructor&lt;br /&gt;&lt;br /&gt;Constructor&lt;br /&gt;&lt;br /&gt;Vijay Mukhi2&lt;br /&gt;&lt;br /&gt;Constructor&lt;br /&gt;&lt;br /&gt;Sonal Mukhi2&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Destructor&lt;br /&gt;&lt;br /&gt;Destructor&lt;br /&gt;&lt;br /&gt;Destructor&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;We shall now initiate a few modifications in the three files, o.cs, s.cs and o.cs.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;o.cs : Microsoft insists that all classes must reside in a namespace. Therefore, class zzz has&lt;br /&gt;been made part of the nnn namespace. A function named pqr has been introduced,&lt;br /&gt;which accepts a&lt;br /&gt;&lt;br /&gt;string parameter and returns the same string. The return value is prefixed with the acronym&lt;br /&gt;&lt;br /&gt;'VMCI', which stands for my institute, i.e. Vijay Mukhi's Computer Institute.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;s.cs : The server has the name of the class nnn.zzz located within the function&lt;br /&gt;&lt;br /&gt;RegisterWellKnownServiceType. It has no regard for the functions belonging to the class zzz,&lt;br /&gt;&lt;br /&gt;since its primary focus is on the endpoint that represents the class.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;c.cs : Internally within the client, the class has been renamed as nnn.zzz. Since we abhor the&lt;br /&gt;&lt;br /&gt;process of writing the same ungainly names repeatedly, we use the keyword 'using' with nnn. As a&lt;br /&gt;&lt;br /&gt;result of this, every occurrence of zzz is replaced by nnn.zzz. So, the typeof keyword&lt;br /&gt;&lt;br /&gt;encounters the class name nnn.zzz.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;The function pqr is called with a single parameter. It is imperative to have the metadata&lt;br /&gt;&lt;br /&gt;available at this stage, since it contains the following:&lt;br /&gt;&lt;br /&gt;• The name of the class&lt;br /&gt;&lt;br /&gt;• All the functions that the class carries&lt;br /&gt;&lt;br /&gt;• The signatures of the functions.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;This is a form of code validation, which ensures that inappropriate signatures are not used&lt;br /&gt;&lt;br /&gt;while the functions in the client are being called.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;The output generated by these two programs is very predictable. There is just a slight departure&lt;br /&gt;&lt;br /&gt;from the expected output, in the case of the server window. Whenever a function from the remote&lt;br /&gt;&lt;br /&gt;server is to be executed, the constructor in the class is called. However, the output of the&lt;br /&gt;&lt;br /&gt;destructors is highly unpredictable.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Therefore, it is possible for us to pass parameters to functions, even if the function is being&lt;br /&gt;&lt;br /&gt;remoted. Its demeanor is very similar to that of calling the function off the same machine.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Interfaces&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;sh.cs&lt;br /&gt;&lt;br /&gt;using System;&lt;br /&gt;&lt;br /&gt;namespace nnn&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;public interface iii&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;String pqr(String s);&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;o.cs&lt;br /&gt;&lt;br /&gt;using System;&lt;br /&gt;&lt;br /&gt;namespace nnn&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;public class zzz : MarshalByRefObject, iii&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;public zzz()&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;Console.WriteLine("Constructor");&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;public String pqr(string s)&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;Console.WriteLine("Sonal Mukhi2");&lt;br /&gt;&lt;br /&gt;return "VMCI " + s ;&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;s.cs&lt;br /&gt;&lt;br /&gt;using System;&lt;br /&gt;&lt;br /&gt;using System.Runtime.Remoting;&lt;br /&gt;&lt;br /&gt;using System.Runtime.Remoting.Channels;&lt;br /&gt;&lt;br /&gt;using System.Runtime.Remoting.Channels.Tcp;&lt;br /&gt;&lt;br /&gt;public class sss&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;public static void Main()&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;TcpChannel c = new TcpChannel(8085);&lt;br /&gt;&lt;br /&gt;ChannelServices.RegisterChannel(c);&lt;br /&gt;&lt;br /&gt;RemotingConfiguration.RegisterWellKnownServiceType(Type.GetType(&lt;br /&gt;&lt;br /&gt;"nnn.zzz,o"), "eee", WellKnownObjectMode.SingleCall);&lt;br /&gt;&lt;br /&gt;System.Console.WriteLine("Press &amp;lt;enter&amp;gt; to exit...");&lt;br /&gt;&lt;br /&gt;System.Console.ReadLine();&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;c.cs&lt;br /&gt;&lt;br /&gt;using System;&lt;br /&gt;&lt;br /&gt;using System.Runtime.Remoting;&lt;br /&gt;&lt;br /&gt;using System.Runtime.Remoting.Channels;&lt;br /&gt;&lt;br /&gt;using System.Runtime.Remoting.Channels.Tcp;&lt;br /&gt;&lt;br /&gt;using nnn;&lt;br /&gt;&lt;br /&gt;public class ccc&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;public static void Main()&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;TcpChannel c = new TcpChannel();&lt;br /&gt;&lt;br /&gt;ChannelServices.RegisterChannel(c);&lt;br /&gt;&lt;br /&gt;iii a=(iii)Activator.GetObject(typeof(nnn.iii),"tcp://localhost:8085/eee");&lt;br /&gt;&lt;br /&gt;if (a == null)&lt;br /&gt;&lt;br /&gt;System.Console.WriteLine("Could not locate server");&lt;br /&gt;&lt;br /&gt;else&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;Console.WriteLine(a.pqr("hell"));&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;a.bat&lt;br /&gt;&lt;br /&gt;del *.exe&lt;br /&gt;&lt;br /&gt;del *.dll&lt;br /&gt;&lt;br /&gt;csc.exe /t:library sh.cs&lt;br /&gt;&lt;br /&gt;csc.exe /t:library /r:sh.dll o.cs&lt;br /&gt;&lt;br /&gt;csc.exe s.cs&lt;br /&gt;&lt;br /&gt;csc.exe /r:sh.dll c.cs&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Output Server&lt;br /&gt;&lt;br /&gt;Press &amp;lt;enter&amp;gt; to exit...&lt;br /&gt;&lt;br /&gt;Constructor&lt;br /&gt;&lt;br /&gt;Constructor&lt;br /&gt;&lt;br /&gt;Sonal Mukhi2&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Output Client&lt;br /&gt;&lt;br /&gt;VMCI hell&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Interfaces are employed to overcome the requirement of placing the entire code of a class in an&lt;br /&gt;&lt;br /&gt;assembly, which is resident on the client machine.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;In the file sh.cs, a simple interface called iii is created in the namespace nnn, possessing a&lt;br /&gt;&lt;br /&gt;single function pqr. It is imperative for all classes that derive from this interface, to&lt;br /&gt;&lt;br /&gt;incorporate this function. The interface iii is compiled to a dll, and it does not require the&lt;br /&gt;&lt;br /&gt;/r: switch, since it does not refer to any external modules. The file carries only the metadata,&lt;br /&gt;&lt;br /&gt;and does not contain any code.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;In the object o.cs, class zzz is derived from both, the class MarshalByRefObject and the&lt;br /&gt;&lt;br /&gt;interface iii. Thus, it is essential to implement the function pqr in the class zzz. While&lt;br /&gt;&lt;br /&gt;compiling the file o.cs, a reference must be provided to the file sh.dll, which contains the&lt;br /&gt;&lt;br /&gt;metadata for the interface iii.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;The server does not get affected in the least by any modifications carried out in the object.&lt;br /&gt;&lt;br /&gt;This is by virtue of the framework, which registers a class zzz and not the interface iii. There&lt;br /&gt;&lt;br /&gt;is no necessity for the server to refer to any of the assemblies of o.dll and sh.dll.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;In the client program, the GetObject function instantiates an object of type iii, and not from&lt;br /&gt;&lt;br /&gt;the class zzz. Whether the code is called from an interface or a class, is of no significance.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;The prime benefit of using an interface is that the C# compiler does not need to introduce the&lt;br /&gt;&lt;br /&gt;assembly o.dll, which contains a sizeable amount of code. The assembly file sh.dll is adequate,&lt;br /&gt;&lt;br /&gt;since it provides the metadata of the interface. For example, Jack can create the interface iii&lt;br /&gt;&lt;br /&gt;on a separate machine and then, send across the file sh.dll to his client Jil. She can, in turn,&lt;br /&gt;&lt;br /&gt;use it on her machine, thereby meeting the requirements of the complier.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;The endpoint represents a zzz class, which in turn, represents an interface named iii, as well&lt;br /&gt;&lt;br /&gt;as, an object called MarshalByRefObject. A request for an iii object in the client does not&lt;br /&gt;&lt;br /&gt;generate any error, since the endpoint represents a zzz instance, which encompasses an iii&lt;br /&gt;&lt;br /&gt;instance.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;We use the file a.bat to compile all the above four files with a single stroke. This program&lt;br /&gt;&lt;br /&gt;evinces how we can separate the definition from the implementation.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;For the next program, the file s.cs remains unchanged.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;o.cs&lt;br /&gt;&lt;br /&gt;using System;&lt;br /&gt;&lt;br /&gt;namespace nnn&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;public class zzz : MarshalByRefObject&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;public zzz()&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;Console.WriteLine("Constructor");&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;public String pqr(string s)&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;Console.WriteLine("Sonal Mukhi2");&lt;br /&gt;&lt;br /&gt;return "VMCI " + s ;&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;c.cs&lt;br /&gt;&lt;br /&gt;using System;&lt;br /&gt;&lt;br /&gt;using System.Threading;&lt;br /&gt;&lt;br /&gt;using System.Runtime.Remoting;&lt;br /&gt;&lt;br /&gt;using System.Runtime.Remoting.Messaging;&lt;br /&gt;&lt;br /&gt;using System.Runtime.Remoting.Channels;&lt;br /&gt;&lt;br /&gt;using System.Runtime.Remoting.Channels.Tcp;&lt;br /&gt;&lt;br /&gt;namespace nnn&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;public class ccc&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;public static ManualResetEvent e;&lt;br /&gt;&lt;br /&gt;public delegate String ddd(String s);&lt;br /&gt;&lt;br /&gt;public static void Main(string [] args)&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;e = new ManualResetEvent(false);&lt;br /&gt;&lt;br /&gt;TcpChannel c = new TcpChannel();&lt;br /&gt;&lt;br /&gt;ChannelServices.RegisterChannel(c);&lt;br /&gt;&lt;br /&gt;zzz o = (zzz)Activator.GetObject(typeof(zzz), "tcp://localhost:8085/eee");&lt;br /&gt;&lt;br /&gt;if (o == null)&lt;br /&gt;&lt;br /&gt;System.Console.WriteLine("Could not locate server");&lt;br /&gt;&lt;br /&gt;else&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;AsyncCallback cb = new AsyncCallback(ccc.abc);&lt;br /&gt;&lt;br /&gt;ddd d = new ddd(o.pqr);&lt;br /&gt;&lt;br /&gt;IAsyncResult ar = d.BeginInvoke("Vijay", cb, null);&lt;br /&gt;&lt;br /&gt;Console.WriteLine(ar.IsCompleted);&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;e.WaitOne();&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;public static void abc(IAsyncResult ar)&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;ddd d = (ddd)((AsyncResult)ar).AsyncDelegate;&lt;br /&gt;&lt;br /&gt;Console.WriteLine(d.EndInvoke(ar));&lt;br /&gt;&lt;br /&gt;Console.WriteLine(ar.IsCompleted);&lt;br /&gt;&lt;br /&gt;e.Set();&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Output Client&lt;br /&gt;&lt;br /&gt;False&lt;br /&gt;&lt;br /&gt;VMCI Vijay&lt;br /&gt;&lt;br /&gt;True&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Output Server&lt;br /&gt;&lt;br /&gt;Press &amp;lt;enter&amp;gt; to exit...&lt;br /&gt;&lt;br /&gt;Constructor&lt;br /&gt;&lt;br /&gt;Constructor&lt;br /&gt;&lt;br /&gt;Sonal Mukhi2&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;In one of the earlier examples, we had called the pqr function from our client. At this point, a&lt;br /&gt;&lt;br /&gt;network message was transmitted to the server, which executed the function abc and dispatched&lt;br /&gt;&lt;br /&gt;the outcome back to the client. There is a likelihood that the function executed in the server,&lt;br /&gt;&lt;br /&gt;could have taken hours to complete execution. So, the client would have had to wait eternally&lt;br /&gt;&lt;br /&gt;for the server. Therefore, this method is prodigiously wasteful in terms of time and resources.&lt;br /&gt;&lt;br /&gt;Thus, whenever a 'synchronous call' is made to the remote object, the client has to cease all&lt;br /&gt;&lt;br /&gt;activities, till it receives a response from the server.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;It would prove to be a lot more efficacious if the client could be allowed to continue doing its&lt;br /&gt;&lt;br /&gt;job, while the server is busy executing the function. On completion of the task, the server&lt;br /&gt;&lt;br /&gt;could notify the client. In other words, we do not want the client to 'wait' or 'block'. This&lt;br /&gt;&lt;br /&gt;mechanism can be implemented by employing an 'asynchronous call' to the function. In such cases,&lt;br /&gt;&lt;br /&gt;the call is made by the client. Therefore, only the code in the client needs modification.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;In the chapter on Threads, we had learnt about the ManualResetEvent event object. We had set it&lt;br /&gt;&lt;br /&gt;to a value of False. In such situations, the client will wait on the WaitOne function, till the&lt;br /&gt;&lt;br /&gt;Set function is called.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;A delegate is a type safe way of calling a method in an oblique manner. We have created a&lt;br /&gt;&lt;br /&gt;delegate ddd that accepts a string as a parameter, and it returns a string. The framework can&lt;br /&gt;&lt;br /&gt;execute a function in an asynchronous mode provided, we represent it as a delegate. Here, the&lt;br /&gt;&lt;br /&gt;callback function has been represented by the use of a delegate. For those who have earned their&lt;br /&gt;&lt;br /&gt;spurs on C/C++, it would be a revelation that delegates are actually 'pointers to functions'.&lt;br /&gt;&lt;br /&gt;The function pqr, which has been called in an asynchronous mode, accepts as well as returns a&lt;br /&gt;&lt;br /&gt;string. Therefore, it is amply evident that the delegate uses the same parameters and return&lt;br /&gt;&lt;br /&gt;types.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;When we were writing a book on Intermediate Language, IL (an assembler language to which all&lt;br /&gt;&lt;br /&gt;code in the .NET world gets converted), we had discovered that a delegate finally gets converted&lt;br /&gt;&lt;br /&gt;into a class. In our case, the delegate ddd contains a large number of functions, which get&lt;br /&gt;&lt;br /&gt;added to it. Two such functions are, BeginInvoke and EndInvoke. The main role of these functions&lt;br /&gt;&lt;br /&gt;is to call code that is written by the system or runtime. Hence, they are called native&lt;br /&gt;&lt;br /&gt;functions.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Thereafter, an object cb, which is an instance of a delegate AsyncCallback, is created. The&lt;br /&gt;&lt;br /&gt;constructor of this class is passed a function abc, which is notified as soon as the remote&lt;br /&gt;&lt;br /&gt;function completes execution. The callback function abc is passed an IAsyncResult parameter,&lt;br /&gt;&lt;br /&gt;since this function matches the declaration of the delegate.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Next, we create a delegate d of type ddd, whose constructor is assigned the function name pqr,&lt;br /&gt;&lt;br /&gt;which is to be executed in an asynchronous manner. The BeginInvoke function is called next. It&lt;br /&gt;&lt;br /&gt;is passed the parameter string, which is to be transmitted to the remote function named pqr. The&lt;br /&gt;&lt;br /&gt;second parameter to this function is another function called cb, which also requires to be&lt;br /&gt;&lt;br /&gt;notified after all the action has been wrapped up. This delegate allusively represents the&lt;br /&gt;&lt;br /&gt;function abc in the class ccc.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;The BeginInvoke function returns an IAsyncResult, which lies inert at this stage. The parameters&lt;br /&gt;&lt;br /&gt;passed to it are similar to the ones provided to our event handling function abc.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;The remote function may take excessive time to execute. Therefore, to avoid wasting time, the&lt;br /&gt;&lt;br /&gt;client continues with its work. Once the call is completed, the framework ensures that the&lt;br /&gt;&lt;br /&gt;function abc is called with the parameter representing the return value. To figure out the&lt;br /&gt;&lt;br /&gt;return value, we simply cast the parameter to a ddd object, and call the function EndInvoke off&lt;br /&gt;&lt;br /&gt;it. This return value is displayed in the client dos box.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;For Asynchronous calls, we create two delegates, one for the callback function, and the other&lt;br /&gt;&lt;br /&gt;for the remote method. Function BeginInvoke calls the remote function, which in turn, calls the&lt;br /&gt;&lt;br /&gt;function abc. When the call ceases, EndInvoke is called to receive the return values.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;The IAsyncResult consists of five properties, one of which is IsCompleted. Initially, this&lt;br /&gt;&lt;br /&gt;property returns False, since the asynchronous call has not been completed. However, in the&lt;br /&gt;&lt;br /&gt;function abc, it returns True, since the remote call has run its course and is done with. The&lt;br /&gt;&lt;br /&gt;EndInvoke function plays no role in ending the remote call. If we display the value contained in&lt;br /&gt;&lt;br /&gt;the IsCompleted property, prior to the EndInvoke function, a value of True would get displayed.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;sh.cs&lt;br /&gt;&lt;br /&gt;using System;&lt;br /&gt;&lt;br /&gt;namespace nnn&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;public class fff : MarshalByRefObject&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;public void xyz(String t)&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;Console.WriteLine(t);&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&amp;gt;csc /t:library sh.cs&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;o.cs&lt;br /&gt;&lt;br /&gt;using System;&lt;br /&gt;&lt;br /&gt;using System.Runtime.Remoting;&lt;br /&gt;&lt;br /&gt;namespace nnn&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;public class zzz : MarshalByRefObject&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;public zzz()&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;Console.WriteLine("Constructor");&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;public String pqr(String s,fff f)&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;f.xyz("Hi");&lt;br /&gt;&lt;br /&gt;Console.WriteLine("pqr called");&lt;br /&gt;&lt;br /&gt;return "VMCI " + s;&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&amp;gt;csc /t:library o.cs /r:sh.dll&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;s.cs&lt;br /&gt;&lt;br /&gt;using System;&lt;br /&gt;&lt;br /&gt;using System.Runtime.Remoting;&lt;br /&gt;&lt;br /&gt;using System.Runtime.Remoting.Channels;&lt;br /&gt;&lt;br /&gt;using System.Runtime.Remoting.Channels.Tcp;&lt;br /&gt;&lt;br /&gt;public class sss&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;public static void Main()&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;TcpChannel c = new TcpChannel(8085);&lt;br /&gt;&lt;br /&gt;ChannelServices.RegisterChannel(c);&lt;br /&gt;&lt;br /&gt;RemotingConfiguration.RegisterWellKnownServiceType(Type.GetType(&lt;br /&gt;&lt;br /&gt;"nnn.zzz,o"), "eee", WellKnownObjectMode.SingleCall);&lt;br /&gt;&lt;br /&gt;System.Console.WriteLine("Press &amp;lt;enter&amp;gt; to exit...");&lt;br /&gt;&lt;br /&gt;System.Console.ReadLine();&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&amp;gt;csc s.cs&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;c.cs&lt;br /&gt;&lt;br /&gt;using System;&lt;br /&gt;&lt;br /&gt;using System.Runtime.Remoting;&lt;br /&gt;&lt;br /&gt;using System.Runtime.Remoting.Channels;&lt;br /&gt;&lt;br /&gt;using System.Runtime.Remoting.Channels.Tcp;&lt;br /&gt;&lt;br /&gt;namespace nnn&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;public class ccc&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;public static void Main(string [] args) {&lt;br /&gt;&lt;br /&gt;TcpChannel c = new TcpChannel(8086);&lt;br /&gt;&lt;br /&gt;ChannelServices.RegisterChannel(c);&lt;br /&gt;&lt;br /&gt;fff f = new fff();&lt;br /&gt;&lt;br /&gt;zzz o = (zzz)Activator.GetObject(typeof(nnn.zzz),"tcp://localhost:8085/eee");&lt;br /&gt;&lt;br /&gt;if (o == null)&lt;br /&gt;&lt;br /&gt;System.Console.WriteLine("Could not locate server");&lt;br /&gt;&lt;br /&gt;else&lt;br /&gt;&lt;br /&gt;Console.WriteLine(o.pqr("Vijay",f));&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&amp;gt;csc /r:sh.dll /r:o.dll c.cs&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Output in server dos box&lt;br /&gt;&lt;br /&gt;Constructor&lt;br /&gt;&lt;br /&gt;Constructor&lt;br /&gt;&lt;br /&gt;Pqr called&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Output in client dos box&lt;br /&gt;&lt;br /&gt;Hi&lt;br /&gt;&lt;br /&gt;VMCI Vijay&lt;br /&gt;&lt;br /&gt;More often than not, the code for the server remains unchanged. In this program, our objective&lt;br /&gt;&lt;br /&gt;is to call a function remotely, having a parameter which is of a 'user-defined' type. Therefore,&lt;br /&gt;in the file sh.cs, we have a class called fff, which is derived from the class&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;MarshalByRefObject. It contains a simple function called xyz, which displays a string that is&lt;br /&gt;&lt;br /&gt;passed as a parameter. The program centers around passing an instance of a user-defined class&lt;br /&gt;&lt;br /&gt;fff, as a parameter to the function pqr.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;In the object file o.dll, the class zzz has a function pqr, which accepts two parameters viz. a&lt;br /&gt;&lt;br /&gt;string s, and an object f. The object f is an instance of the class fff. Using the object f, the&lt;br /&gt;&lt;br /&gt;xyz function is called. This function is passed the string 'hi'. The rest of the code remains&lt;br /&gt;&lt;br /&gt;unaltered. The server, as mentioned earlier, is not modified.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;The client c.cs merely creates an object f, as an instance of class fff. This object is then&lt;br /&gt;&lt;br /&gt;passed as a parameter to pqr, without paying any cognizance to the fact that, the data shall be&lt;br /&gt;&lt;br /&gt;sent to another computer located in some other part of the world. The client could't care less,&lt;br /&gt;&lt;br /&gt;since this class is derived from the class MarshalByRefObject. This is the only prerequisite&lt;br /&gt;&lt;br /&gt;imposed on it, when instances of user-defined type are used as parameters.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;While compiling, references are to be given to the files o.dll and sh.dll, since both contain&lt;br /&gt;&lt;br /&gt;the metadata information required for error checking. The output in the client box displays&lt;br /&gt;&lt;br /&gt;'Hi'.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;You should note that a port number has been furnished in the client program. Earlier, the port&lt;br /&gt;&lt;br /&gt;number had been provided only in the server program. By having two ports registered, the&lt;br /&gt;&lt;br /&gt;framework can use the two port numbers for different purposes. While one port number may be used&lt;br /&gt;&lt;br /&gt;to enable the Client to pass parameters to the server, the other may be used to&lt;br /&gt;permit the&lt;br /&gt;&lt;br /&gt;server to pass parameters to the client. This facilitates bi-directional communication between&lt;br /&gt;&lt;br /&gt;servers and clients, thereby, permitting the use of remote parameters.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Passing by value&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;sh.cs&lt;br /&gt;&lt;br /&gt;using System;&lt;br /&gt;&lt;br /&gt;namespace nnn&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;[Serializable]&lt;br /&gt;&lt;br /&gt;public class fff&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;int j = 1;&lt;br /&gt;&lt;br /&gt;public fff()&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;Console.WriteLine("Constructor " + j);&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;public void xyz()&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;j++;&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;public int aaa()&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;return j;&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;o.cs&lt;br /&gt;&lt;br /&gt;using System;&lt;br /&gt;&lt;br /&gt;using System.Runtime.Remoting;&lt;br /&gt;&lt;br /&gt;namespace nnn&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;public class zzz : MarshalByRefObject&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;public zzz()&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;Console.WriteLine("Constructor");&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;public fff pqr(fff o)&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;o.xyz();&lt;br /&gt;&lt;br /&gt;o.xyz();&lt;br /&gt;&lt;br /&gt;return o;&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;c.cs&lt;br /&gt;&lt;br /&gt;using System;&lt;br /&gt;&lt;br /&gt;using System.Runtime.Remoting;&lt;br /&gt;&lt;br /&gt;using System.Runtime.Remoting.Channels;&lt;br /&gt;&lt;br /&gt;using System.Runtime.Remoting.Channels.Tcp;&lt;br /&gt;&lt;br /&gt;namespace nnn&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;public class ccc&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;public static void Main(string [] args)&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;TcpChannel c = new TcpChannel(8086);&lt;br /&gt;&lt;br /&gt;ChannelServices.RegisterChannel(c);&lt;br /&gt;&lt;br /&gt;fff p = new fff();&lt;br /&gt;&lt;br /&gt;zzz o = (zzz)Activator.GetObject(typeof(nnn.zzz),"Tcp://localhost:8085/eee");&lt;br /&gt;&lt;br /&gt;if (o == null)&lt;br /&gt;&lt;br /&gt;System.Console.WriteLine("Could not locate server");&lt;br /&gt;&lt;br /&gt;else&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;Console.WriteLine("Before " + p.aaa());&lt;br /&gt;&lt;br /&gt;fff a = o.pqr(p);&lt;br /&gt;&lt;br /&gt;Console.WriteLine("After " + a.aaa() + " " + p.aaa());&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Server Output&lt;br /&gt;&lt;br /&gt;Constructor&lt;br /&gt;&lt;br /&gt;Constructor&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Client Output&lt;br /&gt;&lt;br /&gt;Constructor 1&lt;br /&gt;&lt;br /&gt;Before 1&lt;br /&gt;&lt;br /&gt;After 3 1&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;In the file sh.cs, we have two functions, aaa and xyz, and a single variable j.&lt;br /&gt;Their roles are&lt;br /&gt;&lt;br /&gt;as follows:&lt;br /&gt;&lt;br /&gt;• The constructor displays the value of variable j.&lt;br /&gt;&lt;br /&gt;• The function xyz increments its value by one.&lt;br /&gt;&lt;br /&gt;• The function aaa returns the value of the variable.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;The variable j could have been accessed directly, by making it public. Instead, the program uses&lt;br /&gt;&lt;br /&gt;functions to access it. The fff class, for some reason, has to be made Serializable.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;In the file o.cs, the object zzz contains a function pqr. This function accepts an fff instance&lt;br /&gt;&lt;br /&gt;as a parameter, and then returns a object of the same type. The xyz function in fff is called&lt;br /&gt;&lt;br /&gt;twice. An object that is an instance of a particular class, is different from&lt;br /&gt;another object,&lt;br /&gt;&lt;br /&gt;which may be an instance of the same class. There is no difference in the functions contained&lt;br /&gt;&lt;br /&gt;in the objects, since the code is identical amongst the instances. The variation occurs by&lt;br /&gt;&lt;br /&gt;virtue of the values of the instance variables, contained in these objects. Thus, the variable j&lt;br /&gt;&lt;br /&gt;will possess dissimilar values in the varied instances of the same class.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;In the client, a new fff instance named 'p' is created. The constructor is called, which results&lt;br /&gt;in display of the text "Constructor 1", since the current value of the object j&lt;br /&gt;is 1. The value&lt;br /&gt;&lt;br /&gt;in j is again printed, by calling the function aaa.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Thereafter, function pqr is called with the parameter p, which is an fff object. The value of&lt;br /&gt;&lt;br /&gt;the variable j, which is contained in p, is presently 1. The function pqr calls the function xyz&lt;br /&gt;&lt;br /&gt;from the class fff twice. As a consequence of this, the value of j increases by 2. The fff&lt;br /&gt;&lt;br /&gt;instance, which is returned back by this function, is stored in 'a'. The WriteLine function then&lt;br /&gt;&lt;br /&gt;prints the value of j from both the fff objects, p and a.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;The object p is local to the client. Therefore, p.aaa would always display the value 1. The&lt;br /&gt;&lt;br /&gt;object 'a' that has been 'remoted', prints the value of j as 3.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;The names of objects are of no consequence at all, across computers. They are not visible in the&lt;br /&gt;&lt;br /&gt;IL code generated either. While remoting, the values in the local objects get handed over to a&lt;br /&gt;&lt;br /&gt;remote function. The modified value of the variable is displayed only in the new instance of the&lt;br /&gt;&lt;br /&gt;object. The local instance continues to display the original value.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Singleton&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;o.cs&lt;br /&gt;&lt;br /&gt;using System;&lt;br /&gt;&lt;br /&gt;namespace nnn&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;public class zzz : MarshalByRefObject&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;public zzz()&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;Console.WriteLine("Constructor");&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;~zzz()&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;Console.WriteLine("Destructor");&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;public String pqr()&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;Console.WriteLine("pqr");&lt;br /&gt;&lt;br /&gt;return "VMCI ";&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;s.cs&lt;br /&gt;&lt;br /&gt;using System;&lt;br /&gt;&lt;br /&gt;using System.Runtime.Remoting;&lt;br /&gt;&lt;br /&gt;using System.Runtime.Remoting.Channels;&lt;br /&gt;&lt;br /&gt;using System.Runtime.Remoting.Channels.Tcp;&lt;br /&gt;&lt;br /&gt;public class sss&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;public static void Main()&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;TcpChannel c = new TcpChannel(8085);&lt;br /&gt;&lt;br /&gt;ChannelServices.RegisterChannel(c);&lt;br /&gt;&lt;br /&gt;RemotingConfiguration.RegisterWellKnownServiceType(Type.GetType("&lt;br /&gt;&lt;br /&gt;nnn.zzz,o"), "eee", WellKnownObjectMode.Singleton);&lt;br /&gt;&lt;br /&gt;System.Console.WriteLine("Press &amp;lt;enter&amp;gt; to exit...");&lt;br /&gt;&lt;br /&gt;System.Console.ReadLine();&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;c.cs&lt;br /&gt;&lt;br /&gt;using System;&lt;br /&gt;&lt;br /&gt;using System.Threading;&lt;br /&gt;&lt;br /&gt;using System.Runtime.Remoting;&lt;br /&gt;&lt;br /&gt;using System.Runtime.Remoting.Channels;&lt;br /&gt;&lt;br /&gt;using System.Runtime.Remoting.Channels.Tcp;&lt;br /&gt;&lt;br /&gt;namespace nnn&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;public class ccc&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;public static void Main(string [] args)&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;TcpChannel c1 = new TcpChannel();&lt;br /&gt;&lt;br /&gt;ChannelServices.RegisterChannel(c1);&lt;br /&gt;&lt;br /&gt;zzz o = (zzz)Activator.GetObject(typeof(zzz),"tcp://localhost:8085/eee");&lt;br /&gt;&lt;br /&gt;Console.WriteLine(o.pqr());&lt;br /&gt;&lt;br /&gt;Console.WriteLine(o.pqr());&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Server Output&lt;br /&gt;&lt;br /&gt;Press &amp;lt;enter&amp;gt; to exit...&lt;br /&gt;&lt;br /&gt;Constructor&lt;br /&gt;&lt;br /&gt;pqr&lt;br /&gt;&lt;br /&gt;pqr&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Destructor&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Client Output&lt;br /&gt;&lt;br /&gt;VMCI&lt;br /&gt;&lt;br /&gt;VMCI&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;The object zzz, which is to be remoted, resides in file o.cs and has a single function pqr,&lt;br /&gt;&lt;br /&gt;which returns 'VMCI'. It also contains a constructor and a destructor. However, its contents are&lt;br /&gt;&lt;br /&gt;nothing earth shaking.&lt;br /&gt;&lt;br /&gt;We have only carried out a single modification in the server program. The last parameter to the&lt;br /&gt;&lt;br /&gt;function RegisterWellKnownServiceType is changed to Singleton. This is suggestive of the fact&lt;br /&gt;&lt;br /&gt;that, all requests to the remoting object are to be handled by the same instance of zzz. It does&lt;br /&gt;&lt;br /&gt;not create a separate instance of the object, every time the framework receives a request. As&lt;br /&gt;&lt;br /&gt;the name itself suggests, Singleton means 'single' or 'only one entity'.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;The client calls the function pqr twice. However, the constructor is called only once at the&lt;br /&gt;&lt;br /&gt;beginning, in order to read the metadata. The object is not created repeatedly on each request.&lt;br /&gt;&lt;br /&gt;Therefore, neither the constructor, nor the destructor is called.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;s.cs&lt;br /&gt;&lt;br /&gt;using System;&lt;br /&gt;&lt;br /&gt;using System.Runtime.Remoting;&lt;br /&gt;&lt;br /&gt;using System.Runtime.Remoting.Channels;&lt;br /&gt;&lt;br /&gt;using System.Runtime.Remoting.Channels.Tcp;&lt;br /&gt;&lt;br /&gt;using System.Runtime.Remoting.Channels.Http;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;namespace nnn {&lt;br /&gt;&lt;br /&gt;public class sss {&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;public static void Main() {&lt;br /&gt;&lt;br /&gt;TcpChannel c = new TcpChannel(8085);&lt;br /&gt;&lt;br /&gt;ChannelServices.RegisterChannel(c);&lt;br /&gt;&lt;br /&gt;HttpChannel c1 = new HttpChannel(8086);&lt;br /&gt;&lt;br /&gt;ChannelServices.RegisterChannel(c1);&lt;br /&gt;&lt;br /&gt;RemotingConfiguration.RegisterWellKnownServiceType(Type.GetType("&lt;br /&gt;&lt;br /&gt;nnn.zzz,o"),"eee", WellKnownObjectMode.Singleton);&lt;br /&gt;&lt;br /&gt;System.Console.WriteLine("Press &amp;lt;enter&amp;gt; to exit...");&lt;br /&gt;&lt;br /&gt;System.Console.ReadLine();&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;c.cs&lt;br /&gt;&lt;br /&gt;using System;&lt;br /&gt;&lt;br /&gt;using System.Threading;&lt;br /&gt;&lt;br /&gt;using System.Runtime.Remoting;&lt;br /&gt;&lt;br /&gt;using System.Runtime.Remoting.Channels;&lt;br /&gt;&lt;br /&gt;using System.Runtime.Remoting.Channels.Http;&lt;br /&gt;&lt;br /&gt;using System.Runtime.Remoting.Channels.Tcp;&lt;br /&gt;&lt;br /&gt;namespace nnn&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;public class ccc&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;public static void Main(string [] args)&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;HttpChannel c = new HttpChannel();&lt;br /&gt;&lt;br /&gt;ChannelServices.RegisterChannel(c);&lt;br /&gt;&lt;br /&gt;TcpChannel c1 = new TcpChannel();&lt;br /&gt;&lt;br /&gt;ChannelServices.RegisterChannel(c1);&lt;br /&gt;&lt;br /&gt;zzz o = (zzz)Activator.GetObject(typeof(zzz),"http://localhost:8086/eee");&lt;br /&gt;&lt;br /&gt;zzz o1 = (zzz)Activator.GetObject(typeof(zzz),"tcp://localhost:8085/eee");&lt;br /&gt;&lt;br /&gt;Console.WriteLine(o1.pqr());&lt;br /&gt;&lt;br /&gt;Console.WriteLine(o1.pqr());&lt;br /&gt;&lt;br /&gt;Console.WriteLine(o.pqr());&lt;br /&gt;&lt;br /&gt;Console.WriteLine(o.pqr());&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Server Output&lt;br /&gt;&lt;br /&gt;Press &amp;lt;enter&amp;gt; to exit...&lt;br /&gt;&lt;br /&gt;Constructor&lt;br /&gt;&lt;br /&gt;pqr&lt;br /&gt;&lt;br /&gt;pqr&lt;br /&gt;&lt;br /&gt;pqr&lt;br /&gt;&lt;br /&gt;pqr&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Destructor&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Client Output&lt;br /&gt;&lt;br /&gt;VMCI&lt;br /&gt;&lt;br /&gt;VMCI&lt;br /&gt;&lt;br /&gt;VMCI&lt;br /&gt;&lt;br /&gt;VMCI&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;The server has created two channels; one is the normal channel named TcpChannel, while the other&lt;br /&gt;&lt;br /&gt;one is called an HttpChannel. A separate port number has to be allotted to each of these&lt;br /&gt;&lt;br /&gt;channels. Therefore, the number 8085 is assigned to the TCP channel, while the number 8086 is&lt;br /&gt;&lt;br /&gt;assigned to the HTTP channel. The registration process does not delve into mundane issues, such&lt;br /&gt;&lt;br /&gt;as, the type of channel that has been used.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;The client creates both, an HttpChannel object, as well as a TcpChannel object. However, it does&lt;br /&gt;&lt;br /&gt;not specify the port numbers to the constructors. The GetObject function is used to specify a&lt;br /&gt;&lt;br /&gt;proxy. A major modification that has been incorporated here is, the HttpChannel being prefixed&lt;br /&gt;&lt;br /&gt;with http:, and the TcpChannel being prefixed with tcp:. The endpoint URI is retained as eee.&lt;br /&gt;&lt;br /&gt;The http channel connects to the server on port 8086, while the tcp channel connects to the&lt;br /&gt;&lt;br /&gt;server on port 8085. If you interchange the port numbers, the program will pause endlessly.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;The pqr function is called, by utilizing the http channel and the tcp channel. Since the call is&lt;br /&gt;&lt;br /&gt;made 4 times, 'pqr' is also displayed 4 times on our screen.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;We have not used a stopwatch to time the output generation; however, we were able to conclude&lt;br /&gt;&lt;br /&gt;that, the HTTP (Hyper Text Transfer Protocol), is much slower than the TCP (Transmission Control&lt;br /&gt;&lt;br /&gt;Protocol). This difference in the speeds of HTTP and TCP occurs because HTTP uses both SOAP (the&lt;br /&gt;&lt;br /&gt;Simple Object Access Protocol) and XML, which slows it down, whereas, TCP uses a binary&lt;br /&gt;&lt;br /&gt;protocol, which makes it relatively faster.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;A Singleton object preserves its state between function invocations, since it is not created&lt;br /&gt;&lt;br /&gt;each time that the function is invoked. A port number is not specified while registering a&lt;br /&gt;&lt;br /&gt;client channel, since the framework assigns it internally. This is dependent upon the task to be&lt;br /&gt;&lt;br /&gt;performed, such as, listening or connecting. The framework connects to a remote channel, using&lt;br /&gt;&lt;br /&gt;the URI specified. If we actually specify a port number in the client, it starts behaving like a&lt;br /&gt;&lt;br /&gt;server, i.e. it too listens on the specified port.&lt;/p&gt;</description><thr:total xmlns:thr="http://purl.org/syndication/thread/1.0">0</thr:total></item><item><title>C Language  Help, C Language Tutorials, C Language Programming, C Language Tricks   { The C# Language }</title><link>http://swebsolution.blogspot.com/2007/12/internet-classes-c.html</link><author>noreply@blogger.com (Unknown)</author><pubDate>Fri, 14 Dec 2007 22:39:00 -0800</pubDate><guid isPermaLink="false">tag:blogger.com,1999:blog-7227425366482465228.post-6728809215451939432</guid><description>&lt;p align="justify"&gt;&lt;strong&gt;&lt;b&gt;C Language  Help, C Language Tutorials, C Language Programming, C Language Tricks &lt;/b&gt;&lt;br&gt;Internet Classes&lt;/strong&gt;&lt;br /&gt;&lt;br /&gt;Before weaving our way into the Internet, let us investigate a few more fundamentals about the C# programming language, in order to acquire a superior&lt;br /&gt;appreciation of internet programming.&lt;br /&gt;&lt;br /&gt;No computer in the world is equipped to recognize the letters of the alphabet on its own. Therefore, an internal representation has to be used. One such&lt;br /&gt;representation is called ASCII, which allots a number falling between 0 to 255, to each of the letters. Here, each letter is represented by a byte, which is an&lt;br /&gt;8-bit number. For instance, the letter 'A' is represented by the number 65, while the letter 'a' is represented by the number 97.&lt;br /&gt;&lt;/p&gt;&lt;p align="justify"&gt;However, languages such as Japanese and Chinese are remarkably visual, and require a larger range of numbers to represent them in totality. In order to&lt;br /&gt;accommodate the requirement of these languages, the computer industry came out with a standard called UNICODE, which can represent every known language in the&lt;br /&gt;world in its entirety. But, to accomplish this, this standard imposes the condition that every character in the language should be represented by a 16-bit&lt;br /&gt;number, and not by an 8-bit number.&lt;br /&gt;&lt;br /&gt;a.cs&lt;br /&gt;&lt;br /&gt;public class zzz&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;public static void Main()&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;byte i = 65;&lt;br /&gt;&lt;br /&gt;System.Console.WriteLine(i);&lt;br /&gt;&lt;br /&gt;System.Console.WriteLine((char)i);&lt;br /&gt;&lt;br /&gt;i = (byte)'A';&lt;br /&gt;&lt;br /&gt;System.Console.WriteLine(i);&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Output&lt;br /&gt;&lt;br /&gt;65&lt;br /&gt;&lt;br /&gt;A&lt;br /&gt;&lt;br /&gt;65&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;The C# language takes cognizance of the fact that the byte data type represents a number ranging from 0 to 255. Thus, by default, the WriteLine function&lt;br /&gt;displays the number that the byte represents. If we typecast the byte i to a char, the WriteLine function displays its ASCII equivalent instead. The C#&lt;br /&gt;language had to indoctrinate a new data type called 'char', which could represent a UNICODE character or a number ranging from 0 to 65535, since a byte&lt;br /&gt;is too diminutive to be able to accommodate this range of values. A literal or char 'A' cannot be initialized to a byte, since we cannot equate a smaller 8-bit&lt;br /&gt;data type to a larger 16-bit data type. Therefore, a cast operator has to be used.&lt;br /&gt;&lt;br /&gt;a.cs&lt;br /&gt;&lt;br /&gt;public class zzz&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;public static void Main()&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;char i = 'A';&lt;br /&gt;&lt;br /&gt;System.Console.WriteLine(i);&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Output&lt;br /&gt;&lt;br /&gt;A&lt;br /&gt;&lt;br /&gt;A variable of char data type cannot be equated to a number. It can only be initialized to a literal. The WriteLine function prefers to receive the 'char'&lt;br /&gt;data type, since it can display the value as a letter of the alphabet. Thus, the 'char' data type is used to store 16 bit UNICODE characters, while the 'byte'&lt;br /&gt;data type is employed to store small numbers. The 'string' data type is used for a sequence of UNICODE characters.&lt;br /&gt;a.cs&lt;br /&gt;&lt;br /&gt;public class zzz&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;public static void Main()&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;byte [] i = new byte[5];&lt;br /&gt;&lt;br /&gt;i[0] = 65; i[1] = 66;&lt;br /&gt;&lt;br /&gt;System.Console.WriteLine(i);&lt;br /&gt;&lt;br /&gt;char [] j = new char[5];&lt;br /&gt;&lt;br /&gt;j[0] = 'A'; j[1] = 'B';&lt;br /&gt;&lt;br /&gt;System.Console.WriteLine(j);&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Output&lt;br /&gt;&lt;br /&gt;System.Byte[]&lt;br /&gt;&lt;br /&gt;AB&lt;br /&gt;Dealing with an array of bytes is not as straightforward and uncomplicated as dealing with an array of chars. In case of an array of bytes, the WriteLine&lt;br /&gt;function merely displays the data type of the array, whereas, in the case of an array of chars, it displays the entire array. Thus, we need a technique, by&lt;br /&gt;means of which, we are able to display the bytes present in the form of an array of bytes, using the WriteLine function.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;a.cs&lt;br /&gt;&lt;br /&gt;public class zzz&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;public static void Main()&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;byte [] i = new byte[5];&lt;br /&gt;&lt;br /&gt;i[0] = 65; i[1] = 66; i[2] = 67;i[3] = 68;&lt;br /&gt;&lt;br /&gt;string s = System.Text.Encoding.ASCII.GetString(i, 1, 2);&lt;br /&gt;&lt;br /&gt;System.Console.WriteLine(s);&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Output&lt;br /&gt;&lt;br /&gt;BC&lt;br /&gt;&lt;br /&gt;The namespace System.Text.Encoding has a class called ASCII, which contains a static function GetString. This function accepts three parameters:&lt;br /&gt;&lt;br /&gt;• The array of bytes, which is to be converted into a string.&lt;br /&gt;&lt;br /&gt;• The starting point in the array ( in our case, 1) refers to the second element in the Array, which is the letter 'B'.&lt;br /&gt;&lt;br /&gt;• The number of characters or the length of the string. We have specified it as 2. Hence, only 2 characters are seen.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;The function returns a string, which is displayed by employing the WriteLine function.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;This methodology thereby, assists in converting a certain number of bytes from a byte array into a string, which can then be effortlessly printed by the&lt;br /&gt;WriteLine function.&lt;br /&gt;The rationale for introducing this function in this chapter is due to the existence of a large number of functions that return data as an array of bytes.&lt;br /&gt;The approach demonstrated above may be employed, to display such data in all such cases.&lt;br /&gt;a.cs&lt;br /&gt;&lt;br /&gt;public class zzz&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;public static void Main()&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;string s1 = "vmukhi";&lt;br /&gt;&lt;br /&gt;byte[] i;&lt;br /&gt;&lt;br /&gt;char [] j = s1.ToCharArray();&lt;br /&gt;&lt;br /&gt;i = System.Text.Encoding.ASCII.GetBytes(j);&lt;br /&gt;&lt;br /&gt;string s = System.Text.Encoding.ASCII.GetString(i, 0, 5);&lt;br /&gt;&lt;br /&gt;System.Console.WriteLine(s);&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Output&lt;br /&gt;&lt;br /&gt;vmukh&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;We now need to resolve another quandary. In the internet-related classes, functions read and write data in the form of an array of bytes.&lt;br /&gt;&lt;br /&gt;We are comfortable dealing with strings, as they are the most natural data type available for representing data. However, this data type has to be converted&lt;br /&gt;into an array of bytes, if it has to be transmitted. But, there is no direct mechanism of attaining this conversion. The only possibility is to write our own&lt;br /&gt;function.&lt;br /&gt;&lt;br /&gt;The string is first converted into an array of chars, using ToCharArray method in the string class. As of now, there exists no magic formula in the string&lt;br /&gt;class, which can directly provide an array of bytes. So, we are compelled to utilize the GetBytes function from the ASCII class, which accepts a character&lt;br /&gt;array and converts it into an array of bytes. This is an additional step that needs to be executed in the conversion process.&lt;br /&gt;&lt;br /&gt;The last two lines of the above program merely verify whether all data has been supplied correctly or otherwise.&lt;br /&gt;&lt;br /&gt;a.cs&lt;br /&gt;&lt;br /&gt;public class zzz&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;public static void Main()&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;byte [] a;&lt;br /&gt;&lt;br /&gt;a= abc("ABC");&lt;br /&gt;&lt;br /&gt;string s = System.Text.Encoding.ASCII.GetString(a, 0, 3);&lt;br /&gt;&lt;br /&gt;System.Console.WriteLine(s);&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;public static byte [] abc( string a)&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;byte [] b = new byte[a.Length];&lt;br /&gt;&lt;br /&gt;System.Console.WriteLine(a.Length);&lt;br /&gt;&lt;br /&gt;for ( int i = 0 ; i &amp;lt; a.Length ; i++)&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;System.Console.WriteLine(a[i]);&lt;br /&gt;&lt;br /&gt;b[i] = (byte)a[i];&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;return b;&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Output&lt;br /&gt;&lt;br /&gt;3&lt;br /&gt;&lt;br /&gt;A&lt;br /&gt;&lt;br /&gt;B&lt;br /&gt;&lt;br /&gt;C&lt;br /&gt;&lt;br /&gt;ABC&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;In this program, we write our own function that accepts a string as a parameter, and returns an array of bytes. This function eliminates the intermediate step of&lt;br /&gt;converting the string into an array of chars, prior to its final conversion into an array of bytes.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;The Length member in the string returns the length of the string, and also determines the size of the byte array, which is to be created. In the 'for'&lt;br /&gt;loop, every individual character can be accessed, one at a time, by using the indexer property of the string class. Concurrently, the byte array is also&lt;br /&gt;filled up. The need for a cast operator has been explained earlier. Finally, at the end of the function, we return this freshly filled up array, and thereafter,&lt;br /&gt;display the byte array. The array is displayed merely to corroborate that all actions have been executed as planned.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;File Handling&lt;br /&gt;&lt;br /&gt;a.cs&lt;br /&gt;&lt;br /&gt;using System;&lt;br /&gt;&lt;br /&gt;using System.IO;&lt;br /&gt;&lt;br /&gt;class zzz&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;public static void Main()&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;FileStream f = new FileStream("c:\\z.txt", FileMode.Open, FileAccess.Read);&lt;br /&gt;&lt;br /&gt;byte [] a;&lt;br /&gt;&lt;br /&gt;a = new byte[512];&lt;br /&gt;&lt;br /&gt;int b = f.Read(a, 0, 512);&lt;br /&gt;&lt;br /&gt;Console.WriteLine(b);&lt;br /&gt;&lt;br /&gt;string s = System.Text.Encoding.ASCII.GetString(a, 0, b);&lt;br /&gt;&lt;br /&gt;Console.Write(s);&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;z.txt&lt;br /&gt;&lt;br /&gt;vijay&lt;br /&gt;&lt;br /&gt;mukhi&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Output&lt;br /&gt;&lt;br /&gt;12&lt;br /&gt;&lt;br /&gt;vijay&lt;br /&gt;&lt;br /&gt;mukhi&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;We start by creating an object f, which is an instance of class FileStream. The function is assigned the name of the file that we want to work with, along with&lt;br /&gt;the mode in which it is to be opened. The two parameters, FileMode.Open and FileAccess.Read, are enums, which open a file for reading.&lt;br /&gt;The Read function in the file stream reads the file. The function has 3 parameters:&lt;br /&gt;&lt;br /&gt;• The first parameter is a byte array. The data from the file is to be read into this array.&lt;br /&gt;&lt;br /&gt;• The second parameter is the offset position.&lt;br /&gt;&lt;br /&gt;• The third parameter is the maximum number of bytes to be read.&lt;br /&gt;&lt;br /&gt;Thus, the Read function shall attempt to read upto 512 bytes from the file, until it reaches the end of the file. Since the file contains only the words 'vijay&lt;br /&gt;mukhi', the output is 12, which represents the number of bytes read. This is followed by the data, i.e. vijay mukhi.&lt;br /&gt;&lt;br /&gt;Let us now browse through the data sent by a web server.&lt;br /&gt;&lt;br /&gt;a.cs&lt;br /&gt;&lt;br /&gt;using System;&lt;br /&gt;&lt;br /&gt;using System.Net;&lt;br /&gt;&lt;br /&gt;using System.IO;&lt;br /&gt;&lt;br /&gt;class zzz&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;public static void Main()&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;WebRequest r = WebRequest.Create("http://www.vijaymukhi.com");&lt;br /&gt;&lt;br /&gt;WebResponse re = r.GetResponse();&lt;br /&gt;&lt;br /&gt;Stream s = re.GetResponseStream();&lt;br /&gt;&lt;br /&gt;Byte[] a = new Byte[51200];&lt;br /&gt;&lt;br /&gt;int b = s.Read(a, 0, 51200);&lt;br /&gt;&lt;br /&gt;Console.WriteLine(b);&lt;br /&gt;&lt;br /&gt;Console.Write(System.Text.Encoding.ASCII.GetString(a, 0, b));&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Output&lt;br /&gt;&lt;br /&gt;1085&lt;br /&gt;&lt;br /&gt;&amp;lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Frameset//EN"&lt;br /&gt;&lt;br /&gt;"http://www.w3.org/TR/REC-html40/frame.dtd"&amp;gt;&lt;br /&gt;&lt;br /&gt;&amp;lt;html&amp;gt;&lt;br /&gt;&lt;br /&gt;&amp;lt;head&amp;gt;&lt;br /&gt;&lt;br /&gt;&amp;lt;title&amp;gt;Vijay Mukhi's Technology cornucopia&amp;lt;/title&amp;gt;&lt;br /&gt;&lt;br /&gt;&amp;lt;/head&amp;gt;&lt;br /&gt;&lt;br /&gt;&amp;lt;frameset frameborder="0" framespacing="0" border="0" cols="*" rows="97,*"&amp;gt;&lt;br /&gt;&lt;br /&gt;&amp;lt;frame marginwidth="0" marginheight="0" src="heading.html" name="heading"&lt;br /&gt;noresize scrolling="no"&amp;gt;&lt;br /&gt;&lt;br /&gt;&amp;lt;frameset frameborder="0" framespacing="0" border="0" cols="210,*" rows="*"&amp;gt;&lt;br /&gt;&lt;br /&gt;&amp;lt;frameset frameborder="1" framespacing="0" border="0" cols="*" rows="0,*"&amp;gt;&lt;br /&gt;&lt;br /&gt;&amp;lt;frame marginwidth="0" marginheight="0" src="code.html" name="code" noresize&lt;br /&gt;scrolling="no" frameborder="0"&amp;gt;&lt;br /&gt;&lt;br /&gt;&amp;lt;frame marginwidth="5" marginheight="5" src="menu_empty.html" name="menu"&lt;br /&gt;noresize scrolling="auto" frameborder="0"&amp;gt;&lt;br /&gt;&lt;br /&gt;&amp;lt;/frameset&amp;gt;&lt;br /&gt;&lt;br /&gt;&amp;lt;frame marginwidth="5" marginheight="5" src="main.html" name="text" noresize&amp;gt;&lt;br /&gt;&lt;br /&gt;&amp;lt;/frameset&amp;gt;&lt;br /&gt;&lt;br /&gt;&amp;lt;noframes&amp;gt;&lt;br /&gt;&lt;br /&gt;&amp;lt;p&amp;gt;The &amp;lt;code&amp;gt;NOFRAMES&amp;lt;/code&amp;gt; element is to be used to give useful content to&lt;br /&gt;people with browsers that cannot display frames. One example is Lynx, a&lt;br /&gt;text-based browser.&amp;lt;/p&amp;gt;&lt;br /&gt;&lt;br /&gt;&amp;lt;/noframes&amp;gt;&lt;br /&gt;&lt;br /&gt;&amp;lt;/frameset&amp;gt;&lt;br /&gt;&lt;br /&gt;&amp;lt;/html&amp;gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;We expect you to either have a web server installed on your machine, or at least be connected to the net.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Every machine on the internet is recognized by a name. For example. the Microsoft site is known as www.microsoft.com; the Java site on the net is known&lt;br /&gt;as www.javasoft.com; and my site is known as www.vijaymukhi.com. However, every machine is also known by the common name of 'localhost'. When the address&lt;br /&gt;specified in the browser is www.microsoft.com, the web server of that machine sends across an HTML file to the browser. The file that is sent across is either&lt;br /&gt;named index.html or default.html.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;In the above program, we would like to simulate the functioning of a browser, i.e. we want to fetch an HTML file from a web server.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;In the System.Net namespace, there is a class called WebRequest that has a static function called Create. This function requires the name of the machine&lt;br /&gt;that runs a web server. While reading this book, in case you are not connected to the Internet, you may use 'http://localhost/' as the name of the machine. Or&lt;br /&gt;else, you may connect to a site on the net such as www.vijaymukhi.com.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;A WebRequest object r is returned by the Create function. The WebRequest class has another function called GetResponse, which returns a WebResponse object. The&lt;br /&gt;value returned is stored in a variable called re. Calling the method GetResponseStream from the WebResponse object returns a Stream object that can be utilized to read the HTML file sent across by the web server.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;The file handling concepts, learnt a short while ago, may be applied here too. The interaction between our program and the web server is handled by two&lt;br /&gt;classes, i.e. WebRequest and WebResponse. We have no inkling as to what each class accomplishes.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;In order to read the HTML file off the Stream object, we create a byte array having a size of 51200. Thereafter, we employ the Read function to read 51200&lt;br /&gt;bytes, from the inception of the stream, into the array. The value returned by the Read function is indicative of the number of bytes that have been read off&lt;br /&gt;the stream. On displaying this value, the number obtained is less then 51200 (in our case it is 1085), and not 51200. Therefore, despite our having commanded the&lt;br /&gt;Read function to read upto 51200 bytes, it read only 1085 bytes. This is because it encountered an End Of File mark after having read 1085 bytes.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;In case of an error, the WebResponse object displays a Status code. Few of the values that may be assigned to this Status code, are as follows:&lt;br /&gt;&lt;br /&gt;• 200 indicates 'success'.&lt;br /&gt;&lt;br /&gt;• 403 indicates 'permission denied'.&lt;br /&gt;&lt;br /&gt;• 404 indicates that the 'file name does not exist'.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Since no errors are visible in our case, we assume the status code to be 200.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Thereafter, the byte array is converted into a string, so that it can be displayed using the GetString function. Therefore, we see the first 1085 bytes&lt;br /&gt;of the HTML file, sent by the web server.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;a.cs&lt;br /&gt;&lt;br /&gt;using System;&lt;br /&gt;&lt;br /&gt;using System.Net;&lt;br /&gt;&lt;br /&gt;using System.IO;&lt;br /&gt;&lt;br /&gt;class zzz&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;public static void Main()&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;WebRequest r = WebRequest.Create("http://www.yahoo.com");&lt;br /&gt;&lt;br /&gt;WebResponse re = r.GetResponse();&lt;br /&gt;&lt;br /&gt;Stream s = re.GetResponseStream();&lt;br /&gt;&lt;br /&gt;Byte[] a = new Byte[51200];&lt;br /&gt;&lt;br /&gt;int b = s.Read(a, 0, 51200);&lt;br /&gt;&lt;br /&gt;while (b &amp;gt; 0)&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;Console.Write(System.Text.Encoding.ASCII.GetString(a, 0, b));&lt;br /&gt;&lt;br /&gt;b = s.Read(a, 0, 512);&lt;br /&gt;&lt;br /&gt;System.Console.WriteLine(b);&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;We are not in a position to estimate the file size of the HTML file, which has been sent by the web server. So, we take recourse to the 'while' loop in&lt;br /&gt;situations of incertitude or perplexity.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;The Read function returns a zero, when there is no more data to be read from the stream. Thus, the 'while' loop terminates when the value of b becomes zero.&lt;br /&gt;Therefore, the last line of the output would always display a zero. In fact, we can regulate the number of bytes that we want the Read function to read off the&lt;br /&gt;stream. Here, we specify a value of 512. The framework enjoys the license to ignore this number.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;In order to retrieve a specific file off the web server, such as a.html, we add the name of the file at the end of the URL. If the file appertains to the local&lt;br /&gt;hard disk, the Create function will be written as follows:&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;WebRequest r = WebRequest.Create("http://localhost/a.html");&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;The next query which is likely to vex our minds is: Where would the file a.html be stored on the hard disk? The answer to this depends upon the web server&lt;br /&gt;installed on the machine. In case of IIS or PWS from Microsoft, the file a.html would be placed in the sub-directory c:\inetpub\wwwroot. Reading a file sent by&lt;br /&gt;a web server is similar to reading a file from the local disk. Therefore, we do not have to discover two separate ways of reading the same thing.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;a.cs&lt;br /&gt;&lt;br /&gt;using System;&lt;br /&gt;&lt;br /&gt;using System.Net;&lt;br /&gt;&lt;br /&gt;using System.IO;&lt;br /&gt;&lt;br /&gt;class zzz&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;public static void Main()&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;Uri u = new Uri("http://www.yahoo.com");&lt;br /&gt;&lt;br /&gt;HttpWebRequest wr = (HttpWebRequest) WebRequest.Create(u);&lt;br /&gt;&lt;br /&gt;AsyncCallback a = new AsyncCallback(abc);&lt;br /&gt;&lt;br /&gt;IAsyncResult r = (IAsyncResult) wr.BeginGetResponse(a,wr);&lt;br /&gt;&lt;br /&gt;Console.ReadLine();&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;static void abc(IAsyncResult ar)&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;HttpWebRequest r = (HttpWebRequest) ar.AsyncState;&lt;br /&gt;&lt;br /&gt;HttpWebResponse re = (HttpWebResponse) r.EndGetResponse(ar);&lt;br /&gt;&lt;br /&gt;Stream s = re.GetResponseStream();&lt;br /&gt;&lt;br /&gt;Console.WriteLine("Status code: " + re.StatusCode);&lt;br /&gt;&lt;br /&gt;Byte[] a = new Byte[51200];&lt;br /&gt;&lt;br /&gt;int b = s.Read(a, 0, 51200);&lt;br /&gt;&lt;br /&gt;while (b &amp;gt; 0)&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;Console.Write(System.Text.Encoding.ASCII.GetString(a, 0, b));&lt;br /&gt;&lt;br /&gt;b = s.Read(a, 0, 512);&lt;br /&gt;&lt;br /&gt;System.Console.WriteLine(b);&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;The above program displays the same output as before, but with a difference. It follows a dissimilar method of writing code. In the previous example, the Read&lt;br /&gt;function waited till the data was received from the Internet. When we are connected to the Internet, this wait could be indefinite. This effectively would&lt;br /&gt;disable the program from proceeding any further. While awaiting data, the program cannot carry out any other activity. Thus, to avoid squandering of time and resources, we need a mechanism, by means of which, the program can be&lt;br /&gt;intimated about the receipt of data for the HTML file.&lt;br /&gt;&lt;br /&gt;The static function Create, now accepts a URI as a parameter. To create this URI object, we pass the same URI string as a parameter to the constructor. The&lt;br /&gt;return value of this function is stored in a HttpWebRequest object, since this class is derived from the class WebRequest. This class has a function&lt;br /&gt;BeginGetResponse, which accepts two parameters.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;The first is an AsyncCallback delegate, which requires the name of a function. In the C# world, a delegate simply stands-in for a function to be called.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;The second parameter is a State object. Despite our not making any use of any state related information, we are not allowed to supply a null value. It is&lt;br /&gt;mandatory to supply a value, without which, an error is generated. Thus, we specify the object wr, of type HttpWebRequest, as the parameter. Under normal&lt;br /&gt;circumstances, we are required to create a State object and furnish it as a parameter.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;After a certain time duration, data arrives from the web server, and the function abc gets called. This function is passed IAsyncResult as a parameter,&lt;br /&gt;which has a property AsyncState. Since this is an Object, we cast it to HttpWebRequest.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;We require an HttpWebResponse object, in order to obtain a Stream object, which will be employed to read the file received from the web server. The&lt;br /&gt;HttpWebRequest class has a EndGetResponse function, which accepts an IAsyncResult as a parameter, and returns a HttpWebResponse object. This object,&lt;br /&gt;in turn, is derived from the HttpResponse class. This function terminates the asynchronous request. The remaining code is similar to the above program. We&lt;br /&gt;however, are at liberty to execute other tasks, while the file is being received. Normally, a separate thread runs the function abc in an asynchronous&lt;br /&gt;manner.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;a.cs&lt;br /&gt;&lt;br /&gt;using System;&lt;br /&gt;&lt;br /&gt;using System.Net;&lt;br /&gt;&lt;br /&gt;using System.IO;&lt;br /&gt;&lt;br /&gt;class zzz&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;public static void Main()&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;Uri u = new Uri("http://www.yahoo.com");&lt;br /&gt;&lt;br /&gt;HttpWebRequest w = (HttpWebRequest) WebRequest.Create(u);&lt;br /&gt;&lt;br /&gt;IAsyncResult r = (IAsyncResult) w.BeginGetResponse(new AsyncCallback(abc),w);&lt;br /&gt;&lt;br /&gt;Console.ReadLine();&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;static void abc(IAsyncResult ar)&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;HttpWebRequest r = (HttpWebRequest) ar.AsyncState;&lt;br /&gt;&lt;br /&gt;HttpWebResponse re = (HttpWebResponse) r.EndGetResponse(ar);&lt;br /&gt;&lt;br /&gt;int b = 0;&lt;br /&gt;&lt;br /&gt;char[] a = new char[512];&lt;br /&gt;&lt;br /&gt;StreamReader rd = new StreamReader(re.GetResponseStream(),&lt;br /&gt;System.Text.Encoding.UTF8);&lt;br /&gt;&lt;br /&gt;StringWriter w = new StringWriter();&lt;br /&gt;&lt;br /&gt;b = rd.Read(a, 0, 512);&lt;br /&gt;&lt;br /&gt;while (b != 0 )&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;w.Write(a, 0, 512);&lt;br /&gt;&lt;br /&gt;b= rd.Read(a, 0, 512);&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;Console.WriteLine("Message = " + w.ToString());&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;There are a myriad ways of achieving the same output. In our case, we have presented a program that displays the same output as before, by using a&lt;br /&gt;different set of classes. The variations are only in the function abc.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;We first create an array of chars and not bytes. Thereafter, a StreamReader object rd, which is derived from TextReader, is created. This class has the&lt;br /&gt;ability to read characters from the Stream object supplied to it. The second parameter to the constructor is the encoding-type to be used, while reading&lt;br /&gt;character input from a stream. The Stream class is designed for byte input-output only. By default, it takes the UTF-8 encoding. Thus, we did not&lt;br /&gt;have to pass it as a parameter. It does not default to the ANSI code page for the current system. The advantage of UTF-8 is that, it handles Unicode&lt;br /&gt;characters appropriately, with localized versions of the operating system.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;The StreamReader class is similar to the Stream class, in that, its Read function accepts an array of chars and not an array of bytes. The return value&lt;br /&gt;also remains the same.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Thereafter, the StringWriter class concatenates this series of chars. Thus, the StringWriter object w, stores the HTML file as one large string, which gets&lt;br /&gt;appended by the Write function.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;We could also have used the WriteLine function to display the array of chars.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Domain Name System&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;a.cs&lt;br /&gt;&lt;br /&gt;using System;&lt;br /&gt;&lt;br /&gt;using System.Net;&lt;br /&gt;&lt;br /&gt;class zzz&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;public static void Main()&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;String s = Dns.GetHostName();&lt;br /&gt;&lt;br /&gt;System.Console.WriteLine(s);&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Output&lt;br /&gt;&lt;br /&gt;vmukhi&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Whenever we need to connect to any computer, the machine's IP address has to be determined. Every machine on the Internet is identified by a unique number,&lt;br /&gt;which is called the machine's IP address. This number is of a 'long' data type. Since it is humanly impossible to memorize 4 billion numbers, every machine is&lt;br /&gt;also allotted a name. The system that maps a name to its corresponding IP address, is called the Domain Name System (DNS).&lt;br /&gt;&lt;br /&gt;The class Dns contains only static entities, and hence, it is called a static class. One of its members is called GetHostName, which returns the name of the machine on which the current program is being executed.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;As we mentioned earlier, every machine by default, is assigned the name of localhost. While installing Windows 2000, we have proactively assigned it the&lt;br /&gt;name of 'vmukhi'. Hence, the output shows 'vmukhi'.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;a.cs&lt;br /&gt;&lt;br /&gt;using System;&lt;br /&gt;&lt;br /&gt;using System.Net;&lt;br /&gt;&lt;br /&gt;class zzz&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;public static void Main() {&lt;br /&gt;&lt;br /&gt;IPHostEntry i = Dns.GetHostByName("www.microsoft.com");&lt;br /&gt;&lt;br /&gt;IPAddress [] a = i.AddressList;&lt;br /&gt;&lt;br /&gt;foreach ( IPAddress j in a)&lt;br /&gt;&lt;br /&gt;System.Console.WriteLine(j.ToString()+ " " + j.Address);&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Output&lt;br /&gt;&lt;br /&gt;207.46.130.45 763506383&lt;br /&gt;&lt;br /&gt;207.46.230.229 -437899569&lt;br /&gt;&lt;br /&gt;207.46.131.91 1535323855&lt;br /&gt;&lt;br /&gt;207.46.131.199 -947704113&lt;br /&gt;&lt;br /&gt;207.46.230.219 -605671729&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Every web server or client on the Internet is known by an IP address and a corresponding name. Moreover, the name on the Internet, such as,&lt;br /&gt;www.microsoft.com, may not necessarily be restricted to a single IP address. It may have multiple IP addresses mapped onto it. This is what the above program&lt;br /&gt;demonstrates.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;The function GetHostByName is called and given the domain name, whose IP addresses are to be retrieved. An object is returned, which is an instance of&lt;br /&gt;IPHostEntry. This class has a property AddressList, which returns an array of IPAddress objects. A single object is assigned to each IP address represented by&lt;br /&gt;the domain name.&lt;br /&gt;&lt;br /&gt;An IP address is a protracted number. It comprises of four numbers, each having a value, which falls within the range of 0 to 255. Thus, we can represent an IP&lt;br /&gt;address in the dotted decimal notation, where the four numbers are separated from each other by a dot. Displaying the Address member directly exhibits a long&lt;br /&gt;number. You can ascertain as to which of these is more readable. The 'foreach' statement is used to iterate through the array.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;a.cs&lt;br /&gt;&lt;br /&gt;using System;&lt;br /&gt;&lt;br /&gt;using System.Net;&lt;br /&gt;&lt;br /&gt;class zzz&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;public static void Main()&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;IPHostEntry i = Dns.GetHostByName("www.microsoft.com");&lt;br /&gt;&lt;br /&gt;String [] a = i.Aliases;&lt;br /&gt;&lt;br /&gt;foreach ( String j in a)&lt;br /&gt;&lt;br /&gt;System.Console.WriteLine(j);&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Output&lt;br /&gt;&lt;br /&gt;www.microsoft.com&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;An IP address may contain other DNS names, which resolve to the same address. In this case, there are none. Thus, it is evident that the Microsoft site on the&lt;br /&gt;net has no other aliases or names.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;a.cs&lt;br /&gt;&lt;br /&gt;using System;&lt;br /&gt;&lt;br /&gt;using System.Net;&lt;br /&gt;&lt;br /&gt;class zzz&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;public static void Main()&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;IPHostEntry i = Dns.GetHostByName("www.microsoft.com");&lt;br /&gt;&lt;br /&gt;String a = i.HostName;&lt;br /&gt;&lt;br /&gt;System.Console.WriteLine(a);&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;Output&lt;br /&gt;&lt;br /&gt;www.microsoft.akadns.net&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;The property HostName gives us the DNS name of the host. The HostName for www.microsoft.com happens to be www.microsoft.akadns.net.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;a.cs&lt;br /&gt;&lt;br /&gt;using System;&lt;br /&gt;&lt;br /&gt;using System.Net;&lt;br /&gt;&lt;br /&gt;class zzz&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;public static void Main()&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;IPAddress i = IPAddress.Parse("207.46.130.45");&lt;br /&gt;&lt;br /&gt;System.Console.WriteLine(i.ToString()+ " " + i.Address);&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Output&lt;br /&gt;&lt;br /&gt;207.46.130.45 763506383&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;At times, we may wish to convert a dotted decimal IP address to an int, i.e. an actual long number. The Parse function in IPAddress accepts a dotted decimal IP&lt;br /&gt;address as a string, and returns an object that is an instance of IPAddress. We then display the value as a string and an int.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;How do we achieve the reverse process? We may want to convert an IP Address from a dotted decimal notation, into an instance of IP Address. At other times, we&lt;br /&gt;may want to convert an IPAddress, which is in the form of a string, into the dotted decimal notation. The same function can be used to accomplish both these&lt;br /&gt;predilections.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;a.cs&lt;br /&gt;&lt;br /&gt;using System;&lt;br /&gt;&lt;br /&gt;using System.Net;&lt;br /&gt;&lt;br /&gt;class zzz&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;public static void Main()&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;String s = Dns.IpToString(763506383);&lt;br /&gt;&lt;br /&gt;System.Console.WriteLine(s);&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Output&lt;br /&gt;&lt;br /&gt;207.46.130.45&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;The IP addresses are capable of being converted from an int format into a dotted decimal string or quad representation, by using the IpToString function.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;a.cs&lt;br /&gt;&lt;br /&gt;using System;&lt;br /&gt;&lt;br /&gt;using System.Net;&lt;br /&gt;&lt;br /&gt;class zzz&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;public static void Main()&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;IPHostEntry i = Dns.Resolve("www.microsoft.com");&lt;br /&gt;&lt;br /&gt;System.Console.WriteLine(i.AddressList[0].ToString());&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Output&lt;br /&gt;&lt;br /&gt;207.46.230.229&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;The function Resolve in the Dns class, has no complications. We may be interested in an IP address, representing either a single domain name or&lt;br /&gt;multiple domain names. One of the solutions is, to use the function GetHostByName. Alternatively, the Resolve function may be used to achieve the&lt;br /&gt;same output.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Servers and Clients&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Server program&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;a.cs&lt;br /&gt;&lt;br /&gt;using System;&lt;br /&gt;&lt;br /&gt;using System.Net;&lt;br /&gt;&lt;br /&gt;using System.Net.Sockets;&lt;br /&gt;&lt;br /&gt;using System.Text;&lt;br /&gt;&lt;br /&gt;class zzz&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;public static void Main()&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;DateTime n;&lt;br /&gt;&lt;br /&gt;String s1;&lt;br /&gt;&lt;br /&gt;TcpListener t = new TcpListener(13);&lt;br /&gt;&lt;br /&gt;t.Start();&lt;br /&gt;&lt;br /&gt;while (true)&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;System.Console.WriteLine("Before");&lt;br /&gt;&lt;br /&gt;Socket s = t.AcceptSocket();&lt;br /&gt;&lt;br /&gt;System.Console.WriteLine("After");&lt;br /&gt;&lt;br /&gt;n = DateTime.Now;&lt;br /&gt;&lt;br /&gt;s1 = n.ToShortDateString() + " " + n.ToLongTimeString();&lt;br /&gt;&lt;br /&gt;Byte[] b = Encoding.ASCII.GetBytes(s1.ToCharArray());&lt;br /&gt;&lt;br /&gt;s.Send(b, b.Length, 0);&lt;br /&gt;&lt;br /&gt;Console.WriteLine("Sent " + s1);&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Client program&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;b.cs&lt;br /&gt;&lt;br /&gt;using System;&lt;br /&gt;&lt;br /&gt;using System.Net;&lt;br /&gt;&lt;br /&gt;using System.Net.Sockets;&lt;br /&gt;&lt;br /&gt;using System.IO;&lt;br /&gt;&lt;br /&gt;using System.Text;&lt;br /&gt;&lt;br /&gt;class zzz&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;public static void Main(String[] args)&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;TcpClient t = new TcpClient();&lt;br /&gt;&lt;br /&gt;Byte[] r = new Byte[32];&lt;br /&gt;&lt;br /&gt;t.Connect("127.0.0.1", 13);&lt;br /&gt;&lt;br /&gt;Stream s = t.GetStream();&lt;br /&gt;&lt;br /&gt;int n = s.Read(r, 0, r.Length);&lt;br /&gt;&lt;br /&gt;String a = Encoding.ASCII.GetString(r);&lt;br /&gt;&lt;br /&gt;Console.WriteLine("Received " + n );&lt;br /&gt;&lt;br /&gt;Console.WriteLine(a);&lt;br /&gt;&lt;br /&gt;t.Close();&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Server Dos box&lt;br /&gt;&lt;br /&gt;Before&lt;br /&gt;&lt;br /&gt;After&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;We have supplied the following two programs, which are to be executed in two different DOS boxes:&lt;br /&gt;&lt;br /&gt;• a TCP server program.&lt;br /&gt;&lt;br /&gt;• a TCP client program.&lt;br /&gt;&lt;br /&gt;The Server is to be executed first, since the client expects the server to be running. So, lets understand the server program first.&lt;br /&gt;&lt;br /&gt;The DateTime class provides access to the current date and time, in a multitude of formats. We first create an object, which is an instance of the TcpListener&lt;br /&gt;class. Thereafter, the constructor is passed a parameter of 13. It has no other significance, other than being called a Port Number.&lt;br /&gt;&lt;br /&gt;Most people, when online, either surf the net, or download files, or send E-mail. These activities have certain rules/protocols that are required to be&lt;br /&gt;followed for successful execution of the task. We cannot have separate machines to handle each of the above protocols. Therefore, the programs/servers are&lt;br /&gt;executed on the same machine; however, they work on a specified number called, the Port Number.&lt;br /&gt;&lt;br /&gt;Every packet of data is tagged with a number, which specifies the protocol that the packet belongs to. There is a world-wide body called IANA (Internet Assigned&lt;br /&gt;Numbers Authority), which determines the number to be assigned to each protocol. Some of them are as follows:&lt;br /&gt;&lt;br /&gt;• The World Wide Web has been allocated the number 80&lt;br /&gt;&lt;br /&gt;• FTP has been allocated the number 21&lt;br /&gt;&lt;br /&gt;• E-mail has been allocated the number 25.&lt;br /&gt;&lt;br /&gt;Thus, if we notice a packet with a port number 80, we would immediately understand that the packet is carrying data for the World Wide Web, i.e. the www&lt;br /&gt;protocol.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;The TcpListener class constructor requires the Port Number, since our server only accepts packets that carry this number. We have asked our server to listen,&lt;br /&gt;and accept packets on port 13. The first 1024 port numbers are reserved by IANA for various protocols. The rest of the numbers may be utilized by us, for our&lt;br /&gt;own protocols. The number 13 falls within the range of reserved port numbers.&lt;br /&gt;&lt;br /&gt;TCP stands for Transmission Control Protocol. Every TCP server needs a function to kick start the entire process. This function is called 'Start'. As of now, we&lt;br /&gt;are neither aware of, nor care about what 'start' actually does. We are only apprehensive and aware about the fact that, if this function is not called, none of the clients would ever be able to connect to our server. The TcpListener&lt;br /&gt;class is used extensively while writing an Internet server.&lt;br /&gt;&lt;br /&gt;Around 20 years ago, a large number of networking protocols existed. A networking protocol merely consists of a series of numbers in a specific format,&lt;br /&gt;which two machines transmit to each other, in order to facilitate communication. It was extremely complex and cumbersome for programmers to comprehend the&lt;br /&gt;numerous networking protocols. TCP/IP was also one such protocol. At that time, nobody would have known that, it would turn out to be the world's most dominant&lt;br /&gt;networking protocol, in the days to come.&lt;br /&gt;&lt;br /&gt;A group of programmers banded together and came out with a series of functions, which could generate the bytes for a large number of networking protocols. For&lt;br /&gt;reasons unknown, they named these functions as Sockets API. Thus, a networking coder is traditionally known as a Sockets programmer.&lt;br /&gt;&lt;br /&gt;In the case of C#, we have a class called Socket. TcpListener is derived from it. The advantage of using socket classes like TCPListener is that, functions&lt;br /&gt;like 'Start', call a tremendous amount of code from other Sockets classes. So, we do not have to delve into the innards of sockets programming, in order to be&lt;br /&gt;able to use them. The TCPListener class, in a sense, provides us with networking services at a higher level of abstraction, than what could have been achieved,&lt;br /&gt;by using the Socket class directly.&lt;br /&gt;&lt;br /&gt;On running our server ‘a’, we only notice a single word 'Before'. Thereafter, our server seems to wait forever. This wait is attributable to the Accept&lt;br /&gt;function. In computer parlance, it goes off to sleep. Our server waits till some TCP client connects to our server on port 13. This wait could extend till&lt;br /&gt;eternity.&lt;br /&gt;&lt;br /&gt;The time has come to try and comprehend as to what a TCP client program is.&lt;br /&gt;&lt;br /&gt;The TCPClient class is similar to a TcpListener class. This class has functions that use the code from Sockets classes and offer assistance in writing a TCP&lt;br /&gt;client program.&lt;br /&gt;&lt;br /&gt;The Connect function connects to a server. Therefore, it requires the domain name or IP address of the machine, which we desire to connect to. Further, the&lt;br /&gt;port number also needs to be specified. We have specified the port number as 13. This is because our server only accepts packets marked with the port number 13.&lt;br /&gt;When the Connect function attempts to connect to the server, the server moves beyond the Accept function.&lt;br /&gt;&lt;br /&gt;The function Accept in the server, returns an instance of a Socket class. This object now becomes a handle, and is used to return data back to the client. The&lt;br /&gt;DateTime class has a static member called 'Now', which gives the current date and time. Since the value is a DateTime object, we use one of the many functions&lt;br /&gt;in the DateTime class, to display the date as a string in a specific format. Here, we use two functions, ToShortDateString and ToLongDateString; and store&lt;br /&gt;the concatenated string value in a variable s1.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;This string is now required to be sent across to the client. To accomplish this, we need to convert the date in the string, into an array of bytes. So, we first&lt;br /&gt;convert the string s1 to an array of chars, and then, employ the GetBytes function to convert this array of chars into an array of bytes. The socket class&lt;br /&gt;has a function called 'Send'. It accepts a byte array as the first parameter and the length of the array as the second parameter; and eventually sends the data&lt;br /&gt;to the client.&lt;br /&gt;At the client end, the GetStream function is used to obtain a Stream object. Then, using the good old Read function, the bytes in the Stream are read into a&lt;br /&gt;byte array. Finally, the byte array is converted into a string, and then its contents are displayed.&lt;br /&gt;&lt;br /&gt;In one of our other C# books, we had demonstrated how the Web client connects to the Webserver on port 80. This program had used a set of classes distinct from&lt;br /&gt;the ones used here. But, in the ultimate analysis, they all call code from the Sockets class.&lt;br /&gt;&lt;br /&gt;&lt;/p&gt;</description><thr:total xmlns:thr="http://purl.org/syndication/thread/1.0">0</thr:total></item><item><title>C Language  Help, C Language Tutorials, C Language Programming, C Language Tricks  { The C# Language }</title><link>http://swebsolution.blogspot.com/2007/12/threads-events-and-mutexes.html</link><author>noreply@blogger.com (Unknown)</author><pubDate>Fri, 14 Dec 2007 03:40:00 -0800</pubDate><guid isPermaLink="false">tag:blogger.com,1999:blog-7227425366482465228.post-5080552392371182743</guid><description>&lt;p align="justify"&gt;&lt;br /&gt;&lt;b&gt;C Language  Help, C Language Tutorials, C Language Programming, C Language Tricks &lt;/b&gt;&lt;br /&gt;&lt;br /&gt;Threads, Events and Mutexes&lt;br /&gt;&lt;br /&gt;Threads operate on the premise that the computer is definitely more expeditious&lt;br /&gt;than a human at&lt;br /&gt;&lt;br /&gt;executing tasks, resulting in the computer idling away most of its processing&lt;br /&gt;time, waiting for&lt;br /&gt;&lt;br /&gt;the human operator. Threads are a means of overcoming this wastage of processing&lt;br /&gt;time. They&lt;br /&gt;&lt;br /&gt;perform multiple tasks on a computer in rapid succession, thereby, creating the&lt;br /&gt;illusion of&lt;br /&gt;&lt;br /&gt;these tasks being executed simultaneously. No application can ever be&lt;br /&gt;comprehensive without&lt;br /&gt;&lt;br /&gt;employing threads. Before we try to infer what a thread does in the programming&lt;br /&gt;context, let us&lt;br /&gt;&lt;br /&gt;rummage through a few examples given below.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Without being discursive, let us venture out on our odyssey of understanding the&lt;br /&gt;concept of a&lt;br /&gt;&lt;br /&gt;thread with the assistance of a very diminutive program.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;a.cs&lt;br /&gt;&lt;br /&gt;using System.Threading;&lt;br /&gt;&lt;br /&gt;public class yyy&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;public static void abc()&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;System.Console.WriteLine("Hi");&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;public class zzz&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;public static void Main()&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;ThreadStart ts = new ThreadStart(yyy.abc);&lt;br /&gt;&lt;br /&gt;Thread t = new Thread(ts);&lt;br /&gt;&lt;br /&gt;System.Console.WriteLine("Before Start");&lt;br /&gt;&lt;br /&gt;t.Start();&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Output&lt;br /&gt;&lt;br /&gt;Before Start&lt;br /&gt;&lt;br /&gt;Hi&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;This one is bound to leave you astonished because, we had earlier talked about&lt;br /&gt;starting out with&lt;br /&gt;&lt;br /&gt;a 'dimunitive' program. However, by no stretch of the imagination can the above&lt;br /&gt;program qualify&lt;br /&gt;&lt;br /&gt;as miniscule. Besides, the only work accomplished by this function is that it&lt;br /&gt;calls the static&lt;br /&gt;&lt;br /&gt;function abc, which in turn displays 'Hi'.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;In Main, we create an object ts, which is an instance of the class ThreadStart,&lt;br /&gt;which is&lt;br /&gt;&lt;br /&gt;derived from Delegate. Therefore, even though ThreadStart is a class, it also&lt;br /&gt;happens to be a&lt;br /&gt;&lt;br /&gt;delegate, whose constructor is given a static function called abc. Function abc&lt;br /&gt;is placed in the&lt;br /&gt;&lt;br /&gt;yyy class so that other classes can also use it. The program will work in a&lt;br /&gt;similar manner even&lt;br /&gt;&lt;br /&gt;if the static function is placed in class zzz.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Next, we create another object t, which is an instance of Thread. The&lt;br /&gt;constructor of this object&lt;br /&gt;&lt;br /&gt;is given a ThreadStart object ts. Indirectly, ts stands for the static function&lt;br /&gt;yyy.abc since it&lt;br /&gt;&lt;br /&gt;is a delegate.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;So far, tranquility prevails and nothing transpires. The function yyy.abc too&lt;br /&gt;does not get&lt;br /&gt;&lt;br /&gt;called. But, as soon as we call Start off the thread, the function abc gets&lt;br /&gt;catapulted into&lt;br /&gt;&lt;br /&gt;action. Thus, the function abc is called only when the Start function is called.&lt;br /&gt;This is really&lt;br /&gt;&lt;br /&gt;no big deal. Note that classes beginning with Thread belong to the&lt;br /&gt;System.Threading namespace.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;a.cs&lt;br /&gt;&lt;br /&gt;using System.Threading;&lt;br /&gt;&lt;br /&gt;public class yyy&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;public void abc()&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;System.Console.WriteLine("Hi");&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;public class zzz&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;public static void Main()&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;yyy a = new yyy();&lt;br /&gt;&lt;br /&gt;Thread t = new Thread(new ThreadStart(a.abc));&lt;br /&gt;&lt;br /&gt;t.Start();&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Output&lt;br /&gt;&lt;br /&gt;Hi&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;The above program is similar to the previous one and resembles the samples&lt;br /&gt;supplied by&lt;br /&gt;&lt;br /&gt;Microsoft. The function abc to be called, is non-static and hence, an object&lt;br /&gt;name is needed to&lt;br /&gt;&lt;br /&gt;reference it. The ThreadStart delegate object is directly passed as a parameter&lt;br /&gt;to the Thread&lt;br /&gt;&lt;br /&gt;constructor.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;a.cs&lt;br /&gt;&lt;br /&gt;using System.Threading;&lt;br /&gt;&lt;br /&gt;public class yyy&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;public void abc()&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;for ( int i = 0; i&amp;lt;=3;i++)&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;System.Console.Write(i + " ");&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;public void pqr()&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;for ( int i = 0; i&amp;lt;=3;i++)&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;System.Console.Write(i+ "...");&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;public class zzz&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;public static void Main()&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;yyy a = new yyy();&lt;br /&gt;&lt;br /&gt;Thread t = new Thread(new ThreadStart(a.abc));&lt;br /&gt;&lt;br /&gt;Thread t1 = new Thread(new ThreadStart(a.pqr));&lt;br /&gt;&lt;br /&gt;t.Start();&lt;br /&gt;&lt;br /&gt;t1.Start();&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Output&lt;br /&gt;&lt;br /&gt;0 1 2 3 0...1...2...3...&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;The example embodies a very large amount of code. However, it does not create&lt;br /&gt;any fresh ripples&lt;br /&gt;&lt;br /&gt;in our pond of knowledge. We have merely created two Thread objects t and t1,&lt;br /&gt;and passed their&lt;br /&gt;&lt;br /&gt;constructors a different delegate or function name, i.e. abc and pqr,&lt;br /&gt;respectively. Thereafter,&lt;br /&gt;&lt;br /&gt;the Start function has been called in t and t1. Here, the function abc gets&lt;br /&gt;called, which&lt;br /&gt;&lt;br /&gt;displays four numbers. Thereafter, the function pqr gets called, which also&lt;br /&gt;displays four&lt;br /&gt;&lt;br /&gt;numbers, but with three dots.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;You may wonder with trepidation as to when you will bite into the real meat.&lt;br /&gt;Keep your&lt;br /&gt;&lt;br /&gt;impatience in abeyance for a little while !&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;a.cs&lt;br /&gt;&lt;br /&gt;using System.Threading;&lt;br /&gt;&lt;br /&gt;public class yyy&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;public void abc()&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;for ( int i = 0; i&amp;lt;=3;i++)&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;System.Console.Write(i + " ");&lt;br /&gt;&lt;br /&gt;Thread.Sleep(1);&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;public void pqr()&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;for ( int i = 0; i&amp;lt;=3;i++)&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;System.Console.Write(i+ "...");&lt;br /&gt;&lt;br /&gt;Thread.Sleep(1);&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;public class zzz&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;public static void Main()&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;yyy a = new yyy();&lt;br /&gt;&lt;br /&gt;Thread t = new Thread(new ThreadStart(a.abc));&lt;br /&gt;&lt;br /&gt;Thread t1 = new Thread(new ThreadStart(a.pqr));&lt;br /&gt;&lt;br /&gt;t.Start();&lt;br /&gt;&lt;br /&gt;t1.Start();&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Output&lt;br /&gt;&lt;br /&gt;0 0...1 1...2 2...3 3...&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Now, we are at the threshold of excitement. By adding the function Sleep, which&lt;br /&gt;is a static&lt;br /&gt;&lt;br /&gt;function in the Thread class, the functions abc and pqr get called&lt;br /&gt;simultaneously, although not&lt;br /&gt;&lt;br /&gt;sequentially. This is how the concept of a thread is implemented, i.e. it&lt;br /&gt;empowers your computer&lt;br /&gt;&lt;br /&gt;to accomplish multiple jobs at the same time. This process is also termed as&lt;br /&gt;multi-tasking.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Take the case of Microsoft Word. When you attempt to save a file, irrespective&lt;br /&gt;of the size of&lt;br /&gt;&lt;br /&gt;the file, Word appears to consume the same amount of time in completing this&lt;br /&gt;task. To expect it&lt;br /&gt;&lt;br /&gt;to save a 10MB file within a very short time period is a thought fraught with&lt;br /&gt;absurdity and&lt;br /&gt;&lt;br /&gt;contrary to reason. Actually, what the Word program does is that it creates a&lt;br /&gt;thread and then&lt;br /&gt;&lt;br /&gt;solicits the thread to save the file. While the thread is saving that file,&lt;br /&gt;another thread waits&lt;br /&gt;&lt;br /&gt;for the user to type something. Hence, the user is emancipated to continue&lt;br /&gt;working with Word.&lt;br /&gt;&lt;br /&gt;Thus, in effect, two tasks are being executed concurrently without the user&lt;br /&gt;being cognizant of&lt;br /&gt;&lt;br /&gt;it ! The same mechanism is employed by Excel and other similar products.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Any application under Windows runs in its own thread. Thus, if two programs are&lt;br /&gt;launched, there&lt;br /&gt;&lt;br /&gt;will be two threads running. The operating system refrains from playing&lt;br /&gt;favorites and gives an&lt;br /&gt;&lt;br /&gt;equal amount of time to each thread to execute. If there are two threads&lt;br /&gt;co-existing, then, out&lt;br /&gt;&lt;br /&gt;of every minute of processing time available, each of them will be allotted 30&lt;br /&gt;seconds to&lt;br /&gt;&lt;br /&gt;execute. If a third program is now executed, a new thread will be launched and&lt;br /&gt;each of the three&lt;br /&gt;&lt;br /&gt;threads will be allotted only 20 seconds of the processor time per minute to&lt;br /&gt;execute.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;If, instead of running a third program, what if the second program itself&lt;br /&gt;creates a thread ? As&lt;br /&gt;&lt;br /&gt;before, this will result in the creation of a third thread and each thread will&lt;br /&gt;be allotted 20&lt;br /&gt;&lt;br /&gt;seconds of time. The resultant effect would be that that the first program will&lt;br /&gt;be allotted 20&lt;br /&gt;&lt;br /&gt;seconds of time, whereas the second program will get 40 seconds per minute of&lt;br /&gt;the processor&lt;br /&gt;&lt;br /&gt;time. Thus, the larger number of threads that a program creates, more will be&lt;br /&gt;the processor time&lt;br /&gt;&lt;br /&gt;allotted to it. If you crave for more time and attention from your computer,&lt;br /&gt;desist from&lt;br /&gt;&lt;br /&gt;throwing a tantrum. Instead, generate greater number of threads.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;The computer allots a specific amount of time to each thread and then puts it to&lt;br /&gt;sleep.&lt;br /&gt;&lt;br /&gt;Thereafter, it executes another thread. The time given to each thread to execute&lt;br /&gt;its code is&lt;br /&gt;&lt;br /&gt;designated as a Time Slice. This allocation of Time Slices occurs so swiftly&lt;br /&gt;that each thread&lt;br /&gt;&lt;br /&gt;suffers from the hallucination that it enjoys the undivided attention of the&lt;br /&gt;computer.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;The static function Sleep in the Thread class facilitates this process. Sleep is&lt;br /&gt;like the&lt;br /&gt;&lt;br /&gt;sandman. It puts the thread to sleep for a certain number of milliseconds, as&lt;br /&gt;specified in its&lt;br /&gt;&lt;br /&gt;parameter. In this case, it is 1 millisecond. In the earlier example, in a&lt;br /&gt;single time slice,&lt;br /&gt;&lt;br /&gt;the Thread executed all the code and the function completed execution. In this&lt;br /&gt;case, the Sleep&lt;br /&gt;&lt;br /&gt;delays it long enough for the next thread to execute, and thus, the code in the&lt;br /&gt;functions get&lt;br /&gt;&lt;br /&gt;called one after the other.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;a.cs&lt;br /&gt;&lt;br /&gt;using System.Threading;&lt;br /&gt;&lt;br /&gt;public class yyy&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;public void abc()&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;for ( int i = 0; i&amp;lt;=3;i++)&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;Thread t2 = Thread.CurrentThread;&lt;br /&gt;&lt;br /&gt;System.Console.Write(i + "." + t2.Name + " ");&lt;br /&gt;&lt;br /&gt;Thread.Sleep(1);&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;public class zzz&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;public static void Main()&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;yyy a = new yyy();&lt;br /&gt;&lt;br /&gt;Thread t = new Thread(new ThreadStart(a.abc));&lt;br /&gt;&lt;br /&gt;Thread t1 = new Thread(new ThreadStart(a.abc));&lt;br /&gt;&lt;br /&gt;t.Name="One";&lt;br /&gt;&lt;br /&gt;t1.Name="Two";&lt;br /&gt;&lt;br /&gt;t.Start();&lt;br /&gt;&lt;br /&gt;t1.Start();&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Output&lt;br /&gt;&lt;br /&gt;0.One 0.Two 1.One 1.Two 2.One 2.Two 3.One 3.Two&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Here, we have provided mental catharsis by simplifying the program. What we&lt;br /&gt;actually have done&lt;br /&gt;&lt;br /&gt;is given the same function name abc to our delegate. We have also used the&lt;br /&gt;property Name of the&lt;br /&gt;&lt;br /&gt;Thread class to assign a distinct name to each thread, i.e. One and Two&lt;br /&gt;respectively. Under&lt;br /&gt;&lt;br /&gt;normal circumstances, the names assigned are pre-ordained by the system. Now,&lt;br /&gt;both the threads&lt;br /&gt;&lt;br /&gt;will call the same function abc. How do we determine as to which thread is&lt;br /&gt;calling the function&lt;br /&gt;&lt;br /&gt;abc?&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;The Thread class can have members, which are either static or instance.&lt;br /&gt;CurrentThread is a&lt;br /&gt;&lt;br /&gt;static read-only property that returns a thread object, which represents the&lt;br /&gt;thread that has&lt;br /&gt;&lt;br /&gt;currently called the function. Here, Thread t2 will either represent t or t1,&lt;br /&gt;and the property&lt;br /&gt;&lt;br /&gt;Name will display the name of the thread executing the function.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;a.cs&lt;br /&gt;&lt;br /&gt;using System.Threading;&lt;br /&gt;&lt;br /&gt;public class zzz : Thread&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Compiler Error&lt;br /&gt;&lt;br /&gt;a.cs(2,14): error CS0509: 'zzz' : cannot inherit from sealed class&lt;br /&gt;'System.Threading.Thread'&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;The Thread class is a sealed class, therefore we cannot derive from it. The&lt;br /&gt;designers of the&lt;br /&gt;&lt;br /&gt;Thread class at Microsoft have held very doctrinaire opinions, in that, they&lt;br /&gt;believe that they&lt;br /&gt;&lt;br /&gt;have envisaged all the possible features required in this class. Thus, they have&lt;br /&gt;not provided&lt;br /&gt;&lt;br /&gt;any facility to override or modify it. Therefore, we are constrained to use the&lt;br /&gt;Thread class&lt;br /&gt;&lt;br /&gt;exactly as provided, without introducing any code that will complement or add to&lt;br /&gt;or subtract&lt;br /&gt;&lt;br /&gt;from the code of the Thread class. For your information, the Thread class in&lt;br /&gt;turn is derived&lt;br /&gt;&lt;br /&gt;from the interface ISerializable.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;a.cs&lt;br /&gt;&lt;br /&gt;using System.Threading;&lt;br /&gt;&lt;br /&gt;public class yyy&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;public void abc()&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;System.Console.WriteLine("Hi");&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;public class zzz&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;public static void Main()&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;yyy a = new yyy();&lt;br /&gt;&lt;br /&gt;Thread t = new Thread(new ThreadStart(a.abc));&lt;br /&gt;&lt;br /&gt;System.Console.WriteLine(t.IsAlive);&lt;br /&gt;&lt;br /&gt;t.Start();&lt;br /&gt;&lt;br /&gt;System.Console.WriteLine(t.IsAlive);&lt;br /&gt;&lt;br /&gt;t.Abort();&lt;br /&gt;&lt;br /&gt;System.Console.WriteLine(t.IsAlive);&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Output&lt;br /&gt;&lt;br /&gt;False&lt;br /&gt;&lt;br /&gt;True&lt;br /&gt;&lt;br /&gt;Hi&lt;br /&gt;&lt;br /&gt;False&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;The Thread class has a property called IsAlive that reveals the current state of&lt;br /&gt;the Thread.&lt;br /&gt;&lt;br /&gt;When we create an object that looks like Thread, the thread is in a dead state&lt;br /&gt;and hence, the&lt;br /&gt;&lt;br /&gt;IsAlive property has a value of False. Start breathes life into the thread and&lt;br /&gt;executes through&lt;br /&gt;&lt;br /&gt;the delegate abc. The thread now comes alive, but the code in the thread&lt;br /&gt;function gets executed&lt;br /&gt;&lt;br /&gt;only after the subsequent WriteLine function, which displays the value of&lt;br /&gt;IsAlive as True. When&lt;br /&gt;&lt;br /&gt;we stop or Abort the Thread, the thread dies and the value of the property&lt;br /&gt;IsAlive reverts back&lt;br /&gt;&lt;br /&gt;to False. Thus, IsAlive can hold only one of the two values, False for dead or&lt;br /&gt;True for alive.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;a.cs&lt;br /&gt;&lt;br /&gt;using System.Threading;&lt;br /&gt;&lt;br /&gt;public class yyy&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;public void abc()&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;System.Console.WriteLine("Hi");&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;public class zzz&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;public static void Main()&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;yyy a = new yyy();&lt;br /&gt;&lt;br /&gt;Thread t = new Thread(new ThreadStart(a.abc));&lt;br /&gt;&lt;br /&gt;t.Start();&lt;br /&gt;&lt;br /&gt;while ( !t.IsAlive )&lt;br /&gt;&lt;br /&gt;System.Console.WriteLine("hi1");&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Output&lt;br /&gt;&lt;br /&gt;Hi&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;In today's world, we are working on extremely fast machines. When we execute&lt;br /&gt;t.Start(), the&lt;br /&gt;&lt;br /&gt;thread is brought to life in a fraction of a second. Hence, the while loop is&lt;br /&gt;not executed&lt;br /&gt;&lt;br /&gt;because the condition becomes false immediately. Had the machine been slower,&lt;br /&gt;the while loop may&lt;br /&gt;&lt;br /&gt;have got called a few times, as it would have taken some time before the thread&lt;br /&gt;could have been&lt;br /&gt;&lt;br /&gt;brought to life. Thus, the speed of execution may vary, depending upon the speed&lt;br /&gt;of the machine.&lt;br /&gt;&lt;br /&gt;Whenever the sample code creates a thread, it always contains this while loop.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;a.cs&lt;br /&gt;&lt;br /&gt;using System.Threading;&lt;br /&gt;&lt;br /&gt;public class yyy&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;public void abc()&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;for ( int i = 0; i&amp;lt;=3 ; i++)&lt;br /&gt;&lt;br /&gt;System.Console.Write("Hi " + i + " ");&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;public class zzz&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;public static void Main()&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;yyy a = new yyy();&lt;br /&gt;&lt;br /&gt;Thread t = new Thread(new ThreadStart(a.abc));&lt;br /&gt;&lt;br /&gt;t.Start();&lt;br /&gt;&lt;br /&gt;System.Console.WriteLine("Over");&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Output&lt;br /&gt;&lt;br /&gt;Over&lt;br /&gt;&lt;br /&gt;Hi 0 Hi 1 Hi 2 Hi 3&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;The executable, a.exe, runs in its own thread. So, before calling the function&lt;br /&gt;Start, we already&lt;br /&gt;&lt;br /&gt;have one thread running. On calling the function Start, two threads run&lt;br /&gt;concurrently,&lt;br /&gt;&lt;br /&gt;independent of each other. The second thread executes the function abc&lt;br /&gt;independent of the first&lt;br /&gt;&lt;br /&gt;thread. The first thread executes the last WriteLine function and then stops,&lt;br /&gt;whereas the second&lt;br /&gt;&lt;br /&gt;thread continues executing the function abc till there is no more code to call.&lt;br /&gt;If, on your&lt;br /&gt;&lt;br /&gt;machine, the first thread's time slice gets over before executing the WriteLine&lt;br /&gt;function, then&lt;br /&gt;&lt;br /&gt;some code of the function abc may get executed before Over gets displayed in the&lt;br /&gt;function Main.&lt;br /&gt;&lt;br /&gt;It is reiterated yet again that, in case of threads, there can be no guarantee&lt;br /&gt;as to when the&lt;br /&gt;&lt;br /&gt;time slice of a thread will get over. Even the operating system is not sagacious&lt;br /&gt;enough to offer&lt;br /&gt;&lt;br /&gt;any guarantees in this regard.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;a.cs&lt;br /&gt;&lt;br /&gt;using System.Threading;&lt;br /&gt;&lt;br /&gt;public class yyy&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;public void abc()&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;for ( int i = 0; i&amp;lt;=3 ; i++)&lt;br /&gt;&lt;br /&gt;System.Console.Write("Hi " + i + " ");&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;public class zzz&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;public static void Main()&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;yyy a = new yyy();&lt;br /&gt;&lt;br /&gt;Thread t = new Thread(new ThreadStart(a.abc));&lt;br /&gt;&lt;br /&gt;t.Start();&lt;br /&gt;&lt;br /&gt;t.Join();&lt;br /&gt;&lt;br /&gt;System.Console.WriteLine("Over");&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;Output&lt;br /&gt;&lt;br /&gt;Hi 0 Hi 1 Hi 2 Hi 3 Over&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;The function Join is a blocking function. It makes the program linger at the&lt;br /&gt;function Join till&lt;br /&gt;&lt;br /&gt;the thread finishes execution. Any code after the Join, in this case the&lt;br /&gt;WriteLine function,&lt;br /&gt;&lt;br /&gt;will be executed only after the thread dies. A blocking function will wait until&lt;br /&gt;the purpose it&lt;br /&gt;&lt;br /&gt;is waiting for has reached fructification. Thus, if we want to wait for a thread&lt;br /&gt;to complete&lt;br /&gt;&lt;br /&gt;execution, we use a Join. Join behaves like a party host, who is always the last&lt;br /&gt;to leave.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;a.cs&lt;br /&gt;&lt;br /&gt;using System.Threading;&lt;br /&gt;&lt;br /&gt;public class yyy&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;public void abc()&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;for ( ; ; ) ;&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;public class zzz&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;public static void Main()&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;yyy a = new yyy();&lt;br /&gt;&lt;br /&gt;Thread t = new Thread(new ThreadStart(a.abc));&lt;br /&gt;&lt;br /&gt;t.Start();&lt;br /&gt;&lt;br /&gt;bool b = t.Join(10000);&lt;br /&gt;&lt;br /&gt;System.Console.WriteLine("Over " + b);&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Output&lt;br /&gt;&lt;br /&gt;Over False&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;The Join function accepts a number as a parameter, which represents the maximum&lt;br /&gt;duration in&lt;br /&gt;&lt;br /&gt;milliseconds that it should wait for the thread to complete execution. In our&lt;br /&gt;example, the&lt;br /&gt;&lt;br /&gt;function abc will never end. Thus, the thread t will go on forever. Therefore,&lt;br /&gt;the Join function&lt;br /&gt;&lt;br /&gt;waits for 10 seconds, gives up and finally returns a False.&lt;br /&gt;&lt;br /&gt;Therefore, we are empowered to decide the duration for which we want to wait for&lt;br /&gt;the thread to&lt;br /&gt;&lt;br /&gt;complete execution. Let us not forget that our application has not yet&lt;br /&gt;terminated and is still&lt;br /&gt;&lt;br /&gt;hanging around in memory. At this stage, if you press Ctrl-Alt-Del, you will see&lt;br /&gt;a list of&lt;br /&gt;&lt;br /&gt;programs running. Now, select the End Task option for the program called 'a'. An&lt;br /&gt;alternative&lt;br /&gt;&lt;br /&gt;approach could be to add the Abort function t.Abort() after the WriteLine&lt;br /&gt;function.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;a.cs&lt;br /&gt;&lt;br /&gt;using System.Threading;&lt;br /&gt;&lt;br /&gt;public class yyy&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;public void abc()&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;for ( ; ; ) ;&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;public class zzz&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;public static void Main()&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;yyy a = new yyy();&lt;br /&gt;&lt;br /&gt;Thread t = new Thread(new ThreadStart(a.abc));&lt;br /&gt;&lt;br /&gt;t.Join();&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Output&lt;br /&gt;&lt;br /&gt;Unhandled Exception: System.Threading.ThreadStateException: Thread has not been&lt;br /&gt;started.&lt;br /&gt;&lt;br /&gt;at System.Threading.Thread.Join()&lt;br /&gt;&lt;br /&gt;at zzz.Main()&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;If there is no thread running, the Join function will throw the exception of&lt;br /&gt;&lt;br /&gt;ThreadStateException.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;a.cs&lt;br /&gt;&lt;br /&gt;using System.Threading;&lt;br /&gt;&lt;br /&gt;public class yyy&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;public void abc()&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;System.Console.WriteLine("abc");&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;public class zzz&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;public static void Main()&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;yyy a = new yyy();&lt;br /&gt;&lt;br /&gt;Thread t = new Thread(new ThreadStart(a.abc));&lt;br /&gt;&lt;br /&gt;t.Start();&lt;br /&gt;&lt;br /&gt;Thread.Sleep(3);&lt;br /&gt;&lt;br /&gt;t.Abort();&lt;br /&gt;&lt;br /&gt;System.Console.WriteLine("Over");&lt;br /&gt;&lt;br /&gt;Thread.Sleep(100);&lt;br /&gt;&lt;br /&gt;t.Start();&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Output&lt;br /&gt;&lt;br /&gt;abc&lt;br /&gt;&lt;br /&gt;Over&lt;br /&gt;&lt;br /&gt;Unhandled Exception: System.Threading.ThreadStateException: Thread is running or&lt;br /&gt;terminated.&lt;br /&gt;&lt;br /&gt;Cannot restart.&lt;br /&gt;&lt;br /&gt;at System.Threading.Thread.StartInternal(IPrincipal principal, StackCrawlMark &amp;amp;&lt;br /&gt;stackMark)&lt;br /&gt;&lt;br /&gt;at System.Threading.Thread.Start()&lt;br /&gt;&lt;br /&gt;at zzz.Main()&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;At first, we start the thread t by calling the Start function. Thereafter, we&lt;br /&gt;make the main&lt;br /&gt;&lt;br /&gt;thread sleep for a little while. Then, we abort the thread and again sleep for a&lt;br /&gt;little while,&lt;br /&gt;&lt;br /&gt;in order to enable the thread to fulfill its last wishes and finally die. Now&lt;br /&gt;that the thread is&lt;br /&gt;&lt;br /&gt;dead, we try and infuse life into it by calling Start again. A thread, which has&lt;br /&gt;died cannot be&lt;br /&gt;&lt;br /&gt;resuscitated. Since we have had the audacity to attempt this, the results are&lt;br /&gt;nearly&lt;br /&gt;&lt;br /&gt;cataclysmic, resulting in the generation of an exception which has to be caught.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;a.cs&lt;br /&gt;&lt;br /&gt;using System.Threading;&lt;br /&gt;&lt;br /&gt;public class yyy&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;public void abc()&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;System.Console.WriteLine("abc");&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;public class zzz&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;public static void Main()&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;yyy a = new yyy();&lt;br /&gt;&lt;br /&gt;Thread t = new Thread(new ThreadStart(a.abc));&lt;br /&gt;&lt;br /&gt;t.Start();&lt;br /&gt;&lt;br /&gt;Thread.Sleep(3);&lt;br /&gt;&lt;br /&gt;t.Abort();&lt;br /&gt;&lt;br /&gt;System.Console.WriteLine("Over");&lt;br /&gt;&lt;br /&gt;Thread.Sleep(100);&lt;br /&gt;&lt;br /&gt;try&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;t.Start();&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;catch (ThreadStateException e)&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;System.Console.WriteLine("In Exception");&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Output&lt;br /&gt;&lt;br /&gt;abc&lt;br /&gt;&lt;br /&gt;Over&lt;br /&gt;&lt;br /&gt;In Exception&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;The exception thrown is ThreadStateException, which needs to be caught in our&lt;br /&gt;code. Otherwise,&lt;br /&gt;&lt;br /&gt;the runtime message box is displayed to the user.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;The Thread class constructor can throw two types of exceptions:-&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;• ArgumentNullException - When it is called without a delegate in the&lt;br /&gt;constructor.&lt;br /&gt;&lt;br /&gt;• SecurityException - When the program does not have permission to create a&lt;br /&gt;thread.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;a.cs&lt;br /&gt;&lt;br /&gt;using System.Threading;&lt;br /&gt;&lt;br /&gt;public class yyy&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;public void abc()&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;System.Console.WriteLine("abc");&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;public class zzz&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;public static void Main()&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;yyy a = new yyy();&lt;br /&gt;&lt;br /&gt;Thread t = new Thread(new ThreadStart(a.abc));&lt;br /&gt;&lt;br /&gt;System.Console.WriteLine(t.ApartmentState);&lt;br /&gt;&lt;br /&gt;t.ApartmentState = ApartmentState.STA;&lt;br /&gt;&lt;br /&gt;t.Start();&lt;br /&gt;&lt;br /&gt;System.Console.WriteLine(t.ApartmentState);&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Output&lt;br /&gt;&lt;br /&gt;Unknown&lt;br /&gt;&lt;br /&gt;STA&lt;br /&gt;&lt;br /&gt;abc&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;We all have our own apartments, which are normally done up as per our&lt;br /&gt;preferences i.e. 'We are&lt;br /&gt;&lt;br /&gt;the kings of our castles'. Threads also have apartments. Any thread can be asked&lt;br /&gt;to execute&lt;br /&gt;&lt;br /&gt;either in a single-threaded apartment or in a multi-threaded apartment. These&lt;br /&gt;values can be&lt;br /&gt;&lt;br /&gt;established using the property ApartmentState, either once at the beginning, or&lt;br /&gt;while the thread&lt;br /&gt;&lt;br /&gt;is running. The various values that ApartmentState can assume are as follows:&lt;br /&gt;&lt;br /&gt;• STA : Single Threaded Apartment.&lt;br /&gt;&lt;br /&gt;• MTA : Multi Threaded Apartment.&lt;br /&gt;&lt;br /&gt;• Unknown : Default value assigned when no value is set.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;We, however, cannot use numbers for a string. Thus, an enum called&lt;br /&gt;ApartmentState has been used.&lt;br /&gt;&lt;br /&gt;It holds three values i.e. 0, 1 and 2 corresponding to STA, MTA and Unknown&lt;br /&gt;respectively.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;a.cs&lt;br /&gt;&lt;br /&gt;using System.Threading;&lt;br /&gt;&lt;br /&gt;public class yyy&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;public void abc()&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;System.Console.WriteLine("abc " + Thread.CurrentThread.IsBackground);&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;public class zzz&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;public static void Main()&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;yyy a = new yyy();&lt;br /&gt;&lt;br /&gt;Thread t = new Thread(new ThreadStart(a.abc));&lt;br /&gt;&lt;br /&gt;System.Console.WriteLine("Main " + Thread.CurrentThread.IsBackground);&lt;br /&gt;&lt;br /&gt;t.Start();&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Output&lt;br /&gt;&lt;br /&gt;Main False&lt;br /&gt;&lt;br /&gt;abc False&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;A thread executes either in the background or in the foreground. The property&lt;br /&gt;IsBackground&lt;br /&gt;&lt;br /&gt;indicates the mode in which the thread will run. An important point to be noted&lt;br /&gt;here is that, a&lt;br /&gt;&lt;br /&gt;thread which executes in the background automatically shuts down when its main&lt;br /&gt;program quits.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;a.cs&lt;br /&gt;&lt;br /&gt;using System.Threading;&lt;br /&gt;&lt;br /&gt;public class yyy&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;public void abc()&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;for ( int i = 0 ; i&amp;lt;=100; i++)&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;System.Console.WriteLine("abc " + i);&lt;br /&gt;&lt;br /&gt;Thread.Sleep(1);&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;public class zzz&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;public static void Main()&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;yyy a = new yyy();&lt;br /&gt;&lt;br /&gt;Thread t = new Thread(new ThreadStart(a.abc));&lt;br /&gt;&lt;br /&gt;System.Console.WriteLine("Main " + Thread.CurrentThread.IsBackground);&lt;br /&gt;&lt;br /&gt;t.IsBackground = true;&lt;br /&gt;&lt;br /&gt;t.Start();&lt;br /&gt;&lt;br /&gt;Thread.Sleep(10);&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Output (t.IsBackground = true)&lt;br /&gt;&lt;br /&gt;Main False&lt;br /&gt;&lt;br /&gt;abc 0&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Output (t.IsBackground = false)&lt;br /&gt;&lt;br /&gt;Main False&lt;br /&gt;&lt;br /&gt;abc 0&lt;br /&gt;&lt;br /&gt;abc 1&lt;br /&gt;&lt;br /&gt;abc 2&lt;br /&gt;&lt;br /&gt;..&lt;br /&gt;&lt;br /&gt;..&lt;br /&gt;&lt;br /&gt;abc 100&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;When you change the true to false in t.IsBackground, the output shows the code&lt;br /&gt;of function abc&lt;br /&gt;&lt;br /&gt;being executed. The for loop displays values from 0 to 100.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;By changing the property IsBackground of a thread to true, the thread terminates&lt;br /&gt;when the main&lt;br /&gt;&lt;br /&gt;program stops. Thus, the for loop does not have the time to display values upto&lt;br /&gt;100. At times,&lt;br /&gt;&lt;br /&gt;the program displays abc with a value of 0.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;The sleeps are used to demonstrate the effect of the first thread shutting down&lt;br /&gt;and the second&lt;br /&gt;&lt;br /&gt;carrying on execution. On changing the property IsBackground to false, the&lt;br /&gt;thread takes its own&lt;br /&gt;&lt;br /&gt;sweet time to execute its code without any regard for the first thread, which&lt;br /&gt;may or may not&lt;br /&gt;&lt;br /&gt;have completed its job. So, the foreground threads behave exactly in the same&lt;br /&gt;manner as the&lt;br /&gt;&lt;br /&gt;background threads, when the property is initialized to true.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;a.cs&lt;br /&gt;&lt;br /&gt;using System.Threading;&lt;br /&gt;&lt;br /&gt;public class yyy&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;public void abc()&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;for ( int i = 0; i&amp;lt;=10;i++)&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;System.Console.Write(i + " ");&lt;br /&gt;&lt;br /&gt;Thread.Sleep(1);&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;public void pqr()&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;for ( int i = 0; i&amp;lt;=10;i++)&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;System.Console.Write(i+ "...");&lt;br /&gt;&lt;br /&gt;Thread.Sleep(1);&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;public class zzz&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;public static void Main()&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;yyy a = new yyy();&lt;br /&gt;&lt;br /&gt;Thread t = new Thread(new ThreadStart(a.abc));&lt;br /&gt;&lt;br /&gt;Thread t1 = new Thread(new ThreadStart(a.pqr));&lt;br /&gt;&lt;br /&gt;System.Console.WriteLine(t.Priority);&lt;br /&gt;&lt;br /&gt;t.Priority = ThreadPriority.Highest;&lt;br /&gt;&lt;br /&gt;t1.Priority = ThreadPriority.Lowest;&lt;br /&gt;&lt;br /&gt;t.Start();&lt;br /&gt;&lt;br /&gt;t1.Start();&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Output&lt;br /&gt;&lt;br /&gt;Normal&lt;br /&gt;&lt;br /&gt;0 1 2 3 4 0...5 1...6 2...7 3...8 4...9 5...10 6...7...8...9...10...&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;We are at liberty to decide the duration of the time-slice to be allotted to our&lt;br /&gt;thread&lt;br /&gt;&lt;br /&gt;vis-a-vis other threads. This can be achieved by setting the Priority property&lt;br /&gt;accordingly. By&lt;br /&gt;&lt;br /&gt;default, the priority level is 2. Hence, we see 'Normal' displayed as the&lt;br /&gt;output. All threads&lt;br /&gt;&lt;br /&gt;start at this priority level. The ThreadPriority enum comprises of five levels&lt;br /&gt;of priority,&lt;br /&gt;&lt;br /&gt;which are as follows:&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;• 0 - Zero&lt;br /&gt;&lt;br /&gt;• 1 - BelowNormal&lt;br /&gt;&lt;br /&gt;• 2 - Normal&lt;br /&gt;&lt;br /&gt;• 3 - AboveNormal&lt;br /&gt;&lt;br /&gt;• 4 - Highest&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;In our program, we have changed the priority of thread t to the highest possible&lt;br /&gt;and that of&lt;br /&gt;&lt;br /&gt;thread t1 to the lowest. As a result, Thread t is accorded more time than thread&lt;br /&gt;t1, and it&lt;br /&gt;&lt;br /&gt;finishes its for loop much before thread t1 can do so.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;a.cs&lt;br /&gt;&lt;br /&gt;using System.Threading;&lt;br /&gt;&lt;br /&gt;public class yyy&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;public void abc()&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;try&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;System.Console.WriteLine("in abc");&lt;br /&gt;&lt;br /&gt;for ( ; ; ) ;&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;catch ( System.Exception e)&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;System.Console.WriteLine("in abc Exception " +e.ToString() );&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;finally&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;System.Console.WriteLine("in abc Finally");&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;public class zzz&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;public static void Main()&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;yyy a = new yyy();&lt;br /&gt;&lt;br /&gt;Thread t = new Thread(new ThreadStart(a.abc));&lt;br /&gt;&lt;br /&gt;t.Start();&lt;br /&gt;&lt;br /&gt;Thread.Sleep(10);&lt;br /&gt;&lt;br /&gt;t.Abort();&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Output&lt;br /&gt;&lt;br /&gt;in abc&lt;br /&gt;&lt;br /&gt;in abc Exception System.Threading.ThreadAbortException: Thread was being&lt;br /&gt;aborted.&lt;br /&gt;&lt;br /&gt;at yyy.abc()&lt;br /&gt;&lt;br /&gt;in abc Finally&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Aborting a Thread while it is busy executing some code generates an exception.&lt;br /&gt;Thus, the&lt;br /&gt;&lt;br /&gt;function abc will be sent an exception, resulting in a call to the 'catch' and&lt;br /&gt;'finally'&lt;br /&gt;&lt;br /&gt;clauses. The thread will be terminated after executing the code in the Catch and&lt;br /&gt;the Finally&lt;br /&gt;&lt;br /&gt;clause.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;a.cs&lt;br /&gt;&lt;br /&gt;using System.Threading;&lt;br /&gt;&lt;br /&gt;public class yyy&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;public void abc()&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;try&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;System.Console.WriteLine("in abc");&lt;br /&gt;&lt;br /&gt;for ( ; ; ) ;&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;catch ( System.Exception e)&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;System.Console.WriteLine("in abc Exception " +e.ToString() );&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;finally&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;System.Console.WriteLine("in abc Finally");&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;public class zzz&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;public static void Main()&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;yyy a = new yyy();&lt;br /&gt;&lt;br /&gt;Thread t = new Thread(new ThreadStart(a.abc));&lt;br /&gt;&lt;br /&gt;t.Abort();&lt;br /&gt;&lt;br /&gt;t.Start();&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Output&lt;br /&gt;&lt;br /&gt;(no output)&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Aborting a thread before it has started execution does not generate any&lt;br /&gt;exceptions, since the&lt;br /&gt;&lt;br /&gt;thread is in a dead state. Hence, no code in abc ever gets called. This&lt;br /&gt;situation is akin to the&lt;br /&gt;&lt;br /&gt;one where you don your best attire to watch cricket live at the stadium, but the&lt;br /&gt;show gets&lt;br /&gt;&lt;br /&gt;ruined due to heavy downpour, resulting in the match being abandoned without a&lt;br /&gt;ball being bowled&lt;br /&gt;&lt;br /&gt;!&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;a.cs&lt;br /&gt;&lt;br /&gt;using System.Threading;&lt;br /&gt;&lt;br /&gt;public class yyy&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;public void abc()&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;for (int i = 1 ; i&amp;lt;= 10 ; i++ )&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;System.Console.Write(i + " " );&lt;br /&gt;&lt;br /&gt;Thread.Sleep(1);&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;public class zzz&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;public static void Main()&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;yyy a = new yyy();&lt;br /&gt;&lt;br /&gt;Thread t = new Thread(new ThreadStart(a.abc));&lt;br /&gt;&lt;br /&gt;t.Start();&lt;br /&gt;&lt;br /&gt;Thread.Sleep(10);&lt;br /&gt;&lt;br /&gt;t.Suspend();&lt;br /&gt;&lt;br /&gt;System.Console.WriteLine("\n After Suspend");&lt;br /&gt;&lt;br /&gt;Thread.Sleep(10);&lt;br /&gt;&lt;br /&gt;System.Console.WriteLine("Before Resume");&lt;br /&gt;&lt;br /&gt;t.Resume();&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Output&lt;br /&gt;&lt;br /&gt;1&lt;br /&gt;&lt;br /&gt;After Suspend&lt;br /&gt;&lt;br /&gt;Before Resume&lt;br /&gt;&lt;br /&gt;2 3 4 5 6 7 8 9 10&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;We are allowed to Suspend a thread executing its code, at any point in time. In&lt;br /&gt;the above&lt;br /&gt;&lt;br /&gt;program, we let the thread run for a little while and then Suspend it.&lt;br /&gt;Thereafter, we Resume it.&lt;br /&gt;&lt;br /&gt;Thus, the thread is like a puppet in our hands. We can decide when and which&lt;br /&gt;strings to pull and&lt;br /&gt;&lt;br /&gt;the thread does our biding. If the thread has already been suspended, then&lt;br /&gt;Suspending it once&lt;br /&gt;&lt;br /&gt;again has no effect, and thus, it does not result in an error. A single Resume&lt;br /&gt;is enough to undo&lt;br /&gt;&lt;br /&gt;a million Suspends.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;a.cs&lt;br /&gt;&lt;br /&gt;using System.Threading;&lt;br /&gt;&lt;br /&gt;public class yyy&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;public void abc()&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;for ( int i = 0; i&amp;lt;=3;i++)&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;Thread t2 = Thread.CurrentThread;&lt;br /&gt;&lt;br /&gt;System.Console.Write(i + " " + t2.GetHashCode() + " ");&lt;br /&gt;&lt;br /&gt;Thread.Sleep(1);&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;public class zzz&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;public static void Main()&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;yyy a = new yyy();&lt;br /&gt;&lt;br /&gt;Thread t = new Thread(new ThreadStart(a.abc));&lt;br /&gt;&lt;br /&gt;Thread t1 = new Thread(new ThreadStart(a.abc));&lt;br /&gt;&lt;br /&gt;t.Start();&lt;br /&gt;&lt;br /&gt;t1.Start();&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Output&lt;br /&gt;&lt;br /&gt;0 2 0 3 1 2 1 3 2 2 2 3 3 2 3 3&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Every thread is assigned a number internally. This number is called the&lt;br /&gt;HashCode. Hashing is the&lt;br /&gt;&lt;br /&gt;process of assigning a number to an entity so that it can easily be located from&lt;br /&gt;a long list.&lt;br /&gt;&lt;br /&gt;This hash number is useful and is pressed into service when we are required to&lt;br /&gt;keep track of a&lt;br /&gt;&lt;br /&gt;large number of threads internally.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;a.cs&lt;br /&gt;&lt;br /&gt;using System.Threading;&lt;br /&gt;&lt;br /&gt;public class yyy&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;public void abc()&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;Monitor.Enter(this);&lt;br /&gt;&lt;br /&gt;for ( int i = 1; i&amp;lt;=5;i++)&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;System.Console.Write(i + " " + Thread.CurrentThread.GetHashCode() + " ");&lt;br /&gt;&lt;br /&gt;Thread.Sleep(1000);&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;Monitor.Exit(this);&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;public class zzz&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;public static void Main()&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;yyy a = new yyy();&lt;br /&gt;&lt;br /&gt;Thread t = new Thread(new ThreadStart(a.abc));&lt;br /&gt;&lt;br /&gt;Thread t1 = new Thread(new ThreadStart(a.abc));&lt;br /&gt;&lt;br /&gt;t.Start();&lt;br /&gt;&lt;br /&gt;t1.Start();&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Output&lt;br /&gt;&lt;br /&gt;1 2 2 2 3 2 4 2 5 2 1 3 2 3 3 3 4 3 5 3&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Let us now clarify the abstract concept of the life of a thread with the help of&lt;br /&gt;an example.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Any resource that many people desire is called a shared resource. Imagine the&lt;br /&gt;chaos that would&lt;br /&gt;&lt;br /&gt;be caused if five programs tried to simultaneously use a single network printer&lt;br /&gt;for printing,&lt;br /&gt;&lt;br /&gt;during their respectively time-slices. This would result in the printer printing&lt;br /&gt;a few pages for&lt;br /&gt;&lt;br /&gt;program 1, then a few pages for program 2, and so on. The printer will keep&lt;br /&gt;printing merrily,&lt;br /&gt;&lt;br /&gt;but the printed output will all be jumbled up.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Thus, ideally, one program should use a shared resource at a time, till its job&lt;br /&gt;is complete. For&lt;br /&gt;&lt;br /&gt;this, a method has to be implemented by means of which, unless one program&lt;br /&gt;finishes executing&lt;br /&gt;&lt;br /&gt;some specific code in a function, no other program should be allowed to execute&lt;br /&gt;it. Thus, all&lt;br /&gt;&lt;br /&gt;other programs would have to wait until the first thread finishes execution.&lt;br /&gt;&lt;br /&gt;Let us assume that function abc contains code which sends a request to the&lt;br /&gt;printer to print. The&lt;br /&gt;&lt;br /&gt;thread t gets the first shot at running abc. The second thread t1 has to wait&lt;br /&gt;until thread t&lt;br /&gt;&lt;br /&gt;finishes execution, even if the duration of executing abc takes more time than&lt;br /&gt;one time slice,&lt;br /&gt;&lt;br /&gt;due to a large value being assigned as a parameter to the sleep function. This&lt;br /&gt;is achieved by&lt;br /&gt;&lt;br /&gt;using the static function Enter of the class Monitor.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;The function Enter is given an object which refers to the class in which the&lt;br /&gt;function resides.&lt;br /&gt;&lt;br /&gt;From that point onwards, every thread has to wait at Enter. The barrier lowers&lt;br /&gt;only when Exit is&lt;br /&gt;&lt;br /&gt;called from the Monitor class. Enter acts as a fussy security guard who decides&lt;br /&gt;as to which&lt;br /&gt;&lt;br /&gt;thread is to be prohibited and which one is to be permitted to execute code. No&lt;br /&gt;two threads can&lt;br /&gt;&lt;br /&gt;ever enter the Enter function together. It is a rigid one way lane from Enter to&lt;br /&gt;Exit. One car&lt;br /&gt;&lt;br /&gt;is allowed in at one time and another car can pass through only after the first&lt;br /&gt;car leaves Exit.&lt;br /&gt;&lt;br /&gt;You can see that the names Enter and Exit have been chosen very aptly.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;a.cs&lt;br /&gt;&lt;br /&gt;using System;&lt;br /&gt;&lt;br /&gt;using System.Threading;&lt;br /&gt;&lt;br /&gt;public class ggg&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;};&lt;br /&gt;&lt;br /&gt;public class yyy&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;public ggg g1;&lt;br /&gt;&lt;br /&gt;public void abc( )&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;Monitor.Enter(g1);&lt;br /&gt;&lt;br /&gt;for ( int i = 1; i &amp;lt;= 3; i++)&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;Console.Write(" Hell " + i);&lt;br /&gt;&lt;br /&gt;Monitor.Wait(g1);&lt;br /&gt;&lt;br /&gt;Console.Write(" Waitabc " + i);&lt;br /&gt;&lt;br /&gt;Monitor.Pulse(g1);&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;Monitor.Exit(g1);&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;};&lt;br /&gt;&lt;br /&gt;public class xxx&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;public ggg g1;&lt;br /&gt;&lt;br /&gt;public void pqr( )&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;Monitor.Enter(g1);&lt;br /&gt;&lt;br /&gt;for ( int i = 1; i &amp;lt;= 3; i++)&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;Console.WriteLine(" Everyone " + i);&lt;br /&gt;&lt;br /&gt;Monitor.Pulse(g1);&lt;br /&gt;&lt;br /&gt;Monitor.Wait(g1);&lt;br /&gt;&lt;br /&gt;Console.Write(" Waitpqr " + i);&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;Monitor.Exit(g1);&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;};&lt;br /&gt;&lt;br /&gt;public class zzz&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;public static void Main(String[] args)&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;ggg g = new ggg( );&lt;br /&gt;&lt;br /&gt;yyy a = new yyy( );&lt;br /&gt;&lt;br /&gt;a.g1 = g;&lt;br /&gt;&lt;br /&gt;Thread t = new Thread(new ThreadStart(a.abc));&lt;br /&gt;&lt;br /&gt;t.Start( );&lt;br /&gt;&lt;br /&gt;xxx b = new xxx();&lt;br /&gt;&lt;br /&gt;b.g1 = g;&lt;br /&gt;&lt;br /&gt;Thread t1 = new Thread(new ThreadStart(b.pqr));&lt;br /&gt;&lt;br /&gt;t1.Start( );&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;};&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Output&lt;br /&gt;&lt;br /&gt;Hell 1 Everyone 1&lt;br /&gt;&lt;br /&gt;Waitabc 1 Hell 2 Waitpqr 1 Everyone 2&lt;br /&gt;&lt;br /&gt;Waitabc 2 Hell 3 Waitpqr 2 Everyone 3&lt;br /&gt;&lt;br /&gt;cs Waitabc 3 Waitpqr 3&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;The above example may be a protracted one, yet it is fascinating.&lt;br /&gt;&lt;br /&gt;The world has never been able to work with each other. The only global body we&lt;br /&gt;have is the UNO.&lt;br /&gt;&lt;br /&gt;People are mostly loners.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;In the entry point function Main, we create an object g, which is an instance of&lt;br /&gt;a class ggg,&lt;br /&gt;&lt;br /&gt;having no code or variable. It embodies the emptiness of space. This is a class&lt;br /&gt;that is&lt;br /&gt;&lt;br /&gt;incapable of doing any good or bad. We then create two objects that look like&lt;br /&gt;yyy and xxx&lt;br /&gt;&lt;br /&gt;respectively, and pass them as delegates, so that the threads can call functions&lt;br /&gt;abc and pqr. We&lt;br /&gt;&lt;br /&gt;also initialize the object g1, an instance of ggg, in each of them to g. This is&lt;br /&gt;crucial. The&lt;br /&gt;&lt;br /&gt;g1's in yyy and xxx represent the same g that is created in Main.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;After initializing the requisite members, we activate the two threads. As&lt;br /&gt;earlier, let us assume&lt;br /&gt;&lt;br /&gt;that the function abc gets called first.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Monitor.Enter considers taking a handle to any object, which is to be&lt;br /&gt;synchronized between&lt;br /&gt;&lt;br /&gt;multiple instances of threads, as its first parameter. This function needs an&lt;br /&gt;Exit function to&lt;br /&gt;&lt;br /&gt;release its handle. Thus, the number of Exits in the program must correspond to&lt;br /&gt;the number of&lt;br /&gt;&lt;br /&gt;Enters.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;It prints "Hell", and then due to the Wait function, it waits indefinitely, till&lt;br /&gt;it finally&lt;br /&gt;&lt;br /&gt;receives runtime notification. However, the thread accomplishes nothing&lt;br /&gt;significant. Thus, we do&lt;br /&gt;&lt;br /&gt;not see the Waitabc displayed, since it is entered after the Wait function.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;The second thread starts executing the function pqr. Monitor.Enter uses the same&lt;br /&gt;synchronized&lt;br /&gt;&lt;br /&gt;object, and now it displays Everyone. The next function to be called in pqr is&lt;br /&gt;Monitor.Pulse.&lt;br /&gt;&lt;br /&gt;Pulse does a very simple thing. It wakes up any thread having the same handle,&lt;br /&gt;which happens to&lt;br /&gt;&lt;br /&gt;be waiting at a Wait. Thus, the thread in abc wakes up. However, the one in pqr&lt;br /&gt;waits for&lt;br /&gt;&lt;br /&gt;someone to Pulse it.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;By using Wait and Pulse, we can easily play a game of tag. The thread t performs&lt;br /&gt;some work and&lt;br /&gt;&lt;br /&gt;waits for thread t1 to wrap up whatever work it is preoccupied with. Thread t&lt;br /&gt;does not know how&lt;br /&gt;&lt;br /&gt;long thread t1 will take to fulfill its side of the bargain. But no sweat!&lt;br /&gt;Thread t waits at a&lt;br /&gt;&lt;br /&gt;Wait for the thread t1 to Pulse it. Then, thread t1 waits at a Wait for thread t&lt;br /&gt;to do some&lt;br /&gt;&lt;br /&gt;work. When thread t concludes, it Pulses thread t1, and so on. This can go on&lt;br /&gt;and may involve&lt;br /&gt;&lt;br /&gt;some other threads too.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;The threads use the handle g, which is the synchronized region. It is akin to&lt;br /&gt;being an integral&lt;br /&gt;&lt;br /&gt;part of a clandestine social group. The parameter to these Monitor functions&lt;br /&gt;clubs them together&lt;br /&gt;&lt;br /&gt;as a group. If you change the handle, the group breaks up, leaving your thread&lt;br /&gt;in a lurch. The&lt;br /&gt;&lt;br /&gt;Wait function can also be made to wait for a specified time period.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;If we change the line Monitor.Wait(g1) to Monitor.Wait(g1,0), the output changes&lt;br /&gt;dramatically.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;A monitor is associated with an object on demand and it cannot be instantiated&lt;br /&gt;at all. If the&lt;br /&gt;&lt;br /&gt;constructor is private, we can never create an object. But, the static functions&lt;br /&gt;of that object&lt;br /&gt;&lt;br /&gt;can be used. The Monitor functions can be called from anywhere since they are&lt;br /&gt;unbound. There are&lt;br /&gt;&lt;br /&gt;no restrictions on the sources of the object to be called.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;a.cs&lt;br /&gt;&lt;br /&gt;using System.Threading;&lt;br /&gt;&lt;br /&gt;public class yyy&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;public void abc()&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;Monitor.Enter(this);&lt;br /&gt;&lt;br /&gt;Monitor.Enter(this);&lt;br /&gt;&lt;br /&gt;for ( int i = 1; i&amp;lt;=3;i++)&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;System.Console.WriteLine(i + " " + Thread.CurrentThread.GetHashCode() + " ");&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;Monitor.Exit(this);&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;public class zzz&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;public static void Main() {&lt;br /&gt;&lt;br /&gt;yyy a = new yyy();&lt;br /&gt;&lt;br /&gt;Thread t = new Thread(new ThreadStart(a.abc));&lt;br /&gt;&lt;br /&gt;Thread t1 = new Thread(new ThreadStart(a.abc));&lt;br /&gt;&lt;br /&gt;t.Start();&lt;br /&gt;&lt;br /&gt;t1.Start();&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Output&lt;br /&gt;&lt;br /&gt;1 2&lt;br /&gt;&lt;br /&gt;2 2&lt;br /&gt;&lt;br /&gt;3 2&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;The Exit function can be called as often as one desires. In this program, we&lt;br /&gt;have two threads, t&lt;br /&gt;&lt;br /&gt;and t1. Let us assume that thread t has the first go. At this time, it will pass&lt;br /&gt;through the&lt;br /&gt;&lt;br /&gt;first Monitor.Enter, as well as the second Monitor.Enter, without waiting at&lt;br /&gt;all. However,&lt;br /&gt;&lt;br /&gt;thread t1 will have to wait at the first Monitor.Enter, since the thread t&lt;br /&gt;called the Enter&lt;br /&gt;&lt;br /&gt;function twice, but Exited only once. Thus, the program never quits out. Had it&lt;br /&gt;called Exit&lt;br /&gt;&lt;br /&gt;twice, the thread t1 too would have woken up.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;a.cs&lt;br /&gt;&lt;br /&gt;using System.Threading;&lt;br /&gt;&lt;br /&gt;public class yyy {&lt;br /&gt;&lt;br /&gt;public int j=1;&lt;br /&gt;&lt;br /&gt;public void abc()&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;Monitor.Enter(j);&lt;br /&gt;&lt;br /&gt;for ( int i = 1; i&amp;lt;=3;i++)&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;System.Console.WriteLine(i + " " + Thread.CurrentThread.GetHashCode() + " ");&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;Monitor.Exit(j);&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;public class zzz&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;public static void Main() {&lt;br /&gt;&lt;br /&gt;yyy a = new yyy();&lt;br /&gt;&lt;br /&gt;Thread t = new Thread(new ThreadStart(a.abc));&lt;br /&gt;&lt;br /&gt;t.Start();&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Output&lt;br /&gt;&lt;br /&gt;1 2&lt;br /&gt;&lt;br /&gt;2 2&lt;br /&gt;&lt;br /&gt;3 2&lt;br /&gt;&lt;br /&gt;Unhandled Exception: System.Threading.SynchronizationLockException: Exception of&lt;br /&gt;type&lt;br /&gt;&lt;br /&gt;System.Threading.SynchronizationLockException was thrown.&lt;br /&gt;&lt;br /&gt;at yyy.abc()&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Monitor.Enter takes an object, and not a variable, as a parameter. This is the&lt;br /&gt;reason why we&lt;br /&gt;&lt;br /&gt;used this parameter or some reference object, in the earlier programs. If a&lt;br /&gt;variable or a value&lt;br /&gt;&lt;br /&gt;object is used, an exception will be thrown. Thus, value types or null objects&lt;br /&gt;are not allowed&lt;br /&gt;&lt;br /&gt;as parameters to Monitor.Enter.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;a.cs&lt;br /&gt;&lt;br /&gt;using System.Threading;&lt;br /&gt;&lt;br /&gt;public class xxx&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;public class ttt&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;public class yyy&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;static int p = 1;&lt;br /&gt;&lt;br /&gt;xxx x = new xxx();&lt;br /&gt;&lt;br /&gt;ttt t = new ttt();&lt;br /&gt;&lt;br /&gt;public void abc()&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;if ( p == 1)&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;Monitor.Enter(x);&lt;br /&gt;&lt;br /&gt;p++;&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;else&lt;br /&gt;&lt;br /&gt;Monitor.Enter(t);&lt;br /&gt;&lt;br /&gt;for ( int i = 1; i&amp;lt;=3;i++)&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;Thread.Sleep(1000);&lt;br /&gt;&lt;br /&gt;System.Console.WriteLine(i + " " + Thread.CurrentThread.GetHashCode() + " ");&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;Monitor.Exit(x);&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;public class zzz&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;public static void Main()&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;yyy a = new yyy();&lt;br /&gt;&lt;br /&gt;Thread t = new Thread(new ThreadStart(a.abc));&lt;br /&gt;&lt;br /&gt;Thread t1 = new Thread(new ThreadStart(a.abc));&lt;br /&gt;&lt;br /&gt;t.Start();&lt;br /&gt;&lt;br /&gt;t1.Start();&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Output&lt;br /&gt;&lt;br /&gt;1 2&lt;br /&gt;&lt;br /&gt;1 3&lt;br /&gt;&lt;br /&gt;2 2&lt;br /&gt;&lt;br /&gt;2 3&lt;br /&gt;&lt;br /&gt;3 2&lt;br /&gt;&lt;br /&gt;3 3&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;The Enter function must be used with some caution. The variable p initially has&lt;br /&gt;a value of 1.&lt;br /&gt;&lt;br /&gt;The first thread t sees the value of p as 1. Hence, the 'if' statement is true.&lt;br /&gt;Thus, it crosses&lt;br /&gt;&lt;br /&gt;Monitor.Enter with x as a parameter. It then takes a snooze at the Sleep&lt;br /&gt;function, while the&lt;br /&gt;&lt;br /&gt;second thread t1 executes function abc, where it sees the value of variable p as&lt;br /&gt;2. This thread&lt;br /&gt;&lt;br /&gt;now meets the Monitor.Enter function, with t as the parameter. The object&lt;br /&gt;reference given to&lt;br /&gt;&lt;br /&gt;this Enter is entirely at variance with the object given to the earlier one.&lt;br /&gt;Thus, both the&lt;br /&gt;&lt;br /&gt;threads execute the Enter function, which fails the very objective of the Enter&lt;br /&gt;function. For&lt;br /&gt;&lt;br /&gt;Enter to work as expected, it should be provided with the same object as a&lt;br /&gt;parameter to itself.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;The concept of a Lock is normally used to explain the Monitor class. One thread&lt;br /&gt;gets a Lock,&lt;br /&gt;&lt;br /&gt;while the others wait until the lock is released.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;a.cs&lt;br /&gt;&lt;br /&gt;using System.Threading;&lt;br /&gt;&lt;br /&gt;public class yyy&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;public void abc()&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;bool b = Monitor.TryEnter(this);&lt;br /&gt;&lt;br /&gt;System.Console.WriteLine(b);&lt;br /&gt;&lt;br /&gt;for ( int i = 1; i&amp;lt;=3;i++)&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;Thread.Sleep(1000);&lt;br /&gt;&lt;br /&gt;System.Console.WriteLine(i + " " + Thread.CurrentThread.GetHashCode() + " ");&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;Monitor.Exit(this);&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;public class zzz&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;public static void Main() {&lt;br /&gt;&lt;br /&gt;yyy a = new yyy();&lt;br /&gt;&lt;br /&gt;Thread t = new Thread(new ThreadStart(a.abc));&lt;br /&gt;&lt;br /&gt;Thread t1 = new Thread(new ThreadStart(a.abc));&lt;br /&gt;&lt;br /&gt;t.Start();&lt;br /&gt;&lt;br /&gt;t1.Start();&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Output&lt;br /&gt;&lt;br /&gt;True&lt;br /&gt;&lt;br /&gt;False&lt;br /&gt;&lt;br /&gt;1 2&lt;br /&gt;&lt;br /&gt;1 3&lt;br /&gt;&lt;br /&gt;2 2&lt;br /&gt;&lt;br /&gt;2 3&lt;br /&gt;&lt;br /&gt;3 2&lt;br /&gt;&lt;br /&gt;3 3&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;The TryEnter function is similar to the Enter function, but it does not block.&lt;br /&gt;If the thread&lt;br /&gt;&lt;br /&gt;enters successfully, TryEnter returns a true. This is what happens the first&lt;br /&gt;time. But in the&lt;br /&gt;&lt;br /&gt;case of the second thread t, it returns a false, even when it enters the&lt;br /&gt;critical section.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;a.cs&lt;br /&gt;&lt;br /&gt;using System.Threading;&lt;br /&gt;&lt;br /&gt;public class yyy&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;public void abc()&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;bool b = Monitor.TryEnter(this,1000);&lt;br /&gt;&lt;br /&gt;System.Console.WriteLine(b);&lt;br /&gt;&lt;br /&gt;for ( int i = 1; i&amp;lt;=3;i++)&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;Thread.Sleep(1000);&lt;br /&gt;&lt;br /&gt;System.Console.WriteLine(i + " " + Thread.CurrentThread.GetHashCode() + " ");&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;Monitor.Exit(this);&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;public class zzz&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;public static void Main()&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;yyy a = new yyy();&lt;br /&gt;&lt;br /&gt;Thread t = new Thread(new ThreadStart(a.abc));&lt;br /&gt;&lt;br /&gt;Thread t1 = new Thread(new ThreadStart(a.abc));&lt;br /&gt;&lt;br /&gt;t.Start();&lt;br /&gt;&lt;br /&gt;t1.Start();&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Output&lt;br /&gt;&lt;br /&gt;True&lt;br /&gt;&lt;br /&gt;False&lt;br /&gt;&lt;br /&gt;1 2&lt;br /&gt;&lt;br /&gt;1 3&lt;br /&gt;&lt;br /&gt;2 2&lt;br /&gt;&lt;br /&gt;2 3&lt;br /&gt;&lt;br /&gt;3 2&lt;br /&gt;&lt;br /&gt;3 3&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;The TryEnter function is overloaded to accept a number as a parameter, which&lt;br /&gt;represents the time&lt;br /&gt;&lt;br /&gt;duration for which the TryEnter function should block or wait. In our case, the&lt;br /&gt;timeout exceeds&lt;br /&gt;&lt;br /&gt;the time that thread t spends in the function. Hence, we get a value of False.&lt;br /&gt;If we add an&lt;br /&gt;&lt;br /&gt;extra zero to the time parameter, it will return a true instead. If the value of&lt;br /&gt;the parameter&lt;br /&gt;&lt;br /&gt;is infinite, the behaviour of the TryEnter becomes akin to that of Enter.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Thread Attributes&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;a.cs&lt;br /&gt;&lt;br /&gt;using System.Threading;&lt;br /&gt;&lt;br /&gt;public class yyy&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;[System.ThreadStaticAttribute ()]&lt;br /&gt;&lt;br /&gt;public static int j = 1;&lt;br /&gt;&lt;br /&gt;public static int k = 1;&lt;br /&gt;&lt;br /&gt;public void abc()&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;for ( int i = 1; i&amp;lt;=3;i++)&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;Thread t2 = Thread.CurrentThread;&lt;br /&gt;&lt;br /&gt;j++;k++;&lt;br /&gt;&lt;br /&gt;System.Console.Write(i + "." + t2.Name + " j=" + j + " k=" + k + " ");&lt;br /&gt;&lt;br /&gt;Thread.Sleep(1);&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;public class zzz&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;public static void Main() {&lt;br /&gt;&lt;br /&gt;yyy a = new yyy();&lt;br /&gt;&lt;br /&gt;Thread t = new Thread(new ThreadStart(a.abc));&lt;br /&gt;&lt;br /&gt;Thread t1 = new Thread(new ThreadStart(a.abc));&lt;br /&gt;&lt;br /&gt;t.Name="One";&lt;br /&gt;&lt;br /&gt;t1.Name="Two";&lt;br /&gt;&lt;br /&gt;t.Start();&lt;br /&gt;&lt;br /&gt;t1.Start();&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;Output&lt;br /&gt;&lt;br /&gt;1.One j=1 k=2 1.Two j=1 k=3 2.One j=2 k=4 2.Two j=2 k=5 3.One j=3 k=6 3.Two j=3&lt;br /&gt;k=7&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;A static variable is a loner, as reiterated by us on numerous occasions. It&lt;br /&gt;belongs to the class&lt;br /&gt;&lt;br /&gt;and not to an object. Thus, there will always be a single static variable.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Let us consider a case where we have two threads t and t1 executing code in the&lt;br /&gt;same function&lt;br /&gt;&lt;br /&gt;abc, that also contains static variables j and k. The first question that&lt;br /&gt;strikes us is 'Will&lt;br /&gt;&lt;br /&gt;each of the threads see the same static variable or a different one?' Also, if&lt;br /&gt;we assume that&lt;br /&gt;&lt;br /&gt;both threads cater to the same static variable, when one thread increments the&lt;br /&gt;variable, will&lt;br /&gt;&lt;br /&gt;the other thread see the new value? And if not, then, is the compiler creating a&lt;br /&gt;separate static&lt;br /&gt;&lt;br /&gt;variable for each thread?&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Too many questions that need answers!&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;We want to possess the best of both the worlds. At times, we need the threads to&lt;br /&gt;work on the&lt;br /&gt;&lt;br /&gt;same static variable, while at other times, we need the threads to access&lt;br /&gt;separate copies of the&lt;br /&gt;&lt;br /&gt;static variable. The default behaviour is that of the static variable being&lt;br /&gt;shared. Thus, the&lt;br /&gt;&lt;br /&gt;variable k is incremented by both the threads, and each thread sees the same&lt;br /&gt;value. The value of&lt;br /&gt;&lt;br /&gt;k finally reaches 7 at the end.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;The variable j has an attribute ThreadStaticAttribute above it. This attribute&lt;br /&gt;may have a fancy&lt;br /&gt;&lt;br /&gt;name, but it merely creates a separate variable for each thread executing the&lt;br /&gt;function. Thus, we&lt;br /&gt;&lt;br /&gt;have two static variables j, one for each thread t and t1. Any changes made to&lt;br /&gt;the value of the&lt;br /&gt;&lt;br /&gt;variable j by t, does not get reflected in the static variable j of the thread&lt;br /&gt;t1.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;1.One j=2 k=2 1.Two j=3 k=3 2.Two j=4 k=4 2.One j=5 k=5 3.Two j=6 k=6 3.One j=7&lt;br /&gt;k=7&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;The above attribute only acts on static variables. We see the above output when&lt;br /&gt;we remove static&lt;br /&gt;&lt;br /&gt;from the variables j and k. The compiler is very clear. The attribute&lt;br /&gt;ThreadStaticAttribute is&lt;br /&gt;&lt;br /&gt;only to be used on static variables, but when applied on non-static variables,&lt;br /&gt;the compiler does&lt;br /&gt;&lt;br /&gt;not generate any error or warning. It simply ignores the attribute. Thus,&lt;br /&gt;instance variables are&lt;br /&gt;&lt;br /&gt;shared across threads, whereas, local variables like i are independent of&lt;br /&gt;threads. Thus, a&lt;br /&gt;&lt;br /&gt;variable can either be shared across threads or may be independent of the&lt;br /&gt;thread. No in-between&lt;br /&gt;&lt;br /&gt;options are allowed.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Events&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;a.cs&lt;br /&gt;&lt;br /&gt;using System.Threading;&lt;br /&gt;&lt;br /&gt;public class zzz&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;public static void Main() {&lt;br /&gt;&lt;br /&gt;ManualResetEvent a;&lt;br /&gt;&lt;br /&gt;a = new ManualResetEvent(false) ;&lt;br /&gt;&lt;br /&gt;System.Console.WriteLine("Before WaitOne " );&lt;br /&gt;&lt;br /&gt;bool b = a.WaitOne(1000,false);&lt;br /&gt;&lt;br /&gt;System.Console.WriteLine("After WaitOne " + b);&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Output&lt;br /&gt;&lt;br /&gt;Before WaitOne&lt;br /&gt;&lt;br /&gt;After WaitOne False&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;We have created an object a that is an instance of a ManualResetEvent class. The&lt;br /&gt;constructor is&lt;br /&gt;&lt;br /&gt;assigned a value of false. Then, we call a function WaitOne from this class with&lt;br /&gt;a number as the&lt;br /&gt;&lt;br /&gt;first parameter, and a boolean value as the second parameter. This number, 1000,&lt;br /&gt;is the number&lt;br /&gt;&lt;br /&gt;of milliseconds for which we want the thread to wait at the function WaitOne.&lt;br /&gt;The thread&lt;br /&gt;&lt;br /&gt;therefore waits for one second before quitting out, and then returns a False.&lt;br /&gt;Had we used the&lt;br /&gt;&lt;br /&gt;enum Timeout.Infinite that has a value of -1, we could have made the function&lt;br /&gt;wait forever.&lt;br /&gt;&lt;br /&gt;Thus, we can keep a thread waiting either for a specified duration of time or&lt;br /&gt;forever.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;a.cs&lt;br /&gt;&lt;br /&gt;using System.Threading;&lt;br /&gt;&lt;br /&gt;public class zzz&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;public static void Main()&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;ManualResetEvent a;&lt;br /&gt;&lt;br /&gt;a = new ManualResetEvent(true) ;&lt;br /&gt;&lt;br /&gt;System.Console.WriteLine("Before WaitOne " );&lt;br /&gt;&lt;br /&gt;bool b = a.WaitOne(1000,false);&lt;br /&gt;&lt;br /&gt;System.Console.WriteLine("After WaitOne " + b);&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Output&lt;br /&gt;&lt;br /&gt;Before WaitOne&lt;br /&gt;&lt;br /&gt;After WaitOne True&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;By changing the value in the constructor to true, the thread just refuses to&lt;br /&gt;wait for anyone. It&lt;br /&gt;&lt;br /&gt;just whizzes past, without even acknowledging the presence of the function&lt;br /&gt;WaitOne. Further, the&lt;br /&gt;&lt;br /&gt;return value of WaitOne i.e. b returns a True.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Let us delve a little deeper into this mystery. A ManualResetEvent is like a&lt;br /&gt;boolean variable.&lt;br /&gt;&lt;br /&gt;It can possess only one of the two values, true or false. When we first created&lt;br /&gt;such an object,&lt;br /&gt;&lt;br /&gt;we gave it a value of false. So, the function WaitOne waited till the Event&lt;br /&gt;object turned into&lt;br /&gt;&lt;br /&gt;True or the time value expired. Since the time duration got over while waiting,&lt;br /&gt;and the value of&lt;br /&gt;&lt;br /&gt;the Event object was not set to True, it stopped blocking and returned with a&lt;br /&gt;value of false.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;In the next example, the event object already has a value true and hence, there&lt;br /&gt;is no wait.&lt;br /&gt;&lt;br /&gt;Here, the function WaitOne returns true because the non-blocking in this case,&lt;br /&gt;cannot be&lt;br /&gt;&lt;br /&gt;attributed to a timeout. Thus, a ManualResetEvent has two states, true or false;&lt;br /&gt;or in technical&lt;br /&gt;&lt;br /&gt;lingo, signaled or non-signaled. A value of true implies a signaled state, while&lt;br /&gt;a false would&lt;br /&gt;&lt;br /&gt;mean that it is in the non-signaled state.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;a.cs&lt;br /&gt;&lt;br /&gt;using System.Threading;&lt;br /&gt;&lt;br /&gt;public class zzz&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;public static void Main()&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;ManualResetEvent a;&lt;br /&gt;&lt;br /&gt;a = new ManualResetEvent(true) ;&lt;br /&gt;&lt;br /&gt;System.Console.WriteLine("Before WaitOne " );&lt;br /&gt;&lt;br /&gt;bool b = a.WaitOne(1000,true);&lt;br /&gt;&lt;br /&gt;System.Console.WriteLine("After WaitOne " + b);&lt;br /&gt;&lt;br /&gt;b = a.WaitOne(10000,true);&lt;br /&gt;&lt;br /&gt;System.Console.WriteLine("After second WaitOne " + b);&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Output&lt;br /&gt;&lt;br /&gt;Before WaitOne&lt;br /&gt;&lt;br /&gt;After WaitOne True&lt;br /&gt;&lt;br /&gt;After second WaitOne True&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Since we do not consider ourselves to be very cerebral, we shall desist from&lt;br /&gt;using fancy&lt;br /&gt;&lt;br /&gt;terminology like signaled or non-signaled. Instead, we shall stick to the simple&lt;br /&gt;true and false.&lt;br /&gt;&lt;br /&gt;We initially created the object a to be true or signaled (Oops! There we go&lt;br /&gt;again!). It then&lt;br /&gt;&lt;br /&gt;passed the first and the second WaitOne without waiting. This occurred because&lt;br /&gt;it is a Manual&lt;br /&gt;&lt;br /&gt;ResetEvent (as the name itself suggests), and not automatic. Once it is set to&lt;br /&gt;true, the event&lt;br /&gt;&lt;br /&gt;cannot change to false automatically. This has to be accomplished manually.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;a.cs&lt;br /&gt;&lt;br /&gt;using System.Threading;&lt;br /&gt;&lt;br /&gt;public class zzz&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;public static void Main()&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;ManualResetEvent a;&lt;br /&gt;&lt;br /&gt;a = new ManualResetEvent(true) ;&lt;br /&gt;&lt;br /&gt;System.Console.WriteLine("Before WaitOne " );&lt;br /&gt;&lt;br /&gt;bool b = a.WaitOne(1000,true);&lt;br /&gt;&lt;br /&gt;System.Console.WriteLine("After WaitOne " + b);&lt;br /&gt;&lt;br /&gt;a.Reset();&lt;br /&gt;&lt;br /&gt;b = a.WaitOne(10000,true);&lt;br /&gt;&lt;br /&gt;System.Console.WriteLine("After second WaitOne " + b);&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;Output&lt;br /&gt;&lt;br /&gt;Before WaitOne&lt;br /&gt;&lt;br /&gt;After WaitOne True&lt;br /&gt;&lt;br /&gt;After second WaitOne False&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;The Reset function can be used to change the state from true to false as it&lt;br /&gt;changes the state to&lt;br /&gt;&lt;br /&gt;false. Hence, it has to wait in queue like a commoner, since the event ignores&lt;br /&gt;the earlier&lt;br /&gt;&lt;br /&gt;WaitOne. After some time, it gets activated due to a timeout and the function&lt;br /&gt;WaitOne returns a&lt;br /&gt;&lt;br /&gt;False.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;a.cs&lt;br /&gt;&lt;br /&gt;using System.Threading;&lt;br /&gt;&lt;br /&gt;public class zzz&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;public static void Main()&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;ManualResetEvent a;&lt;br /&gt;&lt;br /&gt;a = new ManualResetEvent(false) ;&lt;br /&gt;&lt;br /&gt;System.Console.WriteLine("Before WaitOne " );&lt;br /&gt;&lt;br /&gt;a.Set();&lt;br /&gt;&lt;br /&gt;bool b = a.WaitOne(1000,true);&lt;br /&gt;&lt;br /&gt;System.Console.WriteLine("After WaitOne " + b);&lt;br /&gt;&lt;br /&gt;b = a.WaitOne(10000,true);&lt;br /&gt;&lt;br /&gt;System.Console.WriteLine("After second WaitOne " + b);&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Output&lt;br /&gt;&lt;br /&gt;Before WaitOne&lt;br /&gt;&lt;br /&gt;After WaitOne True&lt;br /&gt;&lt;br /&gt;After second WaitOne True&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Now, despite the fact that that the Event is originally false or non-signaled,&lt;br /&gt;the Set function&lt;br /&gt;&lt;br /&gt;sets it to true or the signaled state. At this stage, no power on earth is&lt;br /&gt;equipped to stop the&lt;br /&gt;&lt;br /&gt;event as it surges past the WaitOnes, treating them with disdain. It is not in&lt;br /&gt;the least&lt;br /&gt;&lt;br /&gt;concerned about stopping-by to pay homage to them. Such arrogance !&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;The class ManualResetEvent is a sealed class, like most other classes belonging&lt;br /&gt;to the Thread&lt;br /&gt;&lt;br /&gt;family. The developers at Microsoft were of the belief that no one would be able&lt;br /&gt;to add any more&lt;br /&gt;&lt;br /&gt;value to the class, and thus forbade any changes. This class is also derived&lt;br /&gt;from another class&lt;br /&gt;&lt;br /&gt;called WaitHandle, which in fact, embodies functions like WaitOne etc. The&lt;br /&gt;constructor has to be&lt;br /&gt;&lt;br /&gt;passed a parameter that affirms the initial state of the Event object. No&lt;br /&gt;default state is&lt;br /&gt;&lt;br /&gt;available. The Set and Reset methods return a boolean value, which indicates&lt;br /&gt;whether the change&lt;br /&gt;&lt;br /&gt;has taken place successfully or otherwise.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;WaitHandle is a class from which ManualResetEvent is derived. A class that&lt;br /&gt;offers functions&lt;br /&gt;&lt;br /&gt;which are polite, is called a synchronization class. People who are polite, wait&lt;br /&gt;for others to&lt;br /&gt;&lt;br /&gt;finish doing their work, and it is only then that they proceed with their own&lt;br /&gt;work. Any class&lt;br /&gt;&lt;br /&gt;that waits for another is called a synchronization class. All synchronization&lt;br /&gt;objects derive&lt;br /&gt;&lt;br /&gt;from WaitHandle, and so does ManualResetEvent.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;We are cautioned not to use this class directly, since it is the other classes&lt;br /&gt;that use this&lt;br /&gt;&lt;br /&gt;class. The documentation very clearly specifies that the CLR i.e. the Common&lt;br /&gt;Language Runtime or&lt;br /&gt;&lt;br /&gt;code written by Microsoft, calls the constructor of WaitHandle. Therefore, do&lt;br /&gt;not use this class&lt;br /&gt;&lt;br /&gt;directly. You are being forewarned!&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;a.cs&lt;br /&gt;&lt;br /&gt;using System.Threading;&lt;br /&gt;&lt;br /&gt;public class zzz&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;public static void Main()&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;ManualResetEvent a,b;&lt;br /&gt;&lt;br /&gt;a = new ManualResetEvent(false) ;&lt;br /&gt;&lt;br /&gt;b = new ManualResetEvent(true) ;&lt;br /&gt;&lt;br /&gt;WaitHandle [] w = new WaitHandle[2];&lt;br /&gt;&lt;br /&gt;w[0] = a; w[1] = b;&lt;br /&gt;&lt;br /&gt;System.Console.WriteLine("Before WaitAll " );&lt;br /&gt;&lt;br /&gt;bool c = WaitHandle.WaitAll(w,10000,true);&lt;br /&gt;&lt;br /&gt;System.Console.WriteLine("After WaitAll " + c);&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;Output&lt;br /&gt;&lt;br /&gt;Before WaitAll&lt;br /&gt;&lt;br /&gt;After WaitAll False&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;We can complicate life as much as we like. Similarly, we can complicate the&lt;br /&gt;WaitOne, by&lt;br /&gt;&lt;br /&gt;introducing its elder brother, WaitAll. This function will wait for all the&lt;br /&gt;event objects to&lt;br /&gt;&lt;br /&gt;become true or signaled, or it will stay there till the timeout occurs. In this&lt;br /&gt;manner, life can&lt;br /&gt;&lt;br /&gt;be made as complicated as we like. Until all the events reach the signaled&lt;br /&gt;state, the WaitAll&lt;br /&gt;&lt;br /&gt;remains extremely stubborn and does not budge from its vantage position. It does&lt;br /&gt;not take&lt;br /&gt;&lt;br /&gt;cognizance if a few of the events are signaled. All the events should be&lt;br /&gt;signaled. This could be&lt;br /&gt;&lt;br /&gt;a number approaching infinity.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;a.cs&lt;br /&gt;&lt;br /&gt;using System.Threading;&lt;br /&gt;&lt;br /&gt;public class zzz&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;public static void Main()&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;ManualResetEvent a,b;&lt;br /&gt;&lt;br /&gt;a = new ManualResetEvent(false) ;&lt;br /&gt;&lt;br /&gt;b = new ManualResetEvent(true) ;&lt;br /&gt;&lt;br /&gt;WaitHandle [] w = new WaitHandle[2];&lt;br /&gt;&lt;br /&gt;w[0] = a; w[1] = b;&lt;br /&gt;&lt;br /&gt;System.Console.WriteLine("Before WaitAll " );&lt;br /&gt;&lt;br /&gt;bool c = WaitHandle.WaitAll(w);&lt;br /&gt;&lt;br /&gt;System.Console.WriteLine("After WaitAll " + c);&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Output&lt;br /&gt;&lt;br /&gt;Before WaitAll&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;The number of functions in the class WaitHandle may be numerous, but most of&lt;br /&gt;them are only&lt;br /&gt;&lt;br /&gt;helper functions, which make life tranquil. The WaitAll function, if called with&lt;br /&gt;a single&lt;br /&gt;&lt;br /&gt;parameter, assumes that the second parameter is Timeout.Infinite or -1. These&lt;br /&gt;overloads can be&lt;br /&gt;&lt;br /&gt;ignored safely. It is the same for the WaitOne method.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;a.cs&lt;br /&gt;&lt;br /&gt;using System.Threading;&lt;br /&gt;&lt;br /&gt;public class zzz&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;public static void Main()&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;ManualResetEvent a,b;&lt;br /&gt;&lt;br /&gt;a = new ManualResetEvent(false) ;&lt;br /&gt;&lt;br /&gt;b = new ManualResetEvent(true) ;&lt;br /&gt;&lt;br /&gt;WaitHandle [] w = new WaitHandle[2];&lt;br /&gt;&lt;br /&gt;w[0] = a; w[1] = a;&lt;br /&gt;&lt;br /&gt;System.Console.WriteLine("Before WaitAll " );&lt;br /&gt;&lt;br /&gt;bool c = WaitHandle.WaitAll(w);&lt;br /&gt;&lt;br /&gt;System.Console.WriteLine("After WaitAll " + c);&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Output&lt;br /&gt;&lt;br /&gt;Before WaitAll&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Unhandled Exception: System.DuplicateWaitObjectException: Exception of type&lt;br /&gt;&lt;br /&gt;System.DuplicateWaitObjectException was thrown.&lt;br /&gt;&lt;br /&gt;at System.Threading.WaitHandle.WaitMultiple(WaitHandle[] waitHandles, Int32&lt;br /&gt;&lt;br /&gt;millisecondsTimeout, Boolean exitContext, Boolean WaitAll)&lt;br /&gt;&lt;br /&gt;at System.Threading.WaitHandle.WaitAll(WaitHandle[] waitHandles, Int32&lt;br /&gt;millisecondsTimeout,&lt;br /&gt;&lt;br /&gt;Boolean exitContext)&lt;br /&gt;&lt;br /&gt;at System.Threading.WaitHandle.WaitAll(WaitHandle[] waitHandles)&lt;br /&gt;&lt;br /&gt;at zzz.Main()&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;The CLR (Common Language Runtime, if you have forgotten) never sleeps, but the&lt;br /&gt;compiler can be&lt;br /&gt;&lt;br /&gt;caught dozing at times. C# hates duplicates, and thus, we cannot pass the same&lt;br /&gt;handle to the&lt;br /&gt;&lt;br /&gt;WaitHandle array. If you attempt to do so, a runtime exception will be thrown&lt;br /&gt;even when there&lt;br /&gt;&lt;br /&gt;was no reason to throw an exception. The runtime could have simply ignored the&lt;br /&gt;duplicate, but it&lt;br /&gt;&lt;br /&gt;chose not to.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;a.cs&lt;br /&gt;&lt;br /&gt;using System.Threading;&lt;br /&gt;&lt;br /&gt;public class zzz&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;public static void Main()&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;ManualResetEvent a,b;&lt;br /&gt;&lt;br /&gt;a = new ManualResetEvent(false) ;&lt;br /&gt;&lt;br /&gt;b = new ManualResetEvent(true) ;&lt;br /&gt;&lt;br /&gt;WaitHandle [] w = new WaitHandle[2];&lt;br /&gt;&lt;br /&gt;w[0] = b; w[1] = a;&lt;br /&gt;&lt;br /&gt;System.Console.WriteLine("Before WaitAll " );&lt;br /&gt;&lt;br /&gt;int c = WaitHandle.WaitAny(w);&lt;br /&gt;&lt;br /&gt;System.Console.WriteLine("After WaitAll " + c);&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Output&lt;br /&gt;&lt;br /&gt;Before WaitAll&lt;br /&gt;&lt;br /&gt;After WaitAll 0&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Change line containing w[0] = b; w[1] = a; to w[0] = a; w[1] = b; and the output&lt;br /&gt;changes to the&lt;br /&gt;&lt;br /&gt;following:-&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Output&lt;br /&gt;&lt;br /&gt;Before WaitAll&lt;br /&gt;&lt;br /&gt;After WaitAll 1&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;The WaitAny function waits till any of the events change to signaled or true.&lt;br /&gt;When this occurs,&lt;br /&gt;&lt;br /&gt;it returns the array index of the event which caused the exit. In this case,&lt;br /&gt;object b is in the&lt;br /&gt;&lt;br /&gt;signaled state, and since it is the first member of the array, the return value&lt;br /&gt;is 0. On&lt;br /&gt;&lt;br /&gt;interchanging the object to b, which is the second array member, the return&lt;br /&gt;value becomes 1. If&lt;br /&gt;&lt;br /&gt;both events are in the signaled state, then the lower array index is returned.&lt;br /&gt;If a timeout&lt;br /&gt;&lt;br /&gt;occurs, the value returned is 258.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;AutoResetEvent Class&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;a.cs&lt;br /&gt;&lt;br /&gt;using System.Threading;&lt;br /&gt;&lt;br /&gt;public class zzz&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;public static void Main()&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;AutoResetEvent a;&lt;br /&gt;&lt;br /&gt;a = new AutoResetEvent(false) ;&lt;br /&gt;&lt;br /&gt;System.Console.WriteLine("Before WaitAll " );&lt;br /&gt;&lt;br /&gt;bool c = a.WaitOne(1000,true);&lt;br /&gt;&lt;br /&gt;System.Console.WriteLine("After WaitOne " + c);&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Output&lt;br /&gt;&lt;br /&gt;Before WaitAll&lt;br /&gt;&lt;br /&gt;After WaitOne False&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Let us now move up the value chain. The AutoResetEvent class works in the same&lt;br /&gt;way like a&lt;br /&gt;&lt;br /&gt;ManualResetEvent class in the above example. It simply waits for the timeout to&lt;br /&gt;take place or&lt;br /&gt;&lt;br /&gt;the Event to be signaled.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;a.cs&lt;br /&gt;&lt;br /&gt;using System.Threading;&lt;br /&gt;&lt;br /&gt;public class zzz&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;public static void Main() {&lt;br /&gt;&lt;br /&gt;AutoResetEvent a;&lt;br /&gt;&lt;br /&gt;a = new AutoResetEvent(true) ;&lt;br /&gt;&lt;br /&gt;System.Console.WriteLine("Before WaitAll " );&lt;br /&gt;&lt;br /&gt;bool c = a.WaitOne(1000,true);&lt;br /&gt;&lt;br /&gt;System.Console.WriteLine("After WaitOne " + c);&lt;br /&gt;&lt;br /&gt;c = a.WaitOne(10000,true);&lt;br /&gt;&lt;br /&gt;System.Console.WriteLine("After WaitOne1 " + c);&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Output&lt;br /&gt;&lt;br /&gt;Before WaitAll&lt;br /&gt;&lt;br /&gt;After WaitOne True&lt;br /&gt;&lt;br /&gt;After WaitOne1 False&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Ah! Again the wait. The AutoResetEvent object safely crosses the first WaitOne.&lt;br /&gt;But there is one&lt;br /&gt;&lt;br /&gt;major variation now in that, the state of the object changes from true to false&lt;br /&gt;or signaled to&lt;br /&gt;&lt;br /&gt;non-signaled. Thus, it is obliged to pay homage for some time at the second&lt;br /&gt;Wait. The name here&lt;br /&gt;&lt;br /&gt;is Auto and not Manual. Just as we would change color if we saw a wild boar&lt;br /&gt;charging at us, the&lt;br /&gt;&lt;br /&gt;AutoResetEvent changes state at the Wait. Everything else remains identical.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Mutexes&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;a.cs&lt;br /&gt;&lt;br /&gt;using System.Threading;&lt;br /&gt;&lt;br /&gt;public class zzz&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;static Mutex m;&lt;br /&gt;&lt;br /&gt;public static void Main()&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;m = new Mutex(true,"vijay");&lt;br /&gt;&lt;br /&gt;zzz a=new zzz( );&lt;br /&gt;&lt;br /&gt;Thread t= new Thread(new ThreadStart(a.abc));&lt;br /&gt;&lt;br /&gt;t.Start();&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;public void abc( )&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;System.Console.WriteLine("abc start");&lt;br /&gt;&lt;br /&gt;m.WaitOne( );&lt;br /&gt;&lt;br /&gt;System.Console.WriteLine("abc end");&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Output&lt;br /&gt;&lt;br /&gt;abc start&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;If the boolean value of true in the Mutex constructor is changed to false, the&lt;br /&gt;output will&lt;br /&gt;&lt;br /&gt;change to the following:-&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Output&lt;br /&gt;&lt;br /&gt;abc start&lt;br /&gt;&lt;br /&gt;abc end&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;A mutex is a synchronization object derived from the class WaitHandle. A Mutex&lt;br /&gt;is, in some&lt;br /&gt;&lt;br /&gt;sense, similar to the other Event classes.&lt;br /&gt;&lt;br /&gt;We commence by creating a mutex and assigning it a boolean value of either true&lt;br /&gt;or false. In&lt;br /&gt;&lt;br /&gt;this context, these values have a slightly different connotation. They decide&lt;br /&gt;whether the&lt;br /&gt;&lt;br /&gt;creating thread owns the object or not. True allows the thread to own it,&lt;br /&gt;whereas False does&lt;br /&gt;&lt;br /&gt;not. Thus, with a mutex, we need to become very materialistic and start taking&lt;br /&gt;ownership of&lt;br /&gt;&lt;br /&gt;things. The current thread owns the mutex while the other threads are inclined&lt;br /&gt;to own it.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;True or false, signaled or non-signaled, own it or not, are all the same&lt;br /&gt;concepts. The main&lt;br /&gt;&lt;br /&gt;thread already owns the mutex, which is named as vijay. The name is optional and&lt;br /&gt;is used mostly&lt;br /&gt;&lt;br /&gt;to refer to the mutex. Thereafter, we create a thread which calls the function&lt;br /&gt;abc. After&lt;br /&gt;&lt;br /&gt;displaying a message, the function WaitOne is called. This function will now&lt;br /&gt;stake a claim for&lt;br /&gt;&lt;br /&gt;ownership of the mutex, but since the main thread owns it, thread t cannot&lt;br /&gt;proceed beyond this&lt;br /&gt;&lt;br /&gt;function until the original thread gives up ownership. If we change the True in&lt;br /&gt;the constructor&lt;br /&gt;&lt;br /&gt;to False, then the ownership is not claimed. Thus, the thread t will not have to&lt;br /&gt;wait at the&lt;br /&gt;&lt;br /&gt;WaitOne as there is no owner.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;a.cs&lt;br /&gt;&lt;br /&gt;using System.Threading;&lt;br /&gt;&lt;br /&gt;public class zzz&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;static Mutex m;&lt;br /&gt;&lt;br /&gt;public static void Main()&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;m = new Mutex(true,"vijay");&lt;br /&gt;&lt;br /&gt;zzz a=new zzz( );&lt;br /&gt;&lt;br /&gt;Thread t= new Thread(new ThreadStart(a.abc));&lt;br /&gt;&lt;br /&gt;t.Start();&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;public void abc( )&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;System.Console.WriteLine("abc start");&lt;br /&gt;&lt;br /&gt;m.ReleaseMutex();&lt;br /&gt;&lt;br /&gt;m.WaitOne( );&lt;br /&gt;&lt;br /&gt;System.Console.WriteLine("abc end");&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;Output&lt;br /&gt;&lt;br /&gt;abc start&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Unhandled Exception: System.ApplicationException: Attempt to release mutex not&lt;br /&gt;owned by caller.&lt;br /&gt;&lt;br /&gt;at System.Threading.Mutex.ReleaseMutexNative(IntPtr handle)&lt;br /&gt;&lt;br /&gt;at System.Threading.Mutex.ReleaseMutex()&lt;br /&gt;&lt;br /&gt;at zzz.abc()&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;The main thread becomes the owner of the mutex as the boolean value supplied in&lt;br /&gt;the mutex&lt;br /&gt;&lt;br /&gt;constructor is True. Then, in the function abc, we try to release the ownership&lt;br /&gt;of the mutex by&lt;br /&gt;&lt;br /&gt;using the function ReleaseMutex. The thread t does not own the mutex. Hence, we&lt;br /&gt;get a runtime&lt;br /&gt;&lt;br /&gt;error as only the owner i.e. the main thread, can release the ownership of a&lt;br /&gt;mutex and pass it&lt;br /&gt;&lt;br /&gt;on to another thread.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;a.cs&lt;br /&gt;&lt;br /&gt;using System.Threading;&lt;br /&gt;&lt;br /&gt;public class zzz&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;static Mutex m;&lt;br /&gt;&lt;br /&gt;public static void Main() {&lt;br /&gt;&lt;br /&gt;m = new Mutex(true,"vijay");&lt;br /&gt;&lt;br /&gt;zzz a=new zzz( );&lt;br /&gt;&lt;br /&gt;Thread t= new Thread(new ThreadStart(a.abc));&lt;br /&gt;&lt;br /&gt;t.Start();&lt;br /&gt;&lt;br /&gt;System.Console.WriteLine("Before Thread Sleep");&lt;br /&gt;&lt;br /&gt;Thread.Sleep(10000);&lt;br /&gt;&lt;br /&gt;System.Console.WriteLine("After Thread Sleep");&lt;br /&gt;&lt;br /&gt;m.ReleaseMutex();&lt;br /&gt;&lt;br /&gt;System.Console.WriteLine("All over");&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;public void abc( )&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;System.Console.WriteLine("abc start");&lt;br /&gt;&lt;br /&gt;m.WaitOne( );&lt;br /&gt;&lt;br /&gt;System.Console.WriteLine("abc end");&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Output&lt;br /&gt;&lt;br /&gt;Before Thread Sleep&lt;br /&gt;&lt;br /&gt;abc start&lt;br /&gt;&lt;br /&gt;After Thread Sleep&lt;br /&gt;&lt;br /&gt;abc end&lt;br /&gt;&lt;br /&gt;All over&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Now alls well that ends well. The main thread sleeps for a little while and then&lt;br /&gt;calls&lt;br /&gt;&lt;br /&gt;ReleaseMutex. This awakens the thread t, and thus, the function abc now quits&lt;br /&gt;out. Remember that&lt;br /&gt;&lt;br /&gt;you cannot give away what you do not own.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;a.cs&lt;br /&gt;&lt;br /&gt;using System.Threading;&lt;br /&gt;&lt;br /&gt;public class zzz&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;static Mutex m;&lt;br /&gt;&lt;br /&gt;public static void Main()&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;m = new Mutex(true,"vijay");&lt;br /&gt;&lt;br /&gt;zzz a=new zzz( );&lt;br /&gt;&lt;br /&gt;Thread t= new Thread(new ThreadStart(a.abc));&lt;br /&gt;&lt;br /&gt;t.Start();&lt;br /&gt;&lt;br /&gt;System.Console.WriteLine("Before Thread Sleep");&lt;br /&gt;&lt;br /&gt;Thread.Sleep(1000);&lt;br /&gt;&lt;br /&gt;System.Console.WriteLine("After Thread Sleep");&lt;br /&gt;&lt;br /&gt;m.ReleaseMutex();&lt;br /&gt;&lt;br /&gt;System.Console.WriteLine("Before WaitOne");&lt;br /&gt;&lt;br /&gt;m.WaitOne();&lt;br /&gt;&lt;br /&gt;System.Console.WriteLine("All over");&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;public void abc( )&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;System.Console.WriteLine("abc start");&lt;br /&gt;&lt;br /&gt;m.WaitOne();&lt;br /&gt;&lt;br /&gt;System.Console.WriteLine("abc before sleep");&lt;br /&gt;&lt;br /&gt;Thread.Sleep(100000);&lt;br /&gt;&lt;br /&gt;System.Console.WriteLine("abc end");&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Output&lt;br /&gt;&lt;br /&gt;Before Thread Sleep&lt;br /&gt;&lt;br /&gt;abc start&lt;br /&gt;&lt;br /&gt;After Thread Sleep&lt;br /&gt;&lt;br /&gt;abc before sleep&lt;br /&gt;&lt;br /&gt;Before WaitOne&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;There is a long sleep in the function abc. The mutex is still owned by the&lt;br /&gt;thread t, while the&lt;br /&gt;&lt;br /&gt;main thread is trying to regain its ownership. It can only gain ownership if the&lt;br /&gt;thread t&lt;br /&gt;&lt;br /&gt;releases the mutex or dies. Thus, until the thread t dies, we will not see All&lt;br /&gt;Over displayed at&lt;br /&gt;&lt;br /&gt;all. We could have had another sleep in abc and we could have released the mutex&lt;br /&gt;before that&lt;br /&gt;&lt;br /&gt;sleep. The choice is ours. Thus, a mutex is fully concerned with ownership&lt;br /&gt;issues. Either you&lt;br /&gt;&lt;br /&gt;own a mutex or you do not.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;a.cs&lt;br /&gt;&lt;br /&gt;using System.Threading;&lt;br /&gt;&lt;br /&gt;public class zzz&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;static Mutex m;&lt;br /&gt;&lt;br /&gt;static Mutex n;&lt;br /&gt;&lt;br /&gt;public static void Main()&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;m = new Mutex(true,"vijay");&lt;br /&gt;&lt;br /&gt;n = new Mutex(true);&lt;br /&gt;&lt;br /&gt;zzz a=new zzz( );&lt;br /&gt;&lt;br /&gt;Thread t= new Thread(new ThreadStart(a.abc));&lt;br /&gt;&lt;br /&gt;t.Start();&lt;br /&gt;&lt;br /&gt;System.Console.WriteLine("All over");&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;public void abc( )&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;System.Console.WriteLine("abc start");&lt;br /&gt;&lt;br /&gt;Mutex [] g = new Mutex[2];&lt;br /&gt;&lt;br /&gt;g[0] = m;g[1] = n;&lt;br /&gt;&lt;br /&gt;Mutex.WaitAll(g);&lt;br /&gt;&lt;br /&gt;System.Console.WriteLine("abc end");&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Output&lt;br /&gt;&lt;br /&gt;All over&lt;br /&gt;&lt;br /&gt;abc start&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;The program will hang. This is because we have two mutexses that have to be&lt;br /&gt;waited upon. Both m&lt;br /&gt;&lt;br /&gt;and n fill up the array g, and since both are true, the function abc will wait&lt;br /&gt;forever. Same&lt;br /&gt;&lt;br /&gt;rules of event classes apply to a mutex object.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;a.cs&lt;br /&gt;&lt;br /&gt;using System.Threading;&lt;br /&gt;&lt;br /&gt;public class zzz&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;static Mutex m;&lt;br /&gt;&lt;br /&gt;public static void Main()&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;m = new Mutex(true,"vijay");&lt;br /&gt;&lt;br /&gt;zzz a=new zzz( );&lt;br /&gt;&lt;br /&gt;Thread t= new Thread(new ThreadStart(a.abc));&lt;br /&gt;&lt;br /&gt;t.Start();&lt;br /&gt;&lt;br /&gt;System.Console.WriteLine("Before Thread Sleep");&lt;br /&gt;&lt;br /&gt;Thread.Sleep(1000);&lt;br /&gt;&lt;br /&gt;System.Console.WriteLine("After Thread Sleep");&lt;br /&gt;&lt;br /&gt;m.ReleaseMutex();&lt;br /&gt;&lt;br /&gt;System.Console.WriteLine("Before WaitOne");&lt;br /&gt;&lt;br /&gt;m.WaitOne();&lt;br /&gt;&lt;br /&gt;System.Console.WriteLine("All over");&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;public void abc( )&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;System.Console.WriteLine("abc start");&lt;br /&gt;&lt;br /&gt;m.WaitOne();&lt;br /&gt;&lt;br /&gt;System.Console.WriteLine("abc after Wait");&lt;br /&gt;&lt;br /&gt;m.WaitOne();&lt;br /&gt;&lt;br /&gt;System.Console.WriteLine("abc after second Wait");&lt;br /&gt;&lt;br /&gt;Thread.Sleep(10000);&lt;br /&gt;&lt;br /&gt;System.Console.WriteLine("End");&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Output&lt;br /&gt;&lt;br /&gt;Before Thread Sleep&lt;br /&gt;&lt;br /&gt;abc start&lt;br /&gt;&lt;br /&gt;After Thread Sleep&lt;br /&gt;&lt;br /&gt;abc after Wait&lt;br /&gt;&lt;br /&gt;abc after second Wait&lt;br /&gt;&lt;br /&gt;Before WaitOne&lt;br /&gt;&lt;br /&gt;End&lt;br /&gt;&lt;br /&gt;All over&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;The same rules about mutex ownership as mentioned earlier are discussed here.&lt;br /&gt;There may be two&lt;br /&gt;&lt;br /&gt;WaitOne functions, but the first WaitOne that gets the ownership, retains it.&lt;br /&gt;Thus, there was no&lt;br /&gt;&lt;br /&gt;wait between the first and the second wait. The concept of a mutex is extremely&lt;br /&gt;simple. A&lt;br /&gt;&lt;br /&gt;particular thread owns it. It can cross a wait without waiting and only when it&lt;br /&gt;releases the&lt;br /&gt;&lt;br /&gt;mutex can another thread claim and possess its ownership. Other threads cannot&lt;br /&gt;own it and they&lt;br /&gt;&lt;br /&gt;will have to wait at a wait. Dual ownership is not allowed.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;a.cs&lt;br /&gt;&lt;br /&gt;using System.Threading;&lt;br /&gt;&lt;br /&gt;public class zzz&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;static Mutex m;&lt;br /&gt;&lt;br /&gt;public static void Main()&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;m = new Mutex(false,"vijay");&lt;br /&gt;&lt;br /&gt;zzz a=new zzz( );&lt;br /&gt;&lt;br /&gt;System.Console.WriteLine("abc start");&lt;br /&gt;&lt;br /&gt;m.WaitOne( );&lt;br /&gt;&lt;br /&gt;System.Console.WriteLine("abc end");&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Output&lt;br /&gt;&lt;br /&gt;abc start&lt;br /&gt;&lt;br /&gt;abc end&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;A Mutex only works with multiple threads. So, using a mutex with a single thread&lt;br /&gt;is futile,&lt;br /&gt;&lt;br /&gt;because a mutex is concerned with determining as to which thread owns it.&lt;br /&gt;Therefore, there must&lt;br /&gt;&lt;br /&gt;be at least two threads.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Timers&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;a.cs&lt;br /&gt;&lt;br /&gt;using System.Threading;&lt;br /&gt;&lt;br /&gt;public class zzz&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;public static void Main() {&lt;br /&gt;&lt;br /&gt;zzz a = new zzz();&lt;br /&gt;&lt;br /&gt;a.pqr();&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;public void pqr()&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;Timer t;&lt;br /&gt;&lt;br /&gt;yyy b = new yyy();&lt;br /&gt;&lt;br /&gt;t = new Timer(new TimerCallback(b.abc),this,100,0);&lt;br /&gt;&lt;br /&gt;Thread.Sleep(1000);&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;public class yyy&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;public void abc(object o)&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;System.Console.WriteLine("hell");&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Output&lt;br /&gt;&lt;br /&gt;hell&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Timers are time-related. We have created a timer object and have passed it 4&lt;br /&gt;values in its&lt;br /&gt;&lt;br /&gt;constructor. The first parameter is the delegate object which represents the&lt;br /&gt;function that&lt;br /&gt;&lt;br /&gt;should be called after a certain interval elapses. The second parameter is the&lt;br /&gt;object to be&lt;br /&gt;&lt;br /&gt;passed to the timer function. The third and fourth parameters are numbers&lt;br /&gt;representing time i.e.&lt;br /&gt;&lt;br /&gt;time and period. If the thread is not asked to sleep, the main thread will quit&lt;br /&gt;out and there&lt;br /&gt;&lt;br /&gt;will be no thread running. The entire program will halt and all the unfired&lt;br /&gt;timers will die.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Thus, if we remove the last Sleep, the word 'hell' does not get displayed. The&lt;br /&gt;timer delegate&lt;br /&gt;&lt;br /&gt;has to receive an object as a parameter since it is part of the delegate&lt;br /&gt;signature. The timer&lt;br /&gt;&lt;br /&gt;also has to wait for a specified time which is stated in the constructor, before&lt;br /&gt;it can call the&lt;br /&gt;&lt;br /&gt;function. This wait is accomplished using a thread in a thread pool. Needless to&lt;br /&gt;say, the Timer&lt;br /&gt;&lt;br /&gt;class is a sealed class.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;a.cs&lt;br /&gt;&lt;br /&gt;using System.Threading;&lt;br /&gt;&lt;br /&gt;public class zzz&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;public static void Main()&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;zzz a = new zzz();&lt;br /&gt;&lt;br /&gt;a.pqr();&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;public void pqr()&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;Timer t;&lt;br /&gt;&lt;br /&gt;yyy b = new yyy();&lt;br /&gt;&lt;br /&gt;t = new Timer(new TimerCallback(b.abc),this,100,0);&lt;br /&gt;&lt;br /&gt;t.Dispose();&lt;br /&gt;&lt;br /&gt;Thread.Sleep(1000);&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;public class yyy&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;public void abc(object o)&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;System.Console.WriteLine("hell");&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Output&lt;br /&gt;&lt;br /&gt;(none)&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;If we ever desire to cancel the timer for whatever reasons, we can call the&lt;br /&gt;Dispose method to&lt;br /&gt;&lt;br /&gt;kill the timer. The timer dies and the function to be called does not get called&lt;br /&gt;at all.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;a.cs&lt;br /&gt;&lt;br /&gt;using System.Threading;&lt;br /&gt;&lt;br /&gt;public class zzz&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;public static void Main()&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;zzz a = new zzz();&lt;br /&gt;&lt;br /&gt;a.pqr();&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;public void pqr()&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;Timer t;&lt;br /&gt;&lt;br /&gt;ManualResetEvent e = new ManualResetEvent(false);&lt;br /&gt;&lt;br /&gt;yyy b = new yyy();&lt;br /&gt;&lt;br /&gt;t = new Timer(new TimerCallback(b.abc),this,10000,300);&lt;br /&gt;&lt;br /&gt;t.Dispose(e);&lt;br /&gt;&lt;br /&gt;e.WaitOne();&lt;br /&gt;&lt;br /&gt;System.Console.WriteLine("All Over");&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;public class yyy&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;public void abc(object o)&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;System.Console.WriteLine("hell");&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Output&lt;br /&gt;&lt;br /&gt;All Over&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;The Dispose function can also take a parameter which is an event that gets&lt;br /&gt;signaled when the&lt;br /&gt;&lt;br /&gt;timer dies. Thus, the program that would have waited indefinitely now quits out&lt;br /&gt;because,&lt;br /&gt;&lt;br /&gt;removing the timer from the timer queue instantly signals an event.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;a.cs&lt;br /&gt;&lt;br /&gt;using System.Threading;&lt;br /&gt;&lt;br /&gt;public class zzz&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;public static void Main()&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;zzz a = new zzz();&lt;br /&gt;&lt;br /&gt;a.pqr();&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;public void pqr()&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;Timer t;&lt;br /&gt;&lt;br /&gt;yyy b = new yyy();&lt;br /&gt;&lt;br /&gt;t = new Timer(new TimerCallback(b.abc),this,100,1);&lt;br /&gt;&lt;br /&gt;Thread.Sleep(500);&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;public class yyy&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;public void abc(object o)&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;System.Console.WriteLine("hell");&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Output&lt;br /&gt;&lt;br /&gt;hell&lt;br /&gt;&lt;br /&gt;hell&lt;br /&gt;&lt;br /&gt;hell&lt;br /&gt;&lt;br /&gt;hell&lt;br /&gt;&lt;br /&gt;and so on&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;The third parameter to the timer constructor is the duration in milliseconds&lt;br /&gt;after which the&lt;br /&gt;&lt;br /&gt;timer should be fired. The second number can be either a 0 or any other number.&lt;br /&gt;Zero means the&lt;br /&gt;&lt;br /&gt;timer fires once and any other number signifies it as a periodic timer. This&lt;br /&gt;periodic timer&lt;br /&gt;&lt;br /&gt;keeps executing the function abc every 100 milliseconds, until told to do&lt;br /&gt;otherwise.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;a.cs&lt;br /&gt;&lt;br /&gt;using System.Threading;&lt;br /&gt;&lt;br /&gt;public class zzz&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;public static void Main()&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;zzz a = new zzz();&lt;br /&gt;&lt;br /&gt;a.pqr();&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;public void pqr()&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;Timer t;&lt;br /&gt;&lt;br /&gt;yyy b = new yyy();&lt;br /&gt;&lt;br /&gt;t = new Timer(new TimerCallback(b.abc),this,100,1);&lt;br /&gt;&lt;br /&gt;Thread.Sleep(115);&lt;br /&gt;&lt;br /&gt;t.Change(100,0);&lt;br /&gt;&lt;br /&gt;Thread.Sleep(1300);&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;public class yyy&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;public void abc(object o)&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;System.Console.WriteLine("hell");&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Output&lt;br /&gt;&lt;br /&gt;hell&lt;br /&gt;&lt;br /&gt;hell&lt;br /&gt;&lt;br /&gt;hell&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;The Change function is used to stop a periodic timer. Thus, the timer can be&lt;br /&gt;made to run for&lt;br /&gt;&lt;br /&gt;some time in a periodic manner and thereafter, the Change can be used to stop it&lt;br /&gt;from executing&lt;br /&gt;&lt;br /&gt;repeatedly.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;a.cs&lt;br /&gt;&lt;br /&gt;using System.Threading;&lt;br /&gt;&lt;br /&gt;public class zzz&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;public static void Main()&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;zzz a = new zzz();&lt;br /&gt;&lt;br /&gt;a.pqr();&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;public void pqr()&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;Timer t;&lt;br /&gt;&lt;br /&gt;yyy b = new yyy();&lt;br /&gt;&lt;br /&gt;t = new Timer(new TimerCallback(b.abc),this,100000,1);&lt;br /&gt;&lt;br /&gt;Thread.Sleep(115);&lt;br /&gt;&lt;br /&gt;t.Change(100,0);&lt;br /&gt;&lt;br /&gt;Thread.Sleep(1300);&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;public class yyy&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;public void abc(object o)&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;System.Console.WriteLine("hell");&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Output&lt;br /&gt;&lt;br /&gt;hell&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;The timer initially is set to go off after a very long time. We let the main&lt;br /&gt;thread sleep for a&lt;br /&gt;&lt;br /&gt;little while but then change the due time of the timer to a smaller value. Thus,&lt;br /&gt;we can use the&lt;br /&gt;&lt;br /&gt;Change function to alter the initial settings of the timer.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;a.cs&lt;br /&gt;&lt;br /&gt;using System.Threading;&lt;br /&gt;&lt;br /&gt;public class zzz&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;public static void Main()&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;zzz a = new zzz();&lt;br /&gt;&lt;br /&gt;a.pqr();&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;public void pqr()&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;Timer t;&lt;br /&gt;&lt;br /&gt;yyy b = new yyy();&lt;br /&gt;&lt;br /&gt;t = new Timer(new TimerCallback(b.abc),this,100,300);&lt;br /&gt;&lt;br /&gt;Thread.Sleep(1000);&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;public class yyy&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;public void abc(object o)&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;System.Console.WriteLine("hell");&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;Output&lt;br /&gt;&lt;br /&gt;hell&lt;br /&gt;&lt;br /&gt;hell&lt;br /&gt;&lt;br /&gt;hell&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Our dictum has always been to understand things one step at a time. The last&lt;br /&gt;parameter to the&lt;br /&gt;&lt;br /&gt;timer constructor can either be zero or any other value. If the value is 100, it&lt;br /&gt;means that&lt;br /&gt;&lt;br /&gt;every hundred seconds the timer will go off, until it is stopped by a Change or&lt;br /&gt;a Dispose.&lt;br /&gt;&lt;br /&gt;Earlier, we had used the number 1 to signify that the timer should be called&lt;br /&gt;every 1 second. You&lt;br /&gt;&lt;br /&gt;are free to decide these timer values depending upon what you intend to use the&lt;br /&gt;timer for. The&lt;br /&gt;&lt;br /&gt;values cannot be negative and cannot exceed 4294967294 either. If you do so, an&lt;br /&gt;exception will&lt;br /&gt;&lt;br /&gt;occur. Exceptions, lest you have forgotten, are generated at runtime only.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Thread Pool&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;a.cs&lt;br /&gt;&lt;br /&gt;using System;&lt;br /&gt;&lt;br /&gt;using System.Threading;&lt;br /&gt;&lt;br /&gt;public class yyy {&lt;br /&gt;&lt;br /&gt;public ManualResetEvent a = new ManualResetEvent(false);&lt;br /&gt;&lt;br /&gt;public void abc(Object s)&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;a.WaitOne(10,true);&lt;br /&gt;&lt;br /&gt;Console.WriteLine("{0} ", Thread.CurrentThread.GetHashCode());&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;};&lt;br /&gt;&lt;br /&gt;public class zzz {&lt;br /&gt;&lt;br /&gt;public static void Main() {&lt;br /&gt;&lt;br /&gt;ManualResetEvent b = new ManualResetEvent(false);&lt;br /&gt;&lt;br /&gt;yyy y = new yyy();&lt;br /&gt;&lt;br /&gt;for (int i=1;i&amp;lt;=5;i++)&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;ThreadPool.QueueUserWorkItem(new WaitCallback(y.abc),1);&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;b.WaitOne(10000,true);&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;Output&lt;br /&gt;&lt;br /&gt;2&lt;br /&gt;&lt;br /&gt;2&lt;br /&gt;&lt;br /&gt;2&lt;br /&gt;&lt;br /&gt;2&lt;br /&gt;&lt;br /&gt;2&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;The rationale of a thread pool is that the decision makers at Microsoft did not&lt;br /&gt;want us to spend&lt;br /&gt;&lt;br /&gt;time taking care of threads like mothers take care of babies.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;We will first explain the above program and then delve into the nitty-gritty of&lt;br /&gt;thread pools.&lt;br /&gt;&lt;br /&gt;Since we are lazy, we use a for statement to avoid repeating the same code over&lt;br /&gt;and over again.&lt;br /&gt;&lt;br /&gt;The function QueueUserWorkItem will be called at least five times because it is&lt;br /&gt;placed in this&lt;br /&gt;&lt;br /&gt;for loop.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;QueueUserWorkItem function requires one, or at the most, two parameters. The&lt;br /&gt;first parameter,&lt;br /&gt;&lt;br /&gt;which is the name of a function, is most crucial. The name is never ever&lt;br /&gt;assigned directly, but&lt;br /&gt;&lt;br /&gt;indirectly, using a WaitCallback object. The function to be called is y.abc.&lt;br /&gt;Hence, it is passed&lt;br /&gt;&lt;br /&gt;a parameter to WaitCallback. The second parameter to the function&lt;br /&gt;QueueUserWorkItem is the&lt;br /&gt;&lt;br /&gt;number 1. As a result, the function abc has to accept a parameter of type object&lt;br /&gt;which will&lt;br /&gt;&lt;br /&gt;contain the value 1. A parameter of type object does not have any useful code&lt;br /&gt;and by itself is&lt;br /&gt;&lt;br /&gt;totally useless.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Every entity in C# is finally derived from the object class. Thus, every entity&lt;br /&gt;is of an object&lt;br /&gt;&lt;br /&gt;type. Whenever we are not aware of the parameter type to be received in a&lt;br /&gt;function, or in cases&lt;br /&gt;&lt;br /&gt;where we want to receive multiple data types, the data type of object is used.&lt;br /&gt;Object is like a&lt;br /&gt;&lt;br /&gt;rubber man; it can fit anywhere and everywhere. By giving the function name abc&lt;br /&gt;to the function&lt;br /&gt;&lt;br /&gt;QueueUserWorkItem, abc will be called five times by a thread. The function waits&lt;br /&gt;for a small&lt;br /&gt;&lt;br /&gt;amount of time at an event and then uses the function GetHashCode to print the&lt;br /&gt;name/id of the&lt;br /&gt;&lt;br /&gt;thread. This function will print the thread id that is calling abc.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;From our output, we can conclude that only two threads execute the function abc.&lt;br /&gt;Commenting the&lt;br /&gt;&lt;br /&gt;Wait statement will display the same id for the thread. This means that no new&lt;br /&gt;thread executes&lt;br /&gt;&lt;br /&gt;the function. Thus, instead of two threads, only a single thread is used.&lt;br /&gt;Efficiency, thy name&lt;br /&gt;&lt;br /&gt;is Thread Pool.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;We are thus posting work items to a common place or thread pool. Then, a thread&lt;br /&gt;is allocated to&lt;br /&gt;&lt;br /&gt;execute the work item or function. The first available thread from the thread&lt;br /&gt;pool executes the&lt;br /&gt;&lt;br /&gt;function. We are only using one thread pool per program.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;There is a lot of overhead in managing threads. A lot of time is spent figuring&lt;br /&gt;out which thread&lt;br /&gt;&lt;br /&gt;is sleeping, i.e. waiting for an event to occur. Most of the time, threads are&lt;br /&gt;in this inert&lt;br /&gt;&lt;br /&gt;state. At times, threads wake up to check/poll for a change that is expected to&lt;br /&gt;occur, such as a&lt;br /&gt;&lt;br /&gt;key press, or to update some counter or status information and fall asleep&lt;br /&gt;again. Threads are&lt;br /&gt;&lt;br /&gt;like bears, they love sleeping.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Thread Pooling is a means of managing threads more efficiently. This is done by&lt;br /&gt;providing your&lt;br /&gt;&lt;br /&gt;application with a pool of threads that are managed by the system. All threads&lt;br /&gt;that do work are&lt;br /&gt;&lt;br /&gt;called worker threads. There is a thread that monitors the status of threads&lt;br /&gt;waiting at any of&lt;br /&gt;&lt;br /&gt;the wait functions. When the wait is over, the callback function will be&lt;br /&gt;executed.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;The next program demonstrates how a function can be called after a specified&lt;br /&gt;time duration has&lt;br /&gt;&lt;br /&gt;elapsed. There is no way to cancel a function from being executed once it has&lt;br /&gt;gone into the&lt;br /&gt;&lt;br /&gt;thread pool. A prayer will also not help. Timers and Waits use the thread pool&lt;br /&gt;without our&lt;br /&gt;&lt;br /&gt;knowledge.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;The thread pool is created when we call the function QueueUserWorkItem for the&lt;br /&gt;first time or&lt;br /&gt;&lt;br /&gt;when we use a timer function. The number of threads that form part of a thread&lt;br /&gt;pool is limited&lt;br /&gt;&lt;br /&gt;by the amount of memory available. By today's standards, this means that more&lt;br /&gt;threads can be&lt;br /&gt;&lt;br /&gt;made available than we would ever need. These threads receive the default stack&lt;br /&gt;size and run at&lt;br /&gt;&lt;br /&gt;the default priority. No special priority is accorded. For the trivia fans, a&lt;br /&gt;thread can handle&lt;br /&gt;&lt;br /&gt;up to 63 wait operations. No concept is perfect. Thus, one significant&lt;br /&gt;disadvantage of a thread&lt;br /&gt;&lt;br /&gt;pool is that it involves a long calculation.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;a.cs&lt;br /&gt;&lt;br /&gt;using System;&lt;br /&gt;&lt;br /&gt;using System.Threading;&lt;br /&gt;&lt;br /&gt;public class yyy&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;public void abc(object s, bool b)&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;Console.WriteLine("{0} {1}", Thread.CurrentThread.GetHashCode(),b);&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;};&lt;br /&gt;&lt;br /&gt;public class zzz&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;public static void Main()&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;ManualResetEvent a = new ManualResetEvent(false);&lt;br /&gt;&lt;br /&gt;ManualResetEvent b = new ManualResetEvent(false);&lt;br /&gt;&lt;br /&gt;yyy y = new yyy();&lt;br /&gt;&lt;br /&gt;ThreadPool.RegisterWaitForSingleObject(a,new&lt;br /&gt;WaitOrTimerCallback(y.abc),1,1000,true);&lt;br /&gt;&lt;br /&gt;b.WaitOne(10000,true);&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Output&lt;br /&gt;&lt;br /&gt;2 True&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;As mentioned earlier, we would like our function to be called after a certain&lt;br /&gt;wait. To&lt;br /&gt;&lt;br /&gt;accomplish this, we use a function with a very large name called&lt;br /&gt;RegisterWaitForSingleObject.&lt;br /&gt;&lt;br /&gt;The first parameter is the handle of an event. This has been set to false or non&lt;br /&gt;signaled. The&lt;br /&gt;&lt;br /&gt;second parameter is a delegate of type WaitOrTimerCallback which is similar to a&lt;br /&gt;WaitCallback.&lt;br /&gt;&lt;br /&gt;The difference is that the signature of the function given to this function,&lt;br /&gt;which is abc, has&lt;br /&gt;&lt;br /&gt;to have two parameters, viz. an object and a boolean variable. The third&lt;br /&gt;parameter is the object&lt;br /&gt;&lt;br /&gt;that is to be passed to the function. The second last parameter is a timer that&lt;br /&gt;decides the&lt;br /&gt;&lt;br /&gt;duration after which the function should be called. Finally, the last parameter&lt;br /&gt;decides whether&lt;br /&gt;&lt;br /&gt;the function is to be executed only once or every time the timer expires.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;As our event object is set to false, the function gets called after 1000&lt;br /&gt;milliseconds. When the&lt;br /&gt;&lt;br /&gt;timer expires, the function abc obtains a value of true.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;By merely changing one line in the above example, the event can be changed to a&lt;br /&gt;signaled state.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;ManualResetEvent a = new ManualResetEvent(true);&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Output&lt;br /&gt;&lt;br /&gt;2 False&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;The function abc gets called immediately, unlike in the earlier program. Also,&lt;br /&gt;there is no wait&lt;br /&gt;&lt;br /&gt;as the event is true or signaled. The function abc also knows why it has been&lt;br /&gt;called since the&lt;br /&gt;&lt;br /&gt;bool parameter has a value of false, which was true in the earlier case. Thus,&lt;br /&gt;the function abc&lt;br /&gt;&lt;br /&gt;can look at the value of its second parameter, which indicates whether the timer&lt;br /&gt;has elapsed or&lt;br /&gt;&lt;br /&gt;the event has been signaled.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;a.cs&lt;br /&gt;&lt;br /&gt;using System;&lt;br /&gt;&lt;br /&gt;using System.Threading;&lt;br /&gt;&lt;br /&gt;public class yyy&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;public void Beta(object s, bool b)&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;Console.WriteLine("{0} {1}", Thread.CurrentThread.GetHashCode(),b);&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;};&lt;br /&gt;&lt;br /&gt;public class zzz&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;public static void Main()&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;ManualResetEvent a = new ManualResetEvent(false);&lt;br /&gt;&lt;br /&gt;ManualResetEvent b = new ManualResetEvent(false);&lt;br /&gt;&lt;br /&gt;yyy y = new yyy();&lt;br /&gt;&lt;br /&gt;ThreadPool.RegisterWaitForSingleObject(a,new&lt;br /&gt;WaitOrTimerCallback(y.Beta),1,10000,true);&lt;br /&gt;&lt;br /&gt;a.Set();&lt;br /&gt;&lt;br /&gt;b.WaitOne(10000,true);&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Output&lt;br /&gt;&lt;br /&gt;2 False&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;In this program, initially we set the event to false and then, using the Set&lt;br /&gt;function, we set it&lt;br /&gt;&lt;br /&gt;to true immediately. Thus, the function gets called instantly. You would&lt;br /&gt;remember that the&lt;br /&gt;&lt;br /&gt;function gets called whenever the event is in a signaled state or the timer&lt;br /&gt;expires. This&lt;br /&gt;&lt;br /&gt;function first checks whether the wait object is signaled or not. If it is&lt;br /&gt;signaled, it executes&lt;br /&gt;&lt;br /&gt;the function. If it is not, it waits for the timer to elapse or the wait object&lt;br /&gt;to come into the&lt;br /&gt;&lt;br /&gt;signaled state, whichever occurs first.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Interlocked&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;a.cs&lt;br /&gt;&lt;br /&gt;using System;&lt;br /&gt;&lt;br /&gt;using System.Threading;&lt;br /&gt;&lt;br /&gt;public class yyy&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;public ManualResetEvent a = new ManualResetEvent(false);&lt;br /&gt;&lt;br /&gt;int i = 10;&lt;br /&gt;&lt;br /&gt;public void abc(Object s)&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;Interlocked.Increment(ref i);&lt;br /&gt;&lt;br /&gt;Console.WriteLine("{0} {1}", Thread.CurrentThread.GetHashCode(),i);&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;};&lt;br /&gt;&lt;br /&gt;public class zzz&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;public static void Main() {&lt;br /&gt;&lt;br /&gt;ManualResetEvent b = new ManualResetEvent(false);&lt;br /&gt;&lt;br /&gt;yyy y = new yyy();&lt;br /&gt;&lt;br /&gt;for (int i=1;i&amp;lt;=5;i++)&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;ThreadPool.QueueUserWorkItem(new WaitCallback(y.abc),1);&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;b.WaitOne(10000,true);&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Output&lt;br /&gt;&lt;br /&gt;2 11&lt;br /&gt;&lt;br /&gt;2 12&lt;br /&gt;&lt;br /&gt;2 13&lt;br /&gt;&lt;br /&gt;2 14&lt;br /&gt;&lt;br /&gt;2 15&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Threads can be dangerous for the unwary. The function abc gets called five times&lt;br /&gt;by the same or&lt;br /&gt;&lt;br /&gt;a different thread. Consider a case where one thread may be incrementing the&lt;br /&gt;line i = i + 1. To&lt;br /&gt;&lt;br /&gt;do so, it will start by asking what is the value of i. Let us suppose that it is&lt;br /&gt;10. It then&lt;br /&gt;&lt;br /&gt;increments the value by 1 to make it 11. It may so happen that just as it is&lt;br /&gt;about to read its&lt;br /&gt;&lt;br /&gt;value, its time slice may get over and another thread may increment the variable&lt;br /&gt;i by 1 to make&lt;br /&gt;&lt;br /&gt;its value 12.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;When the first thread receives its next time slice and checks the value of i, it&lt;br /&gt;will see the&lt;br /&gt;&lt;br /&gt;value as 12 and not 11. Thus, the variable would have been incremented twice&lt;br /&gt;instead of once.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;The Interlocked class has a sole purpose in life, i.e. to ensure that the&lt;br /&gt;variables are not&lt;br /&gt;&lt;br /&gt;incremented more than once. In a sense, it synchronizes access to a variable&lt;br /&gt;that is being&lt;br /&gt;&lt;br /&gt;shared by a number of threads. The operation will either be done in a single&lt;br /&gt;time slice or not&lt;br /&gt;&lt;br /&gt;done at all. It will not be carried over into another time slice ever. These&lt;br /&gt;operations, which&lt;br /&gt;&lt;br /&gt;are guaranteed to be finished in a single time slice, are called atomic&lt;br /&gt;operations.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;a.cs&lt;br /&gt;&lt;br /&gt;using System;&lt;br /&gt;&lt;br /&gt;using System.Threading;&lt;br /&gt;&lt;br /&gt;public class yyy&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;public ManualResetEvent a = new ManualResetEvent(false);&lt;br /&gt;&lt;br /&gt;public int i = 10;&lt;br /&gt;&lt;br /&gt;public void abc(Object s)&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;i++;&lt;br /&gt;&lt;br /&gt;Interlocked.CompareExchange(ref i,100,12);&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;};&lt;br /&gt;&lt;br /&gt;public class zzz&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;public static void Main()&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;ManualResetEvent b = new ManualResetEvent(false);&lt;br /&gt;&lt;br /&gt;yyy y = new yyy();&lt;br /&gt;&lt;br /&gt;for (int i=1;i&amp;lt;=5;i++)&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;ThreadPool.QueueUserWorkItem(new WaitCallback(y.abc),1);&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;b.WaitOne(1000,true);&lt;br /&gt;&lt;br /&gt;Console.WriteLine(y.i);&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Output&lt;br /&gt;&lt;br /&gt;103&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;The function abc gets called five times. If we intend to carry out comparisons,&lt;br /&gt;we would face&lt;br /&gt;&lt;br /&gt;the above dilemma. Thus, we use a simple function called CompareExchange. This&lt;br /&gt;functions checks&lt;br /&gt;&lt;br /&gt;the value of the first parameter that is passed as a reference, with the third&lt;br /&gt;parameter, which&lt;br /&gt;&lt;br /&gt;in our case is 12. If it matches the value, the first parameter i.e. i is&lt;br /&gt;assigned the value of&lt;br /&gt;&lt;br /&gt;the second parameter i.e. 100. All this is done in a single time slice. Thus,&lt;br /&gt;whenever variable&lt;br /&gt;&lt;br /&gt;i becomes 12, its value shoots up to 100, and as a result, the final answer&lt;br /&gt;becomes 103.&lt;br /&gt;Like the Increment, there is a Decrement function also that reduces the value of&lt;br /&gt;a variable by&lt;br /&gt;&lt;br /&gt;one. As an aside, a ref parameter cannot be replaced by an out parameter. In the&lt;br /&gt;same vein, the&lt;br /&gt;&lt;br /&gt;Exchange function changes the value of two variables passed to it as ref&lt;br /&gt;parameters. This is&lt;br /&gt;&lt;br /&gt;done in an atomic manner.&lt;br /&gt;&lt;br /&gt;&lt;/p&gt;</description><thr:total xmlns:thr="http://purl.org/syndication/thread/1.0">0</thr:total></item><item><title>27 Dos web Design</title><link>http://swebsolution.blogspot.com/2007/11/27-dos-web-design_18.html</link><author>noreply@blogger.com (Unknown)</author><pubDate>Sun, 18 Nov 2007 23:37:00 -0800</pubDate><guid isPermaLink="false">tag:blogger.com,1999:blog-7227425366482465228.post-1334127006249755814</guid><description>&lt;strong&gt;&lt;a href="http://www.swebsolution.150m.com/"&gt;Do reuse images&lt;/a&gt;&lt;/strong&gt;&lt;br /&gt;&lt;strong&gt;&lt;/strong&gt;&lt;br /&gt;Once images are loaded they are stored in cache.&lt;br /&gt;If you use the same image in several webpages the image will be&lt;br /&gt;loaded the second time in a breeze. It is necessary to have the&lt;br /&gt;image in the same location. Also be sure to use exactly the same&lt;br /&gt;filename. Even if a browser can handle differences in uppercase&lt;br /&gt;and lowercase, your browsercache can not.&lt;br /&gt;Don't reference images from other sites&lt;br /&gt;If you use images from another site (i.e. an image archive ) do copy&lt;br /&gt;them to your own site.&lt;br /&gt;Looking up other servers to get the images would introduce&lt;br /&gt;overhead and an extended load-time.&lt;br /&gt;Do provide size information&lt;br /&gt;If you use large images, videos etc., give an indication of the size&lt;br /&gt;before the actual transfer begins.&lt;br /&gt;Don't use large textual images&lt;br /&gt;Avoid creating images of large pieces of text. It gives you more&lt;br /&gt;possibilities but it costs valuable bandwidth.&lt;br /&gt;If you do need textual images be sure to reduce the number of&lt;br /&gt;colors.&lt;br /&gt;Don't use wordprocessors&lt;br /&gt;Don't use wordprocessors to write HTML. They really mess it up.&lt;br /&gt;It's not uncommon for a site to double or triple in size because tags&lt;br /&gt;are added unnecessarily by a wordprocessor.&lt;br /&gt;100 Do's and Don'ts in Web Design SpiderPro&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;&lt;a href="http://www.swebsolution.150m.com/"&gt;Presenting Text&lt;br /&gt;Don't use blink&lt;/a&gt;&lt;/strong&gt;&lt;br /&gt;&lt;strong&gt;&lt;/strong&gt;&lt;br /&gt;The tag &lt;blink&gt; should never have been introduced to HTML.&lt;br /&gt;Blinking text is irritating, it offends your readers. Don't use it.&lt;br /&gt;Don't use columns for text&lt;br /&gt;You can show your text in 2 or more columns. This can be&lt;br /&gt;implemented with tables or with the not official tag &lt;multicol&gt;.&lt;br /&gt;The result is newspaper-column like page.&lt;br /&gt;But columns have a large drawback on a screen. It takes more upand-&lt;br /&gt;down scrolling to read the text, especially if it's a large page.&lt;br /&gt;Avoid using columns this way.&lt;br /&gt;Don't use small serif letters&lt;br /&gt;Serif letters are developed for printing. They don't look good on a&lt;br /&gt;computer screen. At least not in smaller fonts.&lt;br /&gt;You should avoid these letters, especially if the serif is tiny, like&lt;br /&gt;Times.&lt;br /&gt;Don't use all capitals&lt;br /&gt;DO NOT USE ALL CAPITALS.&lt;br /&gt;It takes more time to read text that consists of only capitals.&lt;br /&gt;Besides, using all capitals is the online equivalent of shouting.&lt;br /&gt;Don't overuse bold text&lt;br /&gt;Bold text is meant to give some focus to a part of your text. Don't&lt;br /&gt;put whole paragraphs in bold. It has the same effect as shouting.&lt;br /&gt;Keep focus - and bold text - short and functional.&lt;br /&gt;100 Do's and Don'ts in Web Design SpiderPro&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;&lt;a href="http://www.swebsolution.150m.com/"&gt;presenting text&lt;/a&gt;&lt;/strong&gt;&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;&lt;a href="http://www.swebsolution.150m.com/"&gt;Don't overuse italics&lt;/a&gt;&lt;/strong&gt;&lt;br /&gt;Text in italic is hard to read on a screen. The resolution of a screen&lt;br /&gt;just isn't capable to present italics without distorting them slightly.&lt;br /&gt;This is even more noticeable if you use a small font.&lt;br /&gt;So don't use italics for larger portions of your text.&lt;br /&gt;Don't use small fonts&lt;br /&gt;Don't use small fonts (font size smaller than 4). Small letters are&lt;br /&gt;hard to read and that's even worse on a computer screen.&lt;br /&gt;Don't use too many fonts&lt;br /&gt;Using all kinds of fonts on one page - or in one site - is a very bad&lt;br /&gt;typographical practice.&lt;br /&gt;Unless you run an online font-archive.&lt;br /&gt;Do use punctuation&lt;br /&gt;Present information surveyable.&lt;br /&gt;Present it in small chunks. Use headings to separate them. Use&lt;br /&gt;lists to avoid long textual summing ups.&lt;br /&gt;Don't hide your links&lt;br /&gt;The default color settings of links is pretty standard.&lt;br /&gt;You can change link colors. But if you do you'll have to be sure that&lt;br /&gt;links can be recognized.&lt;br /&gt;Don't let those fancy colors hide your links.&lt;br /&gt;100 Do's and Don'ts in Web Design SpiderPro&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;&lt;a href="http://www.swebsolution.150m.com/"&gt;presenting text&lt;/a&gt;&lt;/strong&gt;&lt;br /&gt;&lt;strong&gt;&lt;/strong&gt;&lt;br /&gt;Do use all lowercase or all uppercase links&lt;br /&gt;Many browsers discriminate between differences in case. Even if&lt;br /&gt;your server doesn't, don't rely on it. If you'll need to move to&lt;br /&gt;another server you will be happy if all your links still function. The&lt;br /&gt;easiest way to accomplish this is to use links consequently all&lt;br /&gt;lowercase or all uppercase.&lt;br /&gt;Do separate adjacent textual links&lt;br /&gt;If you place textual links horizontally be sure to separate them&lt;br /&gt;clearly. With spaces, a vertical line, bracket, whatever.&lt;br /&gt;The point is that it must be clear to a visitor where a link starts and&lt;br /&gt;where it ends.&lt;br /&gt;Do limit the size of predefined text&lt;br /&gt;Predefined text (between the tags &lt;pre&gt; and &lt;/pre&gt;) cannot be&lt;br /&gt;wrapped. If you use it be sure to limit the size, especially the width.&lt;br /&gt;Otherwise your visitors might need to scroll horizontally to read the&lt;br /&gt;text.&lt;br /&gt;Do limit the width of text&lt;br /&gt;Reading full width text on a full screen browser is quite terrible. The&lt;br /&gt;lines of text get too long; giving you a headache reading them.&lt;br /&gt;Limit the width of text-lines using tables, blockquotes etc.&lt;br /&gt;100 Do's and Don'ts in Web Design SpiderPro&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;&lt;a href="http://www.swebsolution.150m.com/"&gt;presenting text&lt;br /&gt;Do use textual dates&lt;/a&gt;&lt;br /&gt;&lt;/strong&gt;&lt;br /&gt;Probably you do use dates. For instance to show the most recent&lt;br /&gt;update of your site.&lt;br /&gt;But it's a world wide web.&lt;br /&gt;Do keep in mind that 02-03-2000 will be the second of March or the&lt;br /&gt;third of February, depending on the country your visitor comes&lt;br /&gt;from.&lt;br /&gt;If you use text for your month, like March 2, 2000 the date will be&lt;br /&gt;correctly interpreted.&lt;br /&gt;Do provide a visual e-mail address&lt;br /&gt;The mailto: trick is great. Start the e-mail-application right from the&lt;br /&gt;webpage. But it only works if your visitor uses an integrated e-mail&lt;br /&gt;application. For all the others provide a readable e-mail-address.&lt;br /&gt;100 Do's and Don'ts in Web Design SpiderPro&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;&lt;a href="http://www.swebsolution.150m.com/"&gt;images and colors&lt;br /&gt;Images and colors&lt;/a&gt;&lt;/strong&gt;&lt;br /&gt;&lt;strong&gt;&lt;/strong&gt;&lt;br /&gt;Do use transparency&lt;br /&gt;The presentation of images often improves by giving the images a&lt;br /&gt;transparent backgroundcolor.&lt;br /&gt;They'll better integrate visually with the background.&lt;br /&gt;Do use interlacing&lt;br /&gt;Interlace larger GIF-images. The visitor will get a quick feedback&lt;br /&gt;while the image is still loading.&lt;br /&gt;For very small images - like bullets - interlacing makes no sense&lt;br /&gt;but in all other cases it does.&lt;br /&gt;Don't use too many images&lt;br /&gt;Too many images slows down your site. Don't chase your visitors&lt;br /&gt;away; limit the number of images.&lt;br /&gt;Do make your graphics reproducible&lt;br /&gt;If you create your own graphics make them reproducible.&lt;br /&gt;You might need another one of the same kind, so be sure to write&lt;br /&gt;down all the effects and the parameters you've used to create the&lt;br /&gt;graphics.&lt;br /&gt;Do break up images&lt;br /&gt;If you use large images you can break them up in several parts.&lt;br /&gt;You can combine the parts in the webpage to form the original&lt;br /&gt;image.&lt;br /&gt;Doing so the image-parts can be downloaded parallel, thus&lt;br /&gt;reducing download time.&lt;br /&gt;100 Do's and Don'ts in Web Design SpiderPro&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;&lt;a href="http://www.swebsolution.150m.com/"&gt;images and colors&lt;br /&gt;Don't use PNG (yet)&lt;/a&gt;&lt;/strong&gt;&lt;br /&gt;&lt;strong&gt;&lt;/strong&gt;&lt;br /&gt;PNG is a great format for graphics and will eventually replace GIF.&lt;br /&gt;But right now many browsers are in use that don't support PNG.&lt;br /&gt;For the time being stick to GIF and JPEG.&lt;br /&gt;Do combine backgroundimages and&lt;br /&gt;background-colors&lt;br /&gt;Even if you do use a background-image, still provide a&lt;br /&gt;background-color. The background-color should approximately be&lt;br /&gt;the main color of the image.&lt;br /&gt;If text has a color that contrasts with the background-image, it will&lt;br /&gt;still be readable before the background-image is loaded.&lt;br /&gt;Do use a browser safe palette&lt;br /&gt;Use a browser safe palette for your colors.&lt;br /&gt;This will prevent colors from dithering on older monitors.&lt;br /&gt;Don't use too many colors&lt;br /&gt;You have access to 16 million colors. Be selective - don't try to use&lt;br /&gt;them all. Too many colors distract form what you're trying to say.&lt;br /&gt;Don't override only one standard color&lt;br /&gt;If you override a standard color, be sure to override them all.&lt;br /&gt;Your visitor has her own color settings. If you change i.e. only the&lt;br /&gt;fontcolor to darkblue, text might become unreadable on a dark&lt;br /&gt;background. So be sure also to change the backgroundcolor.&lt;br /&gt;100 Do's and Don'ts in Web Design SpiderPro&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;&lt;a href="http://www.swebsolution.150m.com/"&gt;Compatibility&lt;/a&gt;&lt;/strong&gt;&lt;br /&gt;&lt;strong&gt;&lt;/strong&gt;&lt;br /&gt;Do supply alt's for your images&lt;br /&gt;Quite a few people disable the automatic load of pictures in their&lt;br /&gt;browser.&lt;br /&gt;Don't blame them, some images take a lifetime to load.&lt;br /&gt;By defining an alternative text with the attribute alt in the tag&lt;br /&gt;&lt;img&gt; you can take care of imageless browsers.&lt;br /&gt;Do use alt text for areas&lt;br /&gt;Use the alt attribute to supply text for areas in an image map, thus&lt;br /&gt;providing an alternative for browsers that don't support images.&lt;br /&gt;Do use NOFRAMES&lt;br /&gt;Not all browsers support frames. Define an alternative with the tag&lt;br /&gt;&lt;noframes&gt;&lt;br /&gt;Do use comments for JavaScript&lt;br /&gt;If you use JavaScript, be sure to place the source between&lt;br /&gt;comments in such a way that browsers that don't support&lt;br /&gt;JavaScript won't show any garbage.&lt;br /&gt;Don't be selective with browsers&lt;br /&gt;You believe I'd change my browser just to visit your site? Wake up!&lt;br /&gt;Saying Best viewed with my favorite browser only has the effect&lt;br /&gt;that you drive off people with other browsers.&lt;br /&gt;Test your site with the favorites. It should be readable with all the&lt;br /&gt;important browsers.&lt;br /&gt;100 Do's and Don'ts in Web Design SpiderPro&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;&lt;a href="http://www.swebsolution.150m.com/"&gt;compatibility&lt;br /&gt;Do test Java applets with all browsers&lt;br /&gt;&lt;/a&gt;&lt;/strong&gt;&lt;br /&gt;Java is great, I really love it. But as soon as a page starts with&lt;br /&gt;Applet loading, I got this feeling it all ends up wrong.&lt;br /&gt;There are indeed far too many untested applets wrecking browsers&lt;br /&gt;and making sure a visitor will never come back again.&lt;br /&gt;Do check your Java applets with all popular browsers before using&lt;br /&gt;them.&lt;br /&gt;Do test with fewer screen colors&lt;br /&gt;256 colors still are popular and even 16 colors are used.&lt;br /&gt;Test your site with these color settings. Admitted, with 16 colors&lt;br /&gt;everything looks ugly. But be sure everything remains readable.&lt;br /&gt;Do test with lowres&lt;br /&gt;Test your pages with low resolution. If you don't use 640*480&lt;br /&gt;yourself, just make your browser-window smaller to get the same&lt;br /&gt;effect.&lt;br /&gt;Do validate&lt;br /&gt;Validate your pages. This is the best way to find errors that won't&lt;br /&gt;show up in some browsers but might be the cause of trouble in&lt;br /&gt;other browsers.&lt;br /&gt;Don't rely on plugins&lt;br /&gt;You can create astonishing effects with plugins like Flash and&lt;br /&gt;Shockwave.&lt;br /&gt;But don't rely on them.&lt;br /&gt;Many websurfers don't bother to install plugins. They won't be able&lt;br /&gt;to see the great effects you implemented. So offer an alternative or&lt;br /&gt;even better: check for plugins.&lt;br /&gt;100 Do's and Don'ts in Web Design SpiderPro&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;&lt;a href="http://www.swebsolution.150m.com/"&gt;compatibility&lt;br /&gt;Do check for browser version&lt;br /&gt;&lt;/a&gt;&lt;/strong&gt;&lt;br /&gt;If you want to use non-standard techniques (Netscape or Explorerspecific&lt;br /&gt;tags) do check the browser version on the client. And do&lt;br /&gt;provide an alternative for non compatible browsers.&lt;br /&gt;Do warn for important cookies&lt;br /&gt;Some pages rely on cookies. And some people disable cookies in&lt;br /&gt;their browsers.&lt;br /&gt;In many circumstances you just can't skip cookies. If so - supply a&lt;br /&gt;warning. Thus giving the visitor an opportunity to enable cookies.&lt;br /&gt;Do offer alternative views&lt;br /&gt;You want to use the newest emerging technologies? Then consider&lt;br /&gt;supplying an alternative view for those that don't use the latest&lt;br /&gt;browsers.&lt;br /&gt;100 Do's and Don'ts in Web Design SpiderPro&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;&lt;a href="http://www.swebsolution.150m.com/"&gt;General&lt;br /&gt;Do close popup screens&lt;/a&gt;&lt;/strong&gt;&lt;br /&gt;&lt;strong&gt;&lt;/strong&gt;&lt;br /&gt;Remove your litter.&lt;br /&gt;If you want to use popup screens, be sure to close them when the&lt;br /&gt;visitor leaves.&lt;br /&gt;Do stop sounds&lt;br /&gt;If you wanna use sounds, be sure to stop them when the visitor&lt;br /&gt;leaves.&lt;br /&gt;Don't try to mislead search engines&lt;br /&gt;Trying to mislead search engines by repeating keywords will turn&lt;br /&gt;against you. Search engines recognize many of these attempts and&lt;br /&gt;will react on it by giving your site a lower ranking. Or by not placing&lt;br /&gt;it at all.&lt;br /&gt;Don't try to misleadingly attract visitors&lt;br /&gt;By adding keywords that are specific to adult sites (like 'XXX') you&lt;br /&gt;might get some traffic. But those visitors will leave quickly - unless&lt;br /&gt;you do have an adult site, ofcourse.&lt;br /&gt;Do register your site&lt;br /&gt;Register your site at searchengines and directories. This is still the&lt;br /&gt;most important way to attract traffic.&lt;br /&gt;100 Do's and Don'ts in Web Design SpiderPro&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;&lt;a href="http://www.swebsolution.150m.com/"&gt;Do read the stats&lt;/a&gt;&lt;/strong&gt;&lt;br /&gt;&lt;br /&gt;Use the statistics of your site. It's a good opportunity to check for&lt;br /&gt;errors like the dreaded 404's.&lt;br /&gt;But statistics are also invaluable for marketing purposes. What are&lt;br /&gt;popular pages? Which countries do your visitors come from?&lt;br /&gt;Don't count hits&lt;br /&gt;Hits are meaningless. A page with 5 images produces 6 hits. And&lt;br /&gt;visited 3 times it produces 18 hits. If you want a counter on your&lt;br /&gt;page, be sure to count the sessions: real visitors.&lt;br /&gt;Do put contact info on your pages&lt;br /&gt;Give your visitors a way to get in touch with you. Their feedback is&lt;br /&gt;invaluable. Supply a e-mail address or a reply-form.&lt;br /&gt;Do beware of robots&lt;br /&gt;Search engines use robots that scan all your directories. Be sure to&lt;br /&gt;provide a robots.txt file to make clear which directories and files&lt;br /&gt;should not be indexed.&lt;br /&gt;100 Do's and Don'ts in Web Design SpiderPro&lt;br /&gt;&lt;br /&gt;&lt;a href="http://www.swebsolution.150m.com/"&gt;&lt;strong&gt;A concluding don't&lt;/strong&gt;&lt;br /&gt;&lt;/a&gt;&lt;br /&gt;Don't take my word for it&lt;br /&gt;Don't take my - or any other persons - word for it.&lt;br /&gt;There's no such thing as a constitution of webdesign.&lt;br /&gt;You might benefit by considering these do's and don'ts. But&lt;br /&gt;actually they're a personal view. And you just might disagree.&lt;br /&gt;But do give these 100 do's and don'ts a serious thought.&lt;br /&gt;They might fit more often than you think&lt;br /&gt;100 Do's and Don'ts in Web Design SpiderPro&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;About&lt;br /&gt;SpiderPro&lt;br /&gt;&lt;/strong&gt;&lt;br /&gt;This eBook is published by SpiderPro.&lt;br /&gt;SpiderPro is a website that presents&lt;br /&gt;information for professional&lt;br /&gt;webmasters.&lt;br /&gt;Well, with professional I don't want to&lt;br /&gt;frighten anyone. If you just want to build&lt;br /&gt;a nice personal homepage you'll find&lt;br /&gt;lots of info at SpiderPro.&lt;br /&gt;But you'll also find info that you can use&lt;br /&gt;only if you have access to a webserver.&lt;br /&gt;Or if you know how to write programs.&lt;br /&gt;With SpiderPro I hope to reach people&lt;br /&gt;that are strongly involved with web&lt;br /&gt;development. Maybe for a living, maybe&lt;br /&gt;just for the heck of it...&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;SpiderPro's mailing list informs you&lt;br /&gt;when new information becomes&lt;br /&gt;available. Subscription is free.&lt;br /&gt;If you subscribe, you'll receive mails&lt;br /&gt;about updates to SpiderPro and about&lt;br /&gt;new released ebooks.&lt;br /&gt;Expect to receive this mails about 2&lt;br /&gt;times a month.&lt;br /&gt;SpiderPro will use your email address&lt;br /&gt;for the sole purpose of sending you&lt;br /&gt;mails from the list.&lt;br /&gt;We will never use your address for any&lt;br /&gt;other purpose. We will never give your&lt;br /&gt;address to anyone and we will never&lt;br /&gt;sell your address.&lt;br /&gt;You can subscribe at:</description><thr:total xmlns:thr="http://purl.org/syndication/thread/1.0">0</thr:total></item><item><title>27 Dos Web Design</title><link>http://swebsolution.blogspot.com/2007/11/27-dos-and-donts-in-web-design.html</link><author>noreply@blogger.com (Unknown)</author><pubDate>Sun, 18 Nov 2007 23:31:00 -0800</pubDate><guid isPermaLink="false">tag:blogger.com,1999:blog-7227425366482465228.post-401888931746276430</guid><description>&lt;a name="669982127019910482"&gt;&lt;/a&gt;&lt;br /&gt;27 Dos Web Design&lt;br /&gt;&lt;br /&gt;&lt;a href="http://www.swebsolution.150m.com/"&gt;General information&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;You're allowed to distribute this ebook as long as you leave the orginal pdf-fileintact and you don't charge anything for it.I hope you'll find this styleguide usefulJan Kampherbeek101 Do's and Don'ts in Web Design SpiderPro-3-IndexGeneral information&lt;br /&gt;2Index&lt;br /&gt;3Content : 18 do's and don'ts 4Navigation: 16 do's and don'ts 8Design: 8 do's and don'ts 11Bandwidth: 9 do's and don'ts 13Presenting Text: 16 do's and don'ts 15Images and colors: 10 do's and don'ts 19Compatibility: 13 do's and don'ts 21General: 9 do's and don'ts 24A concluding don't 26About SpiderPro 27Disclaimer 27Release History 27100 Do's and Don'ts in Web Design SpiderPro-&lt;br /&gt;&lt;br /&gt;&lt;a href="http://www.swebsolution.150m.com/"&gt;Do know your audience&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;It's important to know your audience.If you write for a site that sells toys you'll use other words, colors,images etc. compared to a site for online banking.Write and design with your visitors in mind. Don't get tempted towrite for yourself.Do write about the subjectWrite about the subject.Saying: This page is about breeding goldfish talks about the page.Instead, start right away with the subject. Breeding goldfish is apopular hobby....Do use short sentencesUse short sentences.The World Wide Web is fast. Your visitors want to get your info in asnap. So read and reread your text. Then cut out as manyunnecessary words as possible.Do use correct spellingOK, this one will turn against me....Use correct English or whatever language your site is written in. Asa standard routine use a spelling checker but don't rely completelyon it. Human proof reading is necessary.This can be difficult - especially if you're not native speakingEnglish like your Dutch speaking author of this eBook.100 Do's and Don'ts in Web Design SpiderPro-&lt;br /&gt;&lt;br /&gt;&lt;a href="http://www.swebsolution.150m.com/"&gt;Don't use meaningless words&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;Do you have a cool site with hot subjects?Or a hot site with cool subjects?On some hype-sensitive sites these kind of words might be useful but onmost sites you'd better refrain from meaningless words.Don't use jargonAvoid jargon.That goes for Internet jargon but also for jargon for any othersubject.Only if your site is focused on a selective group of specialistsjargon might make sense.Don't write technicalDon't write technical. Your visitors don't care how you created yoursite and that you prefer Perl over TCL/TK (or the other wayaround).Instead write about your subject.Do use the first screenBe sure to put important text on the first part of your page, the partthat will show up first on a screen.Do present the issues right awayYour visitor wants to know immediately what she can find on yoursite. Keep that in mind when designing your site.Present the important issue(s) of your site on the first page.100 Do's and Don'ts in Web Design SpiderPro-&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;a href="http://www.swebsolution.150m.com/"&gt;Do use a descriptive title&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;The text for the tag</description><thr:total xmlns:thr="http://purl.org/syndication/thread/1.0">0</thr:total></item><item><title>27 Dos web Design</title><link>http://swebsolution.blogspot.com/2007/11/27-dos-web-design.html</link><author>noreply@blogger.com (Unknown)</author><pubDate>Sun, 18 Nov 2007 23:27:00 -0800</pubDate><guid isPermaLink="false">tag:blogger.com,1999:blog-7227425366482465228.post-6011555628324352035</guid><description>&lt;a href="http://www.swebsolution.150m.com/"&gt;&lt;strong&gt;Do comment on your links&lt;/strong&gt; &lt;/a&gt;&lt;br /&gt;&lt;br /&gt;Add value to your links by annotating them.&lt;br /&gt;You visited the linked site otherwise you wouldn't publish the link in&lt;br /&gt;the first place. Right? Share your knowledge and add a description.&lt;br /&gt;Do update your pages&lt;br /&gt;Be sure to check your pages on a regular base and to update them&lt;br /&gt;if necessary.&lt;br /&gt;Do show date of update&lt;br /&gt;You update your pages on a regular basis. Don't you?&lt;br /&gt;Make clear to your visitors how recent or (out-)dated your&lt;br /&gt;information is. Provide the date of the last update. And don't forget&lt;br /&gt;to change the date if you change a page...&lt;br /&gt;Do ask for feedback&lt;br /&gt;You can learn from your visitors.&lt;br /&gt;Ask for their feedback and give them an e-mail address to reach&lt;br /&gt;you.&lt;br /&gt;Don't show any page under construction&lt;br /&gt;Don't publish a page that's under construction.&lt;br /&gt;People will hate you if you do.&lt;br /&gt;If the page isn't finished, it's not ready to be published.&lt;br /&gt;In a sense most pages are always under construction because they&lt;br /&gt;are updated (more or less) frequently.&lt;br /&gt;100 Do's and Don'ts in Web Design SpiderPro&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;&lt;a href="http://www.swebsolution.150m.com/"&gt;Do use explicit addressing &lt;/a&gt;&lt;/strong&gt;&lt;br /&gt;&lt;strong&gt;&lt;br /&gt;&lt;/strong&gt;Navigation should be clear. Links like Back, Next, Previous, or&lt;br /&gt;clickable images of arrows, do point in an unclear direction.&lt;br /&gt;What is 'Back'. The page your visitor came from? The preceding&lt;br /&gt;page in your own website?&lt;br /&gt;Make navigation clear by supplying links like Chapter 12. The&lt;br /&gt;history of beekeeping.&lt;br /&gt;Do check your links&lt;br /&gt;OK, it's a cliché. But anyone who surfs the web will agree. Check&lt;br /&gt;your links frequently.&lt;br /&gt;Don't just check them to avoid 404 errors. You might find that an&lt;br /&gt;external link still works but that the content behind it has changed.&lt;br /&gt;Don't change links&lt;br /&gt;Figure out a good addressing scheme and stick to it.&lt;br /&gt;People will create links to your site. Be sure not to break these&lt;br /&gt;links.&lt;br /&gt;Do always supply textual links&lt;br /&gt;Supply textual links. Using only clickable images or image maps&lt;br /&gt;makes your site unusable for anybody that disables images.&lt;br /&gt;Do supply a link to home&lt;br /&gt;In the rare case people get lost in your site, a link to home comes&lt;br /&gt;in handy.&lt;br /&gt;Supply such a link on each page.&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;&lt;a href="http://www.swebsolution.150m.com/"&gt;Do use navigational aids at the top and the &lt;/a&gt;&lt;/strong&gt;&lt;br /&gt;&lt;/strong&gt;&lt;strong&gt;&lt;br /&gt;&lt;/strong&gt;bottom&lt;br /&gt;Supply navigational aids at the top and the bottom of your page. If&lt;br /&gt;you do, people probably won't need to scroll to navigate.&lt;br /&gt;Do use a table of contents&lt;br /&gt;Do use a table of contents, preferably as a menu. Without it your&lt;br /&gt;visitors will get lost.&lt;br /&gt;Do create a “what's new” page&lt;br /&gt;Returning visitors are interested in the latest additions on your site.&lt;br /&gt;Create a What's new page to supply that info.&lt;br /&gt;Do use short routes&lt;br /&gt;Supply short routes to information. Avoid too many menus and&lt;br /&gt;submenus, instead use larger menus with more items.&lt;br /&gt;People will appreciate it getting to the desired info quickly.&lt;br /&gt;Do keep menu items related&lt;br /&gt;Menu items should be related, don't mix them randomly.&lt;br /&gt;Try to share comparable items in one menu. You can use a larger&lt;br /&gt;menu for more itemgroups if you separate these groups in a clear&lt;br /&gt;way.&lt;br /&gt;Don't link to irrelevant pages&lt;br /&gt;Use only hyperlinks within the context of your page. People will feel&lt;br /&gt;lost if you try to use too many links.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;a href="http://www.swebsolution.150m.com/"&gt;&lt;strong&gt;Don't repeat links too often&lt;/strong&gt; &lt;/a&gt;&lt;br /&gt;&lt;/strong&gt;&lt;br /&gt;You shouldn't repeat links in the text. I.e. you have a page about&lt;br /&gt;beekeeping and want to link it to to a page that describes different&lt;br /&gt;kinds of honey. Then don't link every occurrence of the word&lt;br /&gt;honey.&lt;br /&gt;The only exception are links in a menu. You can repeat menulinks,&lt;br /&gt;i.e. on the top and on the bottom of your page.&lt;br /&gt;Don't use ambivalent navigation&lt;br /&gt;Navigation must be clear. Unless you run some kind of&lt;br /&gt;experimental site be sure to avoid experimental buttons that make&lt;br /&gt;visitors have to guess what they mean.&lt;br /&gt;Don't create dead end pages&lt;br /&gt;A dead end page is a page that is linked to by other pages but&lt;br /&gt;itself has no links.&lt;br /&gt;A visitor gets trapped in a dead end page and needs his&lt;br /&gt;backbutton to get away.&lt;br /&gt;Don't use dead end pages.&lt;br /&gt;Don't make prisoners&lt;br /&gt;You can imprison your visitors. I.e. by redirecting them to a page&lt;br /&gt;without taskbars and icons.&lt;br /&gt;But your prisoner will escape eventually and never return.&lt;br /&gt;Don't frame other sites&lt;br /&gt;You can load pages from other sites within a frame of your own&lt;br /&gt;site.&lt;br /&gt;Don't!&lt;br /&gt;It might ruin the look and feel of the framed site. And it gives the&lt;br /&gt;wrong idea that the framed site is a part of your own site.&lt;br /&gt;Load all pages in a full page.&lt;br /&gt;&lt;br /&gt;Design&lt;br /&gt;&lt;a href="http://www.swebsolution.150m.com/"&gt;&lt;strong&gt;Do use a consistent look and feel&lt;/strong&gt; &lt;/a&gt;&lt;br /&gt;&lt;br /&gt;Your site should stand out as a whole. Use the same look and feel&lt;br /&gt;for all the pages at your site. This way your visitors have a sense of&lt;br /&gt;recognition when they visit various pages. Using stylesheets makes&lt;br /&gt;it much easier to maintain the look and feel of numerous pages.&lt;br /&gt;Do use recurring visual elements&lt;br /&gt;Repeat visual elements (images, colors, fonts etc.) on several&lt;br /&gt;pages. This will add to a consistent look and feel.&lt;br /&gt;Don't use dark backgrounds&lt;br /&gt;Dark backgrounds tend to make text less readable. So avoid dark&lt;br /&gt;colors or dark backgroundimages. If you do need them, use a&lt;br /&gt;nonserif font for the text (like Arial, Universe, Helvetica) and be&lt;br /&gt;sure to not to use a small fontsize.&lt;br /&gt;Don't cram your pages&lt;br /&gt;A page with text pushed aside against the border of a table - or an&lt;br /&gt;image - looks awful. Don't cram your pages, use colspan and&lt;br /&gt;borderspan for tables and vspan and hspan for applets and&lt;br /&gt;images.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;&lt;a href="http://www.swebsolution.150m.com/"&gt;Don't push your table out of the screen&lt;/a&gt;&lt;/strong&gt;&lt;br /&gt;&lt;br /&gt;Tables are very flexible. They're able to get almost anything more&lt;br /&gt;or less visible on a screen.&lt;br /&gt;But by putting large elements in a table cell you might force the&lt;br /&gt;cells to become too large. Thus making horizontal scrolling&lt;br /&gt;necessary.&lt;br /&gt;So limit the number and size of pictures, long words (e.g. long&lt;br /&gt;links), predefined text etc.&lt;br /&gt;Don't overuse frames&lt;br /&gt;Use only a limited number of frames. Always check if the screen&lt;br /&gt;doesn't get crammed if a low resolution screen is used.&lt;br /&gt;Do vertical align the content of table cells&lt;br /&gt;You do you use table cells to get your info on the right position?&lt;br /&gt;Then be sure to align the content vertically.&lt;br /&gt;Don't mix horizontal aligning&lt;br /&gt;Mixing centered text and text that's left aligned makes a mess of&lt;br /&gt;your page.&lt;br /&gt;If you want to center text do it consistently.&lt;br /&gt;Exceptions are centering text of headers or centered text placed in&lt;br /&gt;a border.&lt;br /&gt;100 Do's and Don'ts in Web Design SpiderPro&lt;br /&gt;&lt;br /&gt;&lt;a href="http://www.swebsolution.150m.com/"&gt;&lt;strong&gt;Do use few colors in your GIF's&lt;/strong&gt; &lt;/a&gt;&lt;br /&gt;&lt;br /&gt;Minimize the number of colors in your GIF images.&lt;br /&gt;GIF's can be stored with a maximum of 256 colors. Minimizing the&lt;br /&gt;number of colors to 16, 8 or even 2 dramatically reduces the size of&lt;br /&gt;the GIF-file and therefore improves performance.&lt;br /&gt;Choose as few colors as possible without ruining the image. You&lt;br /&gt;might test both reducing colors with error correction or by selecting&lt;br /&gt;the nearest color.&lt;br /&gt;Do use high compression in JPEG&lt;br /&gt;Improve the performance of your site by reducing the size of your&lt;br /&gt;JPEG-images.&lt;br /&gt;JPEG can be saved with different compression-percentages. A&lt;br /&gt;high compression results in a smaller file size but also in a less&lt;br /&gt;perfect image. Test several compressions for each image you want&lt;br /&gt;to use. For different images the acceptable compressions will&lt;br /&gt;differ.&lt;br /&gt;Do reserve space for images&lt;br /&gt;Generally text arrives more quickly than an image. By reserving&lt;br /&gt;space for an image the browser is able to render the text. A visitor&lt;br /&gt;can start reading right away. Reserving space is done by defining&lt;br /&gt;the attributes vsize and hsize in the tag&lt;br /&gt;Do provide thumbnails for large images&lt;br /&gt;In some cases you do need large pictures that take a while to load.&lt;br /&gt;I.e. if you're running a website on modern art.&lt;br /&gt;In such a case do provide small copies of the original images&lt;br /&gt;(thumbnails) that link to the original ones.</description><thr:total xmlns:thr="http://purl.org/syndication/thread/1.0">0</thr:total></item><item><title/><link>http://swebsolution.blogspot.com/2007/11/27-dos-and-donts-in-web-design-general.html</link><author>noreply@blogger.com (Unknown)</author><pubDate>Sun, 18 Nov 2007 23:06:00 -0800</pubDate><guid isPermaLink="false">tag:blogger.com,1999:blog-7227425366482465228.post-669982127019910482</guid><description>&lt;div align="justify"&gt;&lt;strong&gt;27 Dos and Don'ts in Web Design&lt;br /&gt;&lt;/div&gt;&lt;/strong&gt;&lt;div align="justify"&gt;&lt;strong&gt;&lt;/strong&gt;&lt;/div&gt;&lt;div align="justify"&gt;&lt;strong&gt;&lt;/strong&gt;&lt;/div&gt;&lt;div align="justify"&gt;&lt;strong&gt;&lt;a href="http://www.swebsolution.150m.com/"&gt;General information&lt;/a&gt;&lt;/strong&gt;&lt;/div&gt;&lt;div align="justify"&gt;&lt;strong&gt;&lt;/strong&gt; &lt;/div&gt;&lt;strong&gt;&lt;div align="justify"&gt;&lt;/strong&gt;You're allowed to distribute this ebook as long as you leave the orginal pdf-file&lt;br /&gt;intact and you don't charge anything for it.&lt;br /&gt;I hope you'll find this styleguide useful&lt;br /&gt;Jan Kampherbeek&lt;br /&gt;101 Do's and Don'ts in Web Design SpiderPro&lt;br /&gt;&lt;/div&gt;&lt;div align="justify"&gt;&lt;br /&gt;Index&lt;br /&gt;General information 2&lt;br /&gt;Index 3&lt;br /&gt;Content : 18 do's and don'ts 4&lt;br /&gt;Navigation: 16 do's and don'ts 8&lt;br /&gt;Design: 8 do's and don'ts 11&lt;br /&gt;Bandwidth: 9 do's and don'ts 13&lt;br /&gt;Presenting Text: 16 do's and don'ts 15&lt;br /&gt;Images and colors: 10 do's and don'ts 19&lt;br /&gt;Compatibility: 13 do's and don'ts 21&lt;br /&gt;General: 9 do's and don'ts 24&lt;br /&gt;A concluding don't 26&lt;br /&gt;About SpiderPro 27&lt;br /&gt;Disclaimer 27&lt;br /&gt;Release History 27&lt;br /&gt;100 Do's and Don'ts in Web Design SpiderPro&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;&lt;a href="http://www.swebsolution.150m.com/"&gt;Do know your audience&lt;/a&gt;&lt;/strong&gt; &lt;/div&gt;&lt;div align="justify"&gt;&lt;br /&gt;It's important to know your audience.&lt;br /&gt;If you write for a site that sells toys you'll use other words, colors,&lt;br /&gt;images etc. compared to a site for online banking.&lt;br /&gt;Write and design with your visitors in mind. Don't get tempted to&lt;br /&gt;write for yourself.&lt;br /&gt;&lt;strong&gt;Do write about the subject&lt;br /&gt;&lt;/strong&gt;Write about the subject.&lt;br /&gt;Saying: This page is about breeding goldfish talks about the page.&lt;br /&gt;Instead, start right away with the subject. Breeding goldfish is a&lt;br /&gt;popular hobby....&lt;br /&gt;&lt;strong&gt;Do use short sentences&lt;br /&gt;&lt;/strong&gt;Use short sentences.&lt;br /&gt;The World Wide Web is fast. Your visitors want to get your info in a&lt;br /&gt;snap. So read and reread your text. Then cut out as many&lt;br /&gt;unnecessary words as possible.&lt;br /&gt;&lt;strong&gt;Do use correct spelling&lt;br /&gt;&lt;/strong&gt;OK, this one will turn against me....&lt;br /&gt;Use correct English or whatever language your site is written in. As&lt;br /&gt;a standard routine use a spelling checker but don't rely completely&lt;br /&gt;on it. Human proof reading is necessary.&lt;br /&gt;This can be difficult - especially if you're not native speaking&lt;br /&gt;English like your Dutch speaking author of this eBook.&lt;br /&gt;100 Do's and Don'ts in Web Design SpiderPro&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;&lt;a href="http://www.swebsolution.150m.com/"&gt;Don't use meaningless words&lt;/a&gt; &lt;/strong&gt;&lt;/div&gt;&lt;div align="justify"&gt;&lt;strong&gt;&lt;br /&gt;&lt;/strong&gt;Do you have a cool site with hot subjects?&lt;br /&gt;Or a hot site with cool subjects?&lt;br /&gt;On some hype-sensitive sites these kind of words might be useful but on&lt;br /&gt;most sites you'd better refrain from meaningless words.&lt;br /&gt;&lt;strong&gt;Don't use jargon&lt;/strong&gt;&lt;br /&gt;Avoid jargon.&lt;br /&gt;That goes for Internet jargon but also for jargon for any other&lt;br /&gt;subject.&lt;br /&gt;Only if your site is focused on a selective group of specialists&lt;br /&gt;jargon might make sense.&lt;br /&gt;Don't write technical&lt;br /&gt;Don't write technical. Your visitors don't care how you created your&lt;br /&gt;site and that you prefer Perl over TCL/TK (or the other way&lt;br /&gt;around).&lt;br /&gt;&lt;strong&gt;Instead write about your subject.&lt;br /&gt;&lt;/strong&gt;Do use the first screen&lt;br /&gt;Be sure to put important text on the first part of your page, the part&lt;br /&gt;that will show up first on a screen.&lt;br /&gt;Do present the issues right away&lt;br /&gt;Your visitor wants to know immediately what she can find on your&lt;br /&gt;site. Keep that in mind when designing your site.&lt;br /&gt;Present the important issue(s) of your site on the first page.&lt;br /&gt;100 Do's and Don'ts in Web Design SpiderPro&lt;br /&gt;&lt;br /&gt;content&lt;br /&gt;&lt;strong&gt;&lt;a href="http://www.swebsolution.150m.com/"&gt;Do use a descriptive title&lt;br /&gt;&lt;/a&gt;&lt;/strong&gt;The text for the tag &lt;/div&gt;</description><thr:total xmlns:thr="http://purl.org/syndication/thread/1.0">0</thr:total></item></channel></rss>