Archive for July, 2007

202 Methods Chapter 6 using System.Collections; 6 using (Best web site)

Thursday, July 26th, 2007

202 Methods Chapter 6 using System.Collections; 6 using System.ComponentModel; 7 using System.Windows.Forms; 8 using System.Data; 9 public class RandomInt : System.Windows.Forms.Form 11 { 12 private System.Windows.Forms.Button showOutputButton; 13 private System.Windows.Forms.Label outputLabel; 14 // Visual Studio .NET generated code 16 17 // the main entry point for the application 18 [STAThread] 19 static void Main() { 21 Application.Run( new RandomInt() ); 22 } 23 24 private void showOutputButton_Click( object sender, System.EventArgs e ) 26 { 27 Random randomInteger = new Random(); 28 29 outputLabel.Text = “”; 31 // loop 20 times 32 for ( int counter = 1; counter <= 20; counter++ ) 33 { 34 // pick random integer between 1 and 6 int nextValue = randomInteger.Next( 1, 7 ); 36 37 outputLabel.Text += 38 nextValue + " "; // append value to output 39 // add newline after every 5 values 41 if ( counter % 5 == 0 ) 42 outputLabel.Text += "n"; 43 } 44 } } Fig. 6.9 Random integers in the range 1 6. (Part 2 of 2.)
Searching for affordable and proven webhost to host and run your servlet applications? Go to Linux Web Hosting services and you will find it.

Chapter 6 Methods 201 (Web server iis) Consider the following statements:

Thursday, July 26th, 2007

Chapter 6 Methods 201 Consider the following statements: Random randomObject = new Random(); int randomNumber = randomObject.Next(); The Next method generates a positive int value between zero and the constant Int32.MaxValue(the value 2,147,483,647). If Next produces values at random, every value in this range has an equal chance (or probability) of being chosen when Next is called. Note that values returned by Next are actually pseudo-random numbers a sequence of values produced by a complex mathematical calculation. A seed value is required in this mathematical calculation. When we create our Random object, we use the current time of day as the seed. A particular seed value always produces the same series of random numbers. Programmers commonly use the current time of day as a seed value, since it changes each second and, therefore produces different random-number sequences each time the program executes. The range of values produced directly by Next often is different from the range of values required in a particular application. For example, a program that simulates coin- tossing might require only 0 for heads and 1 for tails. A program that simulates rolling a six-sided die would require random integers in the range 1 6. A video-game program that randomly predicts the next type of spaceship (out of four possibilities) that will fly across the horizon might require random integers in the range 1 4. The one-argument version of method Next returns values in the range from 0 up to (but not including) the value of that argument. For example, value = randomObject.Next( 6 ); produces values from 0 through 5. This is called scaling, because the range of values produced has been scaled down from over two billion to only six. The number 6 is the scaling factor. The two-argument version of method Next allows us to shift and scale the range of numbers. For example, we can use method Next as follows value = randomObject.Next( 1, 7 ); to produce integers in the range from 1 to 6. In this case, we have shifted the numbers to produce a range from 1 up to (but not including) 7. The Windows application of Fig. 6.9 simulates 20 rolls of a six-sided die and shows the integer value of each roll. The dice-rolling simulation begins when the user clicks the Show Ouput button, which invokes the showOutputButton_Click event handler (lines 24 44). The for loop on lines 32 43 repeatedly invokes method Next of class Random to simulate rolling the die. Lines 37 38 append the value rolled to outputLabel s Text property. After every five rolls, line 42 appends a newline character to make the output more readable. 1 // Fig. 6.9: RandomInt.cs 2 // Generating random integer values. 3 using System; 4 using System.Drawing; Fig. 6.9 Random integers in the range 1 6. (Part 1 of 2.)
We highly recommend you visit web and email hosting services if you need stable and cheap web hosting platform for your web applications.

200 Methods Chapter 6 76 77 } //

Wednesday, July 25th, 2007

