P. 1
Rob Miles CSharp Yellow Book 2011

Rob Miles CSharp Yellow Book 2011

|Views: 40|Likes:
Published by tejvan

More info:

Published by: tejvan on Mar 06, 2012
Copyright:Attribution Non-commercial

Availability:

Read on Scribd mobile: iPhone, iPad and Android.
download as PDF, TXT or read online from Scribd
See more
See less

05/27/2012

pdf

text

original

We have seen that when we want to store a quantity in our program we can create a
variable to hold this information. The C# compiler makes sure that the correctly sized
chunk of memory is used to hold the value and it also makes sure that we only ever use
that value correctly. The C# compiler also looks after the part of a program within
which a variable has an existence. This is called the scope of a variable.

3.2.1 Scope and blocks

We have already seen that a block is a number of statements which are enclosed in
curly brackets. Any block can contain any number of local variables, i.e. variables
which are local to that block.

The scope of a local variable is the block within which the variable is declared. As far
as the C# language is concerned you can declare a variable at any point in the block,
but you must declare it before you use it. When the execution of the program moves
outside a block any local variables which are declared in the block are automatically
discarded. The methods that we have created have often contained local variables; the
variable result in the readInt method is local to the method block.

3.2.2 Nested Blocks

We have seen that in C# the programmer can create blocks inside blocks. Each of these
nested blocks can have its own set of local variables:

{

int i ;
{

int j ;

}

}

The variable j has the scope of the inner block. This means that only statements in the
inner block can use this variable. In other words the code:

Creating Programs

Variables and Scope

C# Programming © Rob Miles 2011

59

{

int i ;
{

int j ;

}
j = 99 ;

}

- would cause an error, as the variable j does not exist at this point in the program.

In order to keep you from confusing yourself by creating two versions of a variable
with the same name, C# has an additional rule about the variables in the inner blocks:

{

int i ;
{

int i ;

}

}

This is not a valid program because C# does not let a variable in an inner block have
the same name as one in an outer block. This is because inside the inner block there is
the possibility that you may use the "inner" version of i when you intend to use the
outer one. In order to remove this possibility the compiler refuses to allow this. Note
that this is in contrast to the situation in other languages, for example C++, where this
behaviour is allowed.

It is however perfectly acceptable to reuse a variable name in successive blocks
because in this situation there is no way that one variable can be confused with another.

{

int i ;

}
{

int i ;
{

int j ;

}

}

The first incarnation of i has been destroyed before the second, so this code is OK.

For loop local variables

A special kind of variable can be used when you create a for loop construction. This
allows you to declare a control variable which exists for the duration of the loop itself:

for ( int i = 0 ; i < 10 ; i = i + 1 )
{

Console.WriteLine ( "Hello" ) ;

}

The variable i is declared and initialized at the start of the for loop and only exists for
the duration of the block itself.

3.2.3 Data Member in classes

Local variables are all very well, but they disappear when the program execution leaves
the block where they are declared. We often need to have variables that exist outside
the methods in a class.

Variables Local to a Method

Consider the following code:

Creating Programs

Variables and Scope

C# Programming © Rob Miles 2011

60

class LocalExample
{

static void OtherMethod ()
{

local = 99; // this will not compile

}

static void Main ()
{

int local = 0;
Console.WriteLine ("local is :" + local);

}

}

The variable local is declared and used within the Main method, and can’t be used
anywhere else. If a statement in OtherMethod tries to use local the program will fail
to compile.

Variables which are Data Members of a Class

If I want to allow two methods in a class to share a variable I will have to make the
variable a member of the class. This means declaring it outside the methods in the
class:

class MemberExample
{

// the variable member is part of the class
static int member = 0 ;

static void OtherMethod ()
{

member = 99;

}

static void Main ()
{

Console.WriteLine ("member is : " + member);
OtherMethod();
Console.WriteLine ("member is now : " + member);

}

}

The variable member is now part of the class MemberExample, and so the Main
method and OtherMethod can both use this variable. The program above would print
out:

member is : 0
member is now : 99

This is because the call of OtherMethod would change the value of member to 99
when it runs.

Class variables are very useful if you want to have a number of methods ―sharing‖ a set

of data. For example, if you were creating a program to play chess it would be sensible
to make the variable that stores the board into a member of the class. Then the methods
that read the player move, display the board, and calculate the computer move could all
use the same board information.

Static class members

Note that I have made the data member of the class static, so that it is part of the class
and not an instance of the class. This is not something to worry about just now; we will
investigate the precise meaning of static later on. For now you just have to remember
to put in the static keyword, otherwise your program will not compile.

Creating Programs

Arrays

C# Programming © Rob Miles 2011

61

One common programming mistake is to confuse static with const. Marking a
variable as const means ―the value cannot be changed‖. Marking a variable with
static means ―the variable is part of the class and is always present‖. If it helps you

can think of static as something which is always with us, like the background static
noise on your radio when you tune it off station. Alternatively you can think of it as
stationary, and therefore not going anywhere.

Programmer’s Point: Plan your variable use

You should plan your use of variables in your programs. You should decide which variables are only required for use
in local blocks and which should be members of the class. Bear in mind that if you make a variable a member of the
class it can be used by any method in that class (which increases the chances of something bad happening to it). I am
very careful to keep the number of member variables in a class to the minimum possible and use local variables if I
only need to store the value for a small part of the code.

You're Reading a Free Preview

Download
scribd
/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->