Jeff,
In the function that you have written i.e.
private static void Multiply(int i) and also in the class, you have
private static int i = 1;
In the scope of function Multiply, you see two variable i declaration.
One is a static member while other is a local variable.
As far as i know, local variable has more preference so, the function
was multiplying the local variable than the member variable.
if you want to access the member variable, then you have to resolve the
scope for the compiler and tell it explicitly that you want to access
the member static member variable.
you do that by saying Program.i = i * 5;
-Prakash.
Jeff wrote:
Quote:
Hey
>
Below is a C# program I've made.... it's an app where I experiment with
variable scope. In this code you see two variables named i (one is a local
variable and the other is a member of the class). This code print out 5 (
Console.WriteLine(i); )... the member variable isn't passed on to the
Multiply, Increment variables etc.. I wonder what the rules are here... I've
been googling and and can't find a good article on variable scope in C#
which explain if a local variable and member variable has the same name like
in this code, so maybe you have a link?
>
class Program
{
private static int i = 1;
>
public static void Main(string[] args)
{
int i = 5;
Multiply(i);
Increment(i);
Reset();
Console.WriteLine(i);
}
>
private static void Reset()
{
i = 0;
}
>
private static void Multiply(int i)
{
i *= 2;
}
>
public static void Increment(int i)
{
i++;
}
}
>
>