471,108 Members | 1,687 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,108 software developers and data experts.

variable scope!

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++;
}
}
Nov 26 '06 #1
5 4617
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++;
}
}

Nov 26 '06 #2
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:
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++;
}
}

Nov 26 '06 #3
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:
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++;
}
}

Nov 26 '06 #4
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:
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++;
}
}

Nov 26 '06 #5
Expanding on the above.
If you want to see how to pass integers as reference types, then see this
post:
http://msdn.microsoft.com/newsgroups...0af&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" wrote:
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++;
}
}
Nov 26 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Grant Wagner | last post: by
4 posts views Thread by Gery D. Dorazio | last post: by
23 posts views Thread by Russ Chinoy | last post: by
pbmods
1 post views Thread by pbmods | last post: by
2 posts views Thread by Shraddha | last post: by
112 posts views Thread by istillshine | last post: by
3 posts views Thread by SRoubtsov | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.