Sams Teach Yourself C# in 24 Hours

Sams Teach Yourself C# in 24 Hours

By James Foxall and Wendy Haro-Chun

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:

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

Understanding 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)

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

Understanding Method-Level (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.

12fig02.jpg

Figure 12.2 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. 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).

12fig03.jpg

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

Share ThisShare This

Informit Network