variable scope!

  • Thread starter Thread starter Jeff
  • Start date Start date
J

Jeff

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++;
}
}
 
Hi Jeff,

Method parameters (Multiply(int i)) have preference over members
(private static int i = 1)

In this case, the "i" parameter hides the "i" member, and since an int
is a value type, the contents are copied.

In the increment() and multiply() the results are lost.

you remove the parameters from the method:
 
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,
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,
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.
 
Expanding on the above.
If you want to see how to pass integers as reference types, then see this
post:
http://msdn.microsoft.com/newsgroup...814f62-3918-4d58-8653-9f2265c590af&sloc=en-us

--
Good luck!

Shailen Sukul
Architect
(BSc MCTS, MCSD.Net MCSD MCAD)
Ashlen Consulting Service P/L
(http://www.ashlen.net.au)


Wiebe Tijsma said:
Hi Jeff,

Method parameters (Multiply(int i)) have preference over members
(private static int i = 1)

In this case, the "i" parameter hides the "i" member, and since an int
is a value type, the contents are copied.

In the increment() and multiply() the results are lost.

you remove the parameters from the method:
private static void Multiply()
{
i *= 2;
}

public static void Increment()
{
i++;
}


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++;
}
}
 
Back
Top