200 Methods Chapter 6 76 77 } // end method showOutputButton_Click 78 } Fig. 6.8 Demonstrating ref and out parameters. (Part 3 of 3.) Method showOutputButton_Click (lines 46 77) is an event handler that invokes methods SquareRef, SquareOut and Square when the user clicks the Show Output button. This method begins by initializing y to 5 and declaring (but not initializing) z. Lines 58 59 call methods SquareRef and SquareOut. Notice the syntax used for passing y and z in each case, we precede the argument either with refor with out. The output displays the values of y and z after the function calls. Notice that y has been changed to 25 and z has been set to 36. Finally, on lines 69 70 we call method Square. Arguments y and zboth are passed by value only copies of their values are passed to the method. As a result, the values of y and z remain 25 and 36, respectively. Common Programming Error 6.12 The ref and out arguments in a method call must match those specified in the method definition; otherwise, a syntax error occurs. Software Engineering Observation 6.10 By default, C# does not allow the programmer to choose whether to pass each argument by value or by reference. Value-type variables are passed by value. Objects are not passed to methods; rather, references to objects are passed to methods. The references themselves are passed by value. When a method receives a reference to an object, the method can manipulate the object directly, but the reference value cannot be changed (e.g., to refer to a new object). 6.10 Random-Number Generation We now take a brief and hopefully entertaining diversion into a popular programming application simulation and game playing. In this section and the next, we develop a nicely structured game-playing program that includes multiple methods. The program uses most of the control structures we have studied to this point and also introduces several new concepts. There is something in the air of a gambling casino that invigorates every type of person from the high rollers at the plush mahogany-and-felt craps tables to the quarter poppers at the one-armed bandits. It is the element of chance, the possibility that luck will convert a pocketful of money into a mountain of wealth. The element of chance can be introduced into computer applications with the Random class (located in namespace System).
In case you need quality webspace to host and run your web applications, try our personal web hosting services.

Multiple domain web hosting - Chapter 6 Methods 199 23 24 // x

Wednesday, July 25th, 2007

Chapter 6 Methods 199 23 24 // x passed by reference and method modifies 25 // original variable’s value 26 void SquareRef( ref int x ) 27 { 28 x = x * x; 29 } 30 31 // x passed as out parameter and method initializes 32 // and modifies original variable’s value 33 void SquareOut( out int x ) 34 { 35 x = 6; 36 x = x * x; 37 } 38 39 // x passed by value and method cannot modify 40 // original variable’s value 41 void Square( int x ) 42 { 43 x = x * x; 44 } 45 46 private void showOutputButton_Click( object sender, 47 System.EventArgs e ) 48 { 49 int y = 5; // create new int and initialize to 5 50 int z; // declare z, but do not initialize it 51 52 // display original values of y and z 53 outputLabel.Text = “Original value of y: ” + y + “n”; 54 outputLabel.Text += 55 “Original value of z: uninitializednn”; 56 57 // pass y and z by reference 58 SquareRef( ref y ); 59 SquareOut( out z ); 60 61 // display values of y and z after modified by methods 62 // SquareRef and SquareOut 63 outputLabel.Text += 64 “Value of y after SquareRef: ” + y + “n”; 65 outputLabel.Text += 66 “Value of z after SqaureOut: ” + z + “nn”; 67 68 // pass y and z by value 69 Square( y ); 70 Square( z ); 71 72 // display unchanged values of y and z 73 outputLabel.Text += “Value of y after Square: ” + y + “n”; 74 outputLabel.Text += “Value of z after Square: ” + z + “n”; 75 Fig. 6.8 Demonstrating ref and out parameters. (Part 2 of 3.)
If you are in need for chaep and reliable webhost to host your website, our recommendation is http web server services.

198 Methods Chapter 6 In Section 6.8, we (Domain and web hosting)

Tuesday, July 24th, 2007

