Determining Scope


Constants, variables, and arrays are useful ways to store and retrieve data in Visual 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 10, 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, variable, array, or procedure can be "seen" in code. For a constant or variable, scope can be one of the following:

  • Block level

  • Method level (local)

  • Private

By the Way

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 (and constants, for that matter).


Understanding Block Scope

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:

Do {    <statements to execute in the loop } while(i <10)


By the Way

Visual 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.


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); }


By the Way

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


Understanding Method-Level (Local) Scope

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, almost 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, Visual C# .NET returns a compile error to the method making the reference (the variable or constant doesn't exist). It's generally considered best practice to declare all your local variables at the top of a method, but Visual C# .NET 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 11.3.

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


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. Visual 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 11.4).

Figure 11.4. The declarations section includes the Visual C# generated statements.


Did you Know?

You can hover the pointer over any variable in code and a ToolTip will appear showing you the declaration of the variable.


Did you Know?

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.





Sams Teach Yourself Microsoft Visual C# 2005 in 24 Hours, Complete Starter Kit
Sams Teach Yourself Visual C# 2005 in 24 Hours, Complete Starter Kit
ISBN: 0672327406
EAN: 2147483647
Year: N/A
Pages: 248
Authors: James Foxall

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