Archive for November, 2007

Chapter 8 Object-Based Programming 319 36 MessageBox.Show( “Radius (My space web page)

Saturday, November 10th, 2007

Chapter 8 Object-Based Programming 319 36 MessageBox.Show( “Radius = ” + constantValues.radius + 37 “nCircumference = ” + 38 2 * Constants.PI * constantValues.radius, 39 “Circumference” ); 40 41 } // end method Main 42 43 } // end class UsingConstAndReadOnly Fig. 8.15 Fig. 8.15Fig. 8.FiFi15g. 8.15g. 8.15constand readonlyclass member demonstration. (Part 2 of 2.) Line 11 in class Constantscreates constant PIusing keyword constand initializes PIwith the doublevalue 3.14159 an approximation of p that the program uses to calculate the circumferences of circles. Note that we could have used the predefined constant PI of class Math (Math.PI) as the value, but we wanted to demonstrate how to define a constvariable explicitly. The compiler must be able to determine a constvariable s value at compile time; otherwise, a compilation error will occur. For example, if line 11 initialized PIwith the expression: Double.Parse( “3.14159″ ) the compiler would generate an error. Although the expression uses string literal “3.14159″(a constant value) as an argument, the compiler cannot evaluate the method call Double.Parseat compile time. Variables declared readonly can be initialized at execution time. Line 15 declares readonly variable radius, but does not initialize it. The Constants constructor (lines 17 20) receives an intvalue and assigns it to radiuswhen the program creates a Constantsobject. Note that radius also can be initialized with a more complex expression, such as a method call that returns an int. Class UsingConstAndReadonly (lines 25 43) uses the const and readonly variables of class Constants. Lines 33 34 use a Randomobject to generate a random intbetween 1and 20that corresponds to a circle s radius, then pass that value to the Constants constructor to initialize the readonly variable radius. Lines 36 39 output the radius and circumference of a circle in a MessageBox. Line 36 uses Constants s reference contantValuesto access readonlyvariable radius. Line 38 computes the circle s circumference using const variable Constants.PI and readonlyvariable radius. Note that we use staticsyntax to access constvariable PI, because constvariables implicitly are static. 8.13 Indexers Sometimes a class encapsulates data that a program can manipulate as a list of elements. Such a class can define special properties called indexers that allow array-style indexed access to lists of elements. With conventional C# arrays, the subscript number must be an
Please visit our professional web hosting services to find out about cheap and reliable webhost service that will surely answer all your demands.

318 Object-Based Programming Chapter 8 Common Programming Error (Web hosting unlimited bandwidth)

Friday, November 9th, 2007

318 Object-Based Programming Chapter 8 Common Programming Error 8.10 Declaring a class data member as readonly and attempting to use it before it is initialized is a logic error. Members that are declared as constmust be assigned values at compile time. Therefore, constmembers can be initialized only with other constant values, such as integers, string literals, characters and other const members. Constant members with values that cannot be determined at compile time must be declared with keyword readonly. We mentioned previously that a readonlymember can be assigned a value only once, either when it is declared or within the constructor of the class. When initializing a static readonlymember in a constructor, a staticconstructor must be used. Figure 8.15 demonstrates constants. The program consists of two classes class Constants (lines 8 22) defines two constants, and class UsingConstAndReadonly (lines 25 43) demonstrates the constants in class Constants. 1 // Fig. 8.15: UsingConstAndReadOnly.cs 2 // Demonstrating constant values with const and readonly. 3 4 using System; 5 using System.Windows.Forms; 6 7 // Constants class definition 8 public class Constants 9 { 10 // create constant PI 11 public const double PI = 3.14159; 12 13 // radius is a constant 14 // that is uninitialized 15 public readonly int radius; 16 17 public Constants( int radiusValue ) 18 { 19 radius = radiusValue; 20 } 21 22 } // end class Constants 23 24 // UsingConstAndReadOnly class definition 25 public class UsingConstAndReadonly 26 { 27 // method Main creates Constants 28 // object and displays its values 29 static void Main( string[] args ) 30 { 31 Random random = new Random(); 32 33 Constants constantValues = 34 new Constants( random.Next( 1, 20 ) ); 35 Fig. 8.15 Fig. 8.15Fig. 8.FiFi15g. 8.15g. 8.15constand readonlyclass member demonstration. (Part 1 of 2.)
Looking for affordable and reliable webhost to host and run your business application? Then look no more and go to servlet web hosting services.