198 Methods Chapter 6 In Section 6.8, we discussed the difference between value types and reference types. At this point, the reader can understand one of the major differences between the two data types value-type variables are passed to methods by value, whereas reference-type variables are passed to methods by reference. What if the programmer would like to pass a value type by reference? To do this, C# provides the ref and out keywords. The ref keyword specifies that a value-type argument should be passed by reference, which enables the called method to modify the original variable. This keyword is used for variables that already have been initialized. The out keyword specifies an output parameter, which is an argument to which the called method will assign a value. Normally, when a method receives an uninitialized value, the compiler generates an error. Preceding the parameter with keyword out specifies that the called method will initialize the variable and prevents the compiler from generating an error message for the uninitialized variable. Figure 6.8 demonstrates using the ref and out keywords to manipulate integer values.2 This program contains three methods to calculate the square of an integer. The first method, SquareRef (lines 26 29), multiplies its argument x by itself and assigns the new value to x. SquareRef receives its argument as a refint, specifying that xis an integer that is passed by reference to the method. As a result, the assignment at line 28 modifies the original argument s value, rather than a copy of that value. The second method, SquareOut (lines 33 37), does the same thing, but initializes x to 6 on line 35. SquareOut receives its argument as an outint, which indicates that x is an integer variable that the caller passes to method SquareOut by reference and that SquareOutcan assign a new value to this variable. The final method, Square (lines 41 44), simply takes x as a value-type integer argument and squares its value. 1 // Fig. 6.8: RefOutTest.cs 2 // Demonstrating ref and out parameters. 3 using System; 4 using System.Drawing; 5 using System.Collections; 6 using System.ComponentModel; 7 using System.Windows.Forms; 8 using System.Data; 9 10 public class RefOutTest : System.Windows.Forms.Form 11 { 12 private System.Windows.Forms.Button showOutputButton; 13 private System.Windows.Forms.Label outputLabel; 14 15 // Visual Studio .NET generated code 16 17 // main entry point for application 18 [STAThread] 19 static void Main() 20 { 21 Application.Run( new RefOutTest() ); 22 } Fig. 6.8 Demonstrating ref and out parameters. (Part 1 of 3.) 2. In Chapter 7 we discuss passing reference-type arguments by value and by reference.
You want to have a cheap webhost for your apache application, then check apache web hosting services.

Chapter 6 Methods 197 Type Size in bits (Web hosting reviews)

Tuesday, July 24th, 2007

Chapter 6 Methods 197 Type Size in bits Values Standard double 64 5.0 10-324 to 1.7 10308 (IEEE 754 floating point) object string (Unicode character set) Fig. 6.7 C# built-in data types. (Part 2 of 2.) In C and C++ programs, programmers frequently must write separate program versions to support different computer platforms because the primitive data types are not guaranteed to be identical from computer to computer. For example, an int value on one computer might occupy 16 bits (2 bytes) of memory, whereas an int value on another computer might occupy 32 bits (4 bytes) of memory. In C#, int values are always 32 bits (4 bytes). Portability Tip 6.1 Primitive data types in C# are portable across all platforms that support C#. Each data type in the table is listed with its size in bits (there are 8 bits to a byte) and its range of values. The designers of C# wanted code to be portable; therefore, they chose to use internationally recognized standards for both character formats (Unicode) and floating-point numbers (IEEE 754). 6.9 Passing Arguments: Pass-by-Value vs. Pass-by-Reference Two ways to pass arguments to methods in many programming languages are pass-by-value and pass-by-reference. When an argument is passed by value, the called method receives a copy of the argument s value. Testing and Debugging Tip 6.2 With pass-by-value, changes to the called method s copy do not affect the original variable s value. This prevents some possible side effects that hinder the development of correct and reliable software systems. When an argument is passed using pass-by-reference, the caller gives the method the ability to access and modify the caller s original data directly. Pass-by-reference can improve performance because it eliminates the overhead of copying large data items such as objects; however, pass-by-reference can weaken security because the called method can modify the caller s data. Software Engineering Observation 6.9 When returning information from a method via a return statement, value-type variables always are returned by value (i.e., a copy is returned), and reference-type variables are always returned by reference (i.e., a reference to the object is returned). To pass an object reference into a method, simply specify the reference name in the method call. Then, in the method body, reference the object using the parameter name. This refers to the original object in memory, which allows the called method to access the original object directly.
From our experience, we are can tell you that you can find a reliable and cheap webhost service at Java Web Hosting services.

196 Methods Chapter 6 The set of namespaces

Monday, July 23rd, 2007

