Determining Scope

   

Constants, variables , and arrays are extremely useful ways to store and retrieve data in C# code. Hardly a program is written that doesn't use at least one of these elements. To properly use them, however, it's critical that you understand scope.

You had your first encounter with scope in Hour 11, "Creating and Calling Methods ," with the keywords private and public. You learned that code is written in procedures and that procedures are stored in modules. Scope refers to the level that a constant, a variable, an array, or a procedure can be "seen" in code. For a constant or variable, scope can be one of the following:

  • Block level

  • Method level (local)

  • Private level

graphics/bookpencil.gif

Scope has the same effect on array variables as it does on ordinary variables. For the sake of clarity, I'll reference variables in this discussion on scope, but understand that what I discuss applies equally to arrays.

The different levels of scope are explained in the following sections.

Understanding Block Scope

graphics/newterm.gif

Block scope, also called structure scope, is when a variable is declared within a structure, and if so, it gives the variable block scope.

Structures are coding constructs that consist of two statements as opposed to one. For example, the standard do structure is used to create a loop; it looks like this:

graphics/bookpencil.gif

C# uses the word structure to mean a user -defined type. However, when talking about code, structure is also used to mean a block of code that has a beginning and an end. For the purpose of this discussion, it is this code block that I am referring to.

 do    <statements to execute in the loop while(i <10) 

Another example is the for loop, which looks like this:

 for (int I = 1; i<10;i++) {    <statements to execute when expression is True> } 

If a variable is declared within a structure, the variable's scope is confined to the structure; the variable isn't created until the declaration statement occurs, and it's destroyed when the structure completes. If a variable is needed only within a structure, think about declaring it within the structure to give it block scope. Consider the following example:

 if (blnCreateLoop) {    int intCounter ; for (intCounter=1; intCounter<=100; intCounter++)    // Do something } 

By placing the variable declaration statement within the if structure, you ensure that the variable is created only if it is needed. In fact, you can create a block simply by enclosing statements in opening and closing braces like this:

 {    int intMyVariable = 10;    Console.WriteLine(intMyVariable); } 
graphics/bookpencil.gif

The various structures, including looping and decision-making structures, are discussed in later hours.

Understanding Method-Level (Local) Scope

graphics/newterm.gif

When you declare a constant or variable within a method, that constant or variable has method-level, or local, scope. Most of the variables you'll create will have method scope. In fact, all the variables you've created in previous hours have had method-level scope. You can reference a local constant or variable within the same method, but it isn't visible to other methods. If you try to reference a local constant or variable from a method other than the one in which it's defined, C# returns a compile error to the method making the reference (the variable or constant doesn't exist). It's generally considered the best practice to declare all your local variables at the top of a method, but C# doesn't care where you place declaration statements within a method. Note, however, that if you place a declaration statement within a structure, the corresponding variable will have block scope, not local scope.

Understanding Private-Level Scope

When a constant or variable has private-level scope, it can be viewed by all methods within the class containing the declaration. To methods in all other classes, however, the constant or variable doesn't exist. To create a constant or variable with private-level scope, you must place the declaration within a class but not within a method. Class member declarations are generally done at the beginning of the class (right after the opening brace of the class). Use private-level scope when many methods must share the same variable and when passing the value as a parameter is not a workable solution.

For all modules other than those used to generate forms, it's easy to add code to the declarations section; simply add the declaration statements just after the class declaration line and prior to any method definitions, as shown in Figure 12.2.

Figure 12.2. The declarations section exists above all declared methods.

graphics/12fig02.jpg


Classes used to generate forms have lots of system-generated code within them, so it might not be so obvious where to place private-level variables. C# inserts many private statements in classes used to build forms, so place your variable declarations after any and all form-type declaration statements in such classes (see Figure 12.3).

Figure 12.3. The declarations section includes the C# generated statements.

graphics/12fig03.jpg


graphics/bulb.gif

In general, the smaller the scope the better. When possible, give a variable block or local scope. If you have to increase scope, attempt to make the variable a private-level variable. You should use public variables only when absolutely necessary (and there are times when it is necessary to do so). The higher the scope, the more possibilities exist for problems and the more difficult it is to debug those problems.


   
Top


Sams Teach Yourself C# in 24 Hours
Sams Teach Yourself Visual Basic 2010 in 24 Hours Complete Starter Kit (Sams Teach Yourself -- Hours)
ISBN: 0672331136
EAN: 2147483647
Year: 2002
Pages: 253
Authors: James Foxall

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net