Chapter 8 Object-Based Programming 317 System) (Web domain) to make

Thursday, November 8th, 2007

Chapter 8 Object-Based Programming 317 System) to make this request. The garbage collector is not guaranteed to collect all objects that are currently available for collection. If the garbage collector decides to collect objects, the garbage collector first invokes the destructor of each object. It is important to understand that the garbage collector executes as an independent entity called a thread. (Threads are discussed in Chapter 14, Multithreading.) It is possible for multiple threads to execute in parallel on a multiprocessor system or to share a processor on a single-processor system. Thus, a program could run in parallel with garbage collection. For this reason, we call static method WaitForPendingFinalizers of class GC (line 37), which forces the program to wait until the garbage collector invokes the destructors for all objects that are ready for collection and reclaims those objects. When the program reaches lines 41, we are assured that both destructor calls completed and that the value of counthas been decremented accordingly. In this example, the output shows that the destructor was called for each Employee, which decrements the count value by two (once per Employee being collected). Lines 39 41 use property Count to obtain the value of count after invoking the garbage collector. If the objects had not been collected, the count would be greater than zero. Toward the end of the output, notice that the Employee object for BobJones was finalized before the Employee object for SusanBaker. However, the output of this program on your system could differ. The garbage collector is not guaranteed to collect objects in a specific order. 8.12 constand readonly Members C# allows programmers to create constants whose values cannot change during program execution. Testing and Debugging Tip 8.3 If a variable should never change, make it a constant. This helps eliminate errors that might occur if the value of the variable were to change. To create a constant data member of a class, declare that member using either the const or readonly keyword. Data members declared as const implicitly are static and must be initialized in their declaration. Data members declared as readonly can be initialized in their declaration or in their class s constructor. Neither const nor read- only values can be modified once they are initialized, except that readonly variables can be assigned values in several constructors (only one of which will be called when an object is initialized). Common Programming Error 8.7 Declaring a class data member as const but failing to initialize it in that class s declaration is a syntax error. Common Programming Error 8.8 Assigning a value to a const variable after that variable is initialized is a compilation error. Common Programming Error 8.9 The declaration of a const member as static is a syntax error, because a const member implicitly is static.
Please visit Domain Name Hosting services for high quality webhost to host and run your jsp applications.

316 Object-Based Programming Chapter 8 15 // create

Thursday, November 8th, 2007

316 Object-Based Programming Chapter 8 15 // create two Employees 16 Employee employee1 = new Employee( “Susan”, “Baker” ); 17 Employee employee2 = new Employee( “Bob”, “Jones” ); 18 19 Console.WriteLine( “nEmployees after instantiation: ” + 20 “Employee.Count = ” + Employee.Count + “n” ); 21 22 // display the Employees 23 Console.WriteLine( “Employee 1: ” + 24 employee1.FirstName + ” ” + employee1.LastName + 25 “nEmployee 2: ” + employee2.FirstName + 26 ” ” + employee2.LastName + “n” ); 27 28 // remove references to objects to indicate that 29 // objects can be garbage collected 30 employee1 = null; 31 employee2 = null; 32 33 // force garbage collection 34 System.GC.Collect(); 35 36 // wait until collection completes 37 System.GC.WaitForPendingFinalizers(); 38 39 Console.WriteLine( 40 “nEmployees after garbage collection: ” + 41 Employee.Count ); 42 } 43 } Employees before instantiation: 0 Employee object constructor: Susan Baker; count = 1 Employee object constructor: Bob Jones; count = 2 Employees after instantiation: Employee.Count = 2 Employee 1: Susan Baker Employee 2: Bob Jones Employee object destructor: Bob Jones; count = 1 Employee object destructor: Susan Baker; count = 0 Employees after garbage collection: 0 Fig. 8.14 Fig. 8.14Fig. 8.FiFi14g. 8.14g. 8.14staticmember demonstration. (Part 2 of 2.) The garbage collector is not invoked directly by the program. Either the garbage collector reclaims the memory for objects when the runtime determines garbage collection is appropriate, or the operating system recovers the memory when the program terminates. However, it is possible to request that the garbage collector attempt to collect available objects. Line 34 uses public static method Collect from class GC (namespace
Please visit Domain Name Hosting services for high quality webhost to host and run your jsp applications.