196 Methods Chapter 6 The set of namespaces available in the FCL is quite large. In addition to the namespaces summarized in Fig. 6.6, the FCL includes namespaces for complex graphics, advanced graphical user interfaces, printing, advanced networking, security, multimedia, accessibility (for people with disabilities) and many more. For an overview of the namespaces in the FCL, look up .NET Framework, class library in the help documentation. 6.8 Value Types and Reference Types In the next section, we will discuss passing arguments to methods by value and by reference. To understand this, we first need to make a distinction between types in C#. Data types are either value types or reference types. A variable of a value type contains data of that type. A variable of a reference type, in contrast, contains the address of the location in memory where the data are stored. Value types normally represent single pieces of data, such as int or bool values. Reference types, on the other hand, refer to objects, which can contain many individual pieces of data. We discuss objects in detail in Chapters 8, 9 and 10 (Object-Based Programming, and Object-Oriented Programming parts 1 and 2). C# includes built-in value types and reference types. The built-in value types are the integral types (sbyte, byte, char, short, ushort, int, uint, long and ulong), the floating-point types (float and double) and the types decimal and bool. The built-in reference types are string and object. Programmers also can create value types and reference types. The reference types that programmers can create are classes (Chapter 8) , interfaces (Chapter 8) and delegates (Chapter 9). The table in Fig. 6.7 lists the primitive data types, which are building blocks for more complicated types. Like its predecessor languages C and C++, C# requires all variables to have a type before they can be used in a program. For this reason, C# is referred to as a strongly typed language. Type Size in bits Values Standard bool 8 trueor false char 16 ‘u0000′to ‘uFFFF’ (Unicode character set) byte 8 0 to 255 (unsigned) sbyte 8 -128 to +127 short 16 32,768 to +32,767 ushort 16 0 to 65,535 (unsigned) int 32 2,147,483,648 to 2,147,483,647 uint 32 0 to 4,294,967,295 (unsigned) long 64 9,223,372,036,854,775,808 to +9,223,372,036,854,775,807 ulong 64 0 to 18,446,744,073,709,551,615 (unsigned) decimal 128 1.0 x 10-28 to 7.9 x 1028 float 32 (IEEE 754 floating point) 1.5 10-45 to 3.4 1038 Fig. 6.7 C# built-in data types. (Part 1 of 2.)
From our experience, we can recommend PHP Web Hosting services, if you need affordable webhost to host and run your web application.

