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

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.

Leave a Reply