Web host forum - Chapter 6 Methods 213 able or reference declared

Chapter 6 Methods 213 able or reference declared in a block can be used only in that block or in blocks nested within that block. The possible scopes for an identifier are class scope and block scope. Members of a class have class scope and are visible in what is known as the declaration space of a class. Class scope begins at the opening left brace ({) of the class definition and terminates at the closing right brace (}). Class scope enables methods of a class to access all members defined in that class. In Chapter 8, Object-Based Programming, we see that static members are an exception to this rule. In a sense, all instance variables and methods of a class are global to the methods of the class in which they are defined (i.e., the methods can modify the instance variables directly and invoke other methods of the class). Identifiers declared inside a block have block scope (local-variable declaration space). Block scope begins at the identifier s declaration and ends at the block s terminating right brace (}). Local variables of a method have block scope, as do method parameters, which are local variables of the method. Any block may contain variable declarations. When blocks are nested in a method s body, and an identifier declared in an outer block has the same name as an identifier declared in an inner block, an error is generated. On the other hand, if a local variable in a method has the same name as an instance variable, the value in the calling method (main program) is hidden until the method terminates execution. In Chapter 8, Object-Based Programming, we discuss how to access such hidden instance variables. The reader should note that block scope also applies to methods and for structures. With for structures, any variable declared in the initialization portion of the for header will be in scope only within that for structure. Good Programming Practice 6.4 Avoid local-variable names that hide instance-variable names. The program in Fig. 6.13 demonstrates scoping issues with instance variables and local variables. Instance variable x (line 15) is initialized to 1. This instance variable is hidden in any block (or method) that declares a local variable named x. The showOutputButton_ Click event handler (lines 47 63) declares a local variable x and initializes it to 5 (line 50). Lines 52 53 display the value of this local variable to show that instance variable x (with value 1) is hidden in method showOutputButton_Click. 1 // Fig. 6.13: Scoping.cs 2 // Demonstrating scope of local and instance variables. 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 Scoping : System.Windows.Forms.Form 11 { 12 private System.Windows.Forms.Label outputLabel; 13 private System.Windows.Forms.Button showOutputButton; 14 15 public int x = 1; // instance variable Fig. 6.13 Scoping. (Part 1 of 3.)
You need excellent and relaible webhost company to host your web applications? Then pay a visit to Inexpensive Web Hosting services.

Leave a Reply