Anonymous web server - Chapter 8 Object-Based Programming 315 52 // static

Wednesday, November 7th, 2007

Chapter 8 Object-Based Programming 315 52 // static Count property 53 public static int Count 54 { 55 get 56 { 57 return count; 58 } 59 } 60 61 } // end class Employee Fig. 8.13 Fig. 8.13Fig. 8.FiFi13g. 8.13g. 8.13staticmembers are accessible to all objects of a class. (Part 2 of 2.) When objects of class Employeeexist, staticmember countcan be used in any method of an Employee object in this example, the constructor (lines 14 23) increments count, and the destructor (lines 26 32) decrements count. If no objects of class Employeeexist, the value of member countcan be obtained through staticproperty Count(lines 53 59); this also works when there are Employeeobjects in memory. Class StaticTest (Fig. 8.14) runs the application that demonstrates the static members of class Employee (Fig. 8.13). Lines 12 13 use the static property Count of class Employee to obtain the current count value before the program creates Employeeobjects. Notice that the syntax used to access a staticmember is: ClassName.StaticMember On line 13, ClassName is Employee and StaticMember is Count. Recall that we used this syntax in prior examples to call the staticmethods of class Math(e.g., Math.Pow, Math.Abs, etc.) and other methods, such as Int32.Parseand MessageBox.Show. Next, lines 16 17 instantiate two Employee objects and assign them to references employee1 and employee2. Each call to the Employee constructor increments the countvalue by one. Lines 19 26 display the value of Countas well as the names of the two employees. Lines 30 31 set references employee1 and employee2 to null, so they no longer refer to the Employee objects. Because these were the only references in the program to the Employeeobjects, those objects can now be garbage collected. 1 // Fig. 8.14: StaticTest.cs 2 // Demonstrating static class members. 3 4 using System; 5 6 // StaticTest class definition 7 class StaticTest 8 { 9 // main entry point for application 10 static void Main( string[] args ) 11 { 12 Console.WriteLine( “Employees before instantiation: ” + 13 Employee.Count + “n” ); 14 Fig. 8.14 Fig. 8.14Fig. 8.FiFi14g. 8.14g. 8.14staticmember demonstration. (Part 1 of 2.)
If you are searching for cheap webhost for your web application, please visit MySQL5 Web Hosting services.

314 Object-Based Programming Chapter 8 // Fig. 8.13: (Ftp web hosting)

Tuesday, November 6th, 2007

314 Object-Based Programming Chapter 8 // Fig. 8.13: Employee.cs // Employee class contains static data and a static method. using System; // Employee class definition public class Employee { private string firstName; private string lastName; private static int count; // Employee objects in memory // constructor increments static Employee count public Employee( string fName, string lName ) { firstName = fName; lastName = lName; ++count; Console.WriteLine( “Employee object constructor: ” + firstName + ” ” + lastName + “; count = ” + Count ); } // destructor decrements static Employee count ~Employee() { –count; Console.WriteLine( “Employee object destructor: ” + firstName + ” ” + lastName + “; count = ” + Count ); } // FirstName property public string FirstName { get { return firstName; } } // LastName property public string LastName { get { return lastName; } } Fig. 8.13 Fig. 8.13Fig. 8.FiFi13g. 8.13g. 8.13staticmembers are accessible to all objects of a class. (Part 1 of 2.)
Note: In case you are looking for affordable and reliable webhost to host and run your j2ee application check Vision J2ee Web Hosting services.

Chapter 8 Object-Based Programming (Disney web site) 313 We now consider

Monday, November 5th, 2007