Web hosting bandwidth - Chapter 6 Methods 195 int result = Square(

Monday, July 23rd, 2007

Chapter 6 Methods 195 int result = Square( ( int ) y ); This statement explicitly casts (converts) a copy of the value of y to an integer for use in method Square. Thus, if y s value is 4.5, method Square returns 16, not 20.25. Common Programming Error 6.11 When performing a narrowing conversion (e.g., double to int), converting a primitive- data-type value to another primitive data type may change the value. Also, converting any integral value to a floating-point value and back to an integral value may introduce rounding errors into the result. 6.7 C# Namespaces As we have seen, C# contains many predefined classes that are grouped into namespaces. Collectively we refer to this preexisting code as the Framework Class Library. The actual code for the classes is located in .dll files called assemblies. Throughout the text, using statements specify the namespaces we use in each program. For example, a program includes the statement using System; to tell the compiler that we are using the System namespace. This using statement allows us to write Console.WriteLine rather than System.Console.WriteLine throughout the program. To use a class in a particular namespace, we must add a reference to the appropriate assembly (demonstrated in Section 3.2). Assembly references for namespace System are added automatically other assemblies must be added explicitly. We exercise a large number of the FCL classes in this book. Figure 6.6 lists a subset of the many namespaces in the FCL and provides a brief description of each. We use classes from these namespaces and others throughout the book. This table introduces readers to the variety of reusable components in the FCL. When learning C#, spend time reading the descriptions of the classes in the documentation. Namespace Description System Contains essential classes and data types (such as int, double, char, etc.). Implicitly referenced by all C# programs. System.Data Contains classes that form ADO .NET, used for database access and manipulation. System.Drawing Contains classes used for drawing and graphics. System.IO Contains classes for the input and output of data, such as with files. System.Threading Contains classes for multithreading, used to run multiple parts of a program simultaneously. System.Windows.Forms Contains classes used to create graphical user interfaces. System.Xml Contains classes used to process XML data. Fig. 6.6 Namespaces in the Framework Class Library.
Visit our web design programs services for an affordable and reliable webhost to suit all your needs.

194 Methods Chapter 6 Type Can be Converted (Unlimited web hosting)

Monday, July 23rd, 2007

194 Methods Chapter 6 Type Can be Converted to Type(s) bool object byte decimal, double, float, int, uint, long, ulong, object, shortor ushort sbyte decimal, double, float, int, long, objector short char decimal, double, float, int, uint, long, ulong, objector ushort decimal object double object float doubleor object int decimal, double, float, longor object uint decimal, double, float, long, ulong, or object long decimal, double, floator object ulong decimal, double, floator object short decimal, double, float, int, longor object ushort decimal, double, float, int, uint, long, ulongor object Fig. 6.5 Allowed implicit conversions. For example, the Math class method Sqrt can be called with an integer argument, even though the method is defined in class Mathto receive a double argument. The statement Console.WriteLine( Math.Sqrt( 4 ) ); correctly evaluates Math.Sqrt(4) and displays the value 2. C# implicitly converts the int value 4 to the double value 4.0 before passing the value to Math.Sqrt. In many cases, C# applies implicit conversions to argument values that do not correspond precisely to the parameter types in the method definition. In some cases, attempting these conversions leads to compiler errors because C# uses conversion rules to determine when a widening conversion can occur. In our previous Math.Sqrt example, C# converts an int to a double without changing its value. However, converting a double to an int truncates the fractional part of the double value. Converting large integer types to small integer types (e.g., long to int) also can result in changed values. Such narrowing conversions can lose data; therefore, C# does not allow narrowing conversions without an explicit cast operation. The conversion rules apply to expressions containing values of two or more data types (also referred to as mixed-type expressions) and to primitive data-type values passed as arguments to methods. C# converts the type of each value in a mixed-type expression to the highest type in the expression. C# creates a temporary copy of each value and uses it in the expression the original values remain unchanged. A method argument s type can be promoted to any higher type. Converting values to lower types can result in data loss. In cases where information could be lost through conversion, the compiler requires the programmer to use a cast to force the conversion to occur. To invoke our Square method, which takes an integer parameter (Fig. 6.3) with the double variable y, the method call would be written as
We would like to recommend you tested and proved virtual web hosting services, which you will surely find to be of great quality.

Web design service - Chapter 6 Methods 193 The graphical user interface

Sunday, July 22nd, 2007

Chapter 6 Methods 193 The graphical user interface for this program consists of three TextBoxes in which the user can enter floating-point numbers, a Button for calculating the maximum, Labels for each TextBox and a Label for displaying the maximum value. Lines 31 182 contain the Visual Studio .NET generated code for constructing this graphical user interface. Lines 31 42 define a special type of method called a constructor. Programs invoke constructors to create objects. The constructor performs tasks necessary for preparing an object for use in a program. We discuss constructors in detail in Chapter 8. In the case of Windows applications, the constructor invokes method InitializeComponent to create the program s graphical user interface (line 36). Method Initialize- Component (lines 64 181) configures and arranges the program s graphical user interface component objects, such as its Labels, Buttons and TextBoxes. When the user closes a program s window, the system invokes method Dispose (lines 47 57) to clean up resources used by the Window. To create the graphical user interface for this program, drag the appropriate components from the Toolbox onto the Form in the Windows Form Designer. Arrange the components as shown in the screen capture of Fig. 6.4 and set the Text properties for the Labels and Button. Then, double click the Calculate Maximum button to add an empty event handler. Fill in this empty event handler with the code shown on lines 202 221. Lines 206 213 invoke Double method Parse on the Text property of each TextBox to retrieve the values that the user entered. Line 216 then invokes our Maximum method to determine which value is the largest. Method Maximum provides the largest number as its return value, which line 216 stores in double variable maximum. Line 219 appends the maximum value to the maximumLabel s Text property to display the result to the user. Now let us examine the implementation of method Maximum(lines 195 198). The first line indicates that the method returns a double floating-point value, that the method s name is Maximum and that the method takes three double parameters (x, y and z). The statement in the body of the method (line 197) returns the largest of the three floating-point values using two calls to method Math.Max. First, method Math.Max is invoked and passed the values of variables y and z to determine the larger of these two values. Next, the value of variable x and the result of the first call to Math.Max are passed to method Math.Max. Finally, the result of the second call to Math.Max is returned to the caller. 6.6 Argument Promotion Another important feature of method definitions is the coercion of arguments (i.e., forcing arguments to the appropriate type to pass to a method). This process commonly is referred to as implicit conversion, in that a copy of the variable s value is converted to a different type without an explicit cast. Explicit conversion occurs when an explicit cast specifies that conversion is to occur. Such conversions also can be done with class Convert in namespace System. C# supports both widening and narrowing conversions widening conversion occurs when a type is converted to other types (usually types that can hold more data) without losing data, and a narrowing conversion occurs when data may be lost through a conversion (usually to types that hold a smaller amount of data). Figure 6.5 shows allowed implicit conversions.
We highly recommend you visit web and email hosting services if you need stable and cheap web hosting platform for your web applications.