Chapter 8 Object-Based Programming 313 We now consider a video-game example to justify the need for static class-wide data. Suppose that we have a video game involving Martians and other space creatures. Each Martian tends to be brave and willing to attack other space creatures when the Martian is aware that there are at least four other Martians present. If there are fewer than five Martians present, each Martian becomes cowardly. For this reason, each Martian must know the martianCount. We could endow class Martian with martianCount as instance data. However, if we were to do this, then every Martian would have a separate copy of the instance data, and, every time we create a Martian, we would have to update the instance variable martianCount in every Martian. The redundant copies waste space, and updating those copies is time-consuming. Instead, we declare martianCount to be static so that martianCount is class-wide data. Each Martian can see the martianCount as if it were instance data of that Martian, but C# maintains only one copy of the static variable martianCount to save space. This technique also saves time; because there is only one copy, we do not have to increment separate copies of martianCount for each Martian object. Performance Tip 8.2 When a single copy of the data will suffice, use static variables to save storage. Although static variables might seem like global variables (variables that can be referenced anywhere in a program) in other programming languages, static variables need not be globally accessible. static variables have class scope. The publicstatic data members of a class can be accessed through the class name using the dot operator (e.g., Math.PI). The privatestaticmembers can be accessed only through methods or properties of the class. staticmembers are available as soon as the class is loaded into memory at execution time and they exist for the duration of program execution, even when no objects of that class exist. To enable a program to access a private static member when no objects of the class exist, the class must provide a publicstatic method or property. A static method cannot access instance (non-static) members. Unlike instance methods, a static method has no this reference, because static variables and static methods exist independently of any class objects, even when there are no objects of that class. Common Programming Error 8.5 Using the this reference in a static method or static property is a compilation error. Common Programming Error 8.6 A call to an instance method or an attempt to access an instance variable from a static method is a compilation error. Class Employee (Fig. 8.13) demonstrates a publicstatic property that enables a program to obtain the value of a privatestatic variable. The static variable count (line 11) is not initialized explicitly, so it receives the value zero by default. Class variable count maintains a count of the number of objects of class Employee that have been instantiated, including those objects that have already been marked for garbage collection, but have not yet been reclaimed by the garbage collector.
You want to have a cheap webhost for your apache application, then check apache web hosting services.

312 Object-Based Programming Chapter 8 (Web site builder) Fig. 8.12 Fig.

Sunday, November 4th, 2007

312 Object-Based Programming Chapter 8 Fig. 8.12 Fig. 8.12Fig. 8.FiFi12g. 8.12g. 8.12thisreference demonstration. (Part 2 of 2.) Unlike C and C++, in which programmers must manage memory explicitly, C# performs memory management internally. The .NET Framework performs garbage collection of memory to return to the system memory that is no longer needed. When the garbage collector executes, it locates objects for which the application has no references. Such objects can be collected at that time or during a subsequent execution of the garbage collector. Therefore, the memory leaks that are common in such languages as C and C++, where memory is not reclaimed automatically, are rare in C#. Allocation and deallocation of other resources, such as network connections, database connections and files, must be handled explicitly by the programmer. One technique employed to handle these resources (in conjunction with the garbage collector) is to define a destructor (sometimes known as a finalizer) that returns resources to the system. The garbage collector calls an object s destructor to perform termination housekeeping on that object just before the garbage collector reclaims the object s memory (called finalization). Each class can contain only one destructor. The name of a destructor is formed by preceding the class name with a ~character. For example, the destructor for class Timewould be ~Time(). Destructors do not receive arguments, so destructors cannot be overloaded. When the garbage collector is removing an object from memory, the garbage collector first invokes that object s destructor to clean up resources used by the class. However, we cannot determine exactly when the destructor is called, because we cannot determine exactly when garbage collection occurs. At program termination, any objects that have not been not garbage collected previously will receive destructor calls. 8.11 staticClass Members Each object of a class has its own copy of all the instance variables of the class. However, in certain cases, all class objects should share only one copy of a particular variable. Such variables are called static variables. A program contains only one copy of each of a class s staticvariables in memory, no matter how many objects of the class have been instantiated. A staticvariable represents class-wide information all class objects share the same staticdata item. The declaration of a staticmember begins with the keyword static. A static variable can be initialized in its declaration by following the variable name with an =and an initial value. In cases where a static variable requires more complex initialization, programmers can define a static constructor to initialize only the static members. Such constructors are optional and must be declared with the statickeyword, followed by the name of the class. staticconstructors are called before any staticmembers are used and before any class objects are instantiated.
From our experience, we can recommend PHP Web Hosting services, if you need affordable webhost to host and run your web application.

Chapter 8 Object-Based Programming (My web site) 311 Testing and Debugging

Saturday, November 3rd, 2007

Chapter 8 Object-Based Programming 311 Testing and Debugging Tip 8.2 Avoid method-parameter names (or local variable names) that conflict with instance variable names to prevent subtle, hard-to-trace bugs. Good Programming Practice 8.6 The explicit use of the this reference can increase program clarity in some contexts where this is optional. Class ThisTest(Fig. 8.12) runs the application that demonstrates explicit use of the this reference. Line 13 instantiates an instance of class Time4. Lines 15 16 invoke method BuildStringof the Time4object, then display the results to the user in a MessageBox. The problem of parameters (or local variables) hiding instance variables can be solved by using properties. If we have a property Hourthat accesses the hourinstance variable, then we would not need to use this.hourto distinguish between a parameter (or local variable) hourand the instance variable hour we would simply assign hourto Hour. 8.10 Garbage Collection In previous examples, we have seen how a constructor initializes data in an object of a class after the object is created. Operator newallocates memory for the object, then calls that object s constructor. The constructor might acquire other system resources, such as network connections and databases or files. Objects must have a disciplined way to return memory and release resources when the program no longer uses those objects. Failure to release such resources causes resource leaks potentially exhausting the pool of available resources that programs might need to continue executing. 1 // Fig. 8.12: ThisTest.cs 2 // Using the this reference. 3 4 using System; 5 using System.Windows.Forms; 6 7 // ThisTest class definition 8 class ThisTest 9 { 10 // main entry point for application 11 static void Main( string[] args ) 12 { 13 Time4 time = new Time4( 12, 30, 19 ); 14 15 MessageBox.Show( time.BuildString(), 16 “Demonstrating the “this” Reference” ); 17 } 18 } Fig. 8.12 Fig. 8.12Fig. 8.FiFi12g. 8.12g. 8.12thisreference demonstration. (Part 1 of 2.)
Visit our web design programs services for an affordable and reliable webhost to suit all your needs.

Web server certificate - 310 Object-Based Programming Chapter 8 1 // Fig.

Friday, November 2nd, 2007

310 Object-Based Programming Chapter 8 1 // Fig. 8.11: Time4.cs 2 // Class Time4 provides overloaded constructors. 3 4 using System; 5 6 // Time4 class definition 7 public class Time4 8 { 9 private int hour; // 0-23 10 private int minute; // 0-59 11 private int second; // 0-59 12 13 // constructor 14 public Time4( int hour, int minute, int second ) 15 { 16 this.hour = hour; 17 this.minute = minute; 18 this.second = second; 19 } 20 21 // create string using this and implicit references 22 public string BuildString() 23 { 24 return “this.ToStandardString(): ” + 25 this.ToStandardString() + 26 “nToStandardString(): ” + ToStandardString(); 27 } 28 29 // convert time to standard-time (12-hour) format string 30 public string ToStandardString() 31 { 32 return String.Format( “{0}:{1:D2}:{2:D2} {3}”, 33 ( ( this.hour == 12 || this.hour == 0 ) ? 12 : 34 this.hour % 12 ), this.minute, this.second, 35 ( this.hour < 12 ? "AM" : "PM" ) ); 36 } 37 38 } // end class Time4 Fig. 8.11 Fig. 8.11Fig. 8.FiFi11g. 8.11g. 8.11thisreference used implicitly and explicitly to enable an object to manipulate its own data and invoke its own methods. (Part 1 of 2) Method BuildString (lines 22 27) returns a string created by a statement that uses the thisreference explicitly and implicitly. Line 25 uses the thisreference explicitly to call method ToStandardString, whereas line 26 uses the this reference implicitly to call the same method. Note that both lines perform the same task. Therefore, programmers usually do not use the thisreference explicitly to reference methods within the current object. Common Programming Error 8.4 For a method in which a parameter (or local variable) has the same name as an instance variable, use reference this if you wish to access the instance variable; otherwise, the method parameter (or local variable) will be referenced.
From our experience, we can recommend PHP Web Hosting services, if you need affordable webhost to host and run your web application.