469,917 Members | 1,458 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,917 developers. It's quick & easy.

A tricky Local Global member variable example

Why is the same variable local inside a 'foreach' loop yet 'global' in
scope (or to the class) outside it?

RL

class MyClass
{

int[] MyMemberArray1; //member variables, arrays, that are "global" to
the class
char[] MyMemberArray2;
string[] AnotherArray;

public void MyFunction()
{

MyMemberArray1 = new int[10]; //'global' here; compare
with below
MyMemberArray2 = new char[10];

foreach (string s in AnotherArray)
{
// MyMemberArray1 = new int[10]; //if placed here, only
local to 'foreach' despite being a member of class MyClass!
// MyMemberArray2 = new char[10]; //Ditto
}

}

}
Nov 16 '08 #1
4 2427
Why do you have the impression the variable is local inside the foreach? If
you right click the variable, then it should take you to the single
declaration.

"raylopez99" <ra********@yahoo.comwrote in message
news:49**********************************@x16g2000 prn.googlegroups.com...
Why is the same variable local inside a 'foreach' loop yet 'global' in
scope (or to the class) outside it?

RL

class MyClass
{

int[] MyMemberArray1; //member variables, arrays, that are "global" to
the class
char[] MyMemberArray2;
string[] AnotherArray;

public void MyFunction()
{

MyMemberArray1 = new int[10]; //'global' here; compare
with below
MyMemberArray2 = new char[10];

foreach (string s in AnotherArray)
{
// MyMemberArray1 = new int[10]; //if placed here, only
local to 'foreach' despite being a member of class MyClass!
// MyMemberArray2 = new char[10]; //Ditto
}

}

}
Nov 16 '08 #2
Well I was getting no answer (empty set) when the commented code was
included, but did get an answer (MyMember filled with data) when I
moved the variables up one scope. Problem solved. One of those
things, no matter how you classify it.

RL

Family Tree Mike wrote:
Why do you have the impression the variable is local inside the foreach? If
you right click the variable, then it should take you to the single
declaration.

"raylopez99" <ra********@yahoo.comwrote in message
news:49**********************************@x16g2000 prn.googlegroups.com...
Why is the same variable local inside a 'foreach' loop yet 'global' in
scope (or to the class) outside it?

RL

class MyClass
{

int[] MyMemberArray1; //member variables, arrays, that are "global" to
the class
char[] MyMemberArray2;
string[] AnotherArray;

public void MyFunction()
{

MyMemberArray1 = new int[10]; //'global' here; compare
with below
MyMemberArray2 = new char[10];

foreach (string s in AnotherArray)
{
// MyMemberArray1 = new int[10]; //if placed here, only
local to 'foreach' despite being a member of class MyClass!
// MyMemberArray2 = new char[10]; //Ditto
}

}

}
Nov 17 '08 #3
Your question is quite confusing. What were you doing with the arrays
inside the foreach? If you notice, you're calling new on them each
time inside the loop, which makes them just that, new. In other words,
every loop around, it'll clear out that variable and replace it with a
new object or pointer.

If that's not the answer, we would need more code to go by to see what
you're doing wrong.
Pax,
K

http://home.infusionblogs.com/kszklenski/default.aspx

On Nov 17, 7:07*am, raylopez99 <raylope...@yahoo.comwrote:
Well I was getting no answer (empty set) when the commented code was
included, but did get an answer (MyMember filled with data) when I
moved the variables up one scope. *Problem solved. *One of those
things, no matter how you classify it.

RL

Family Tree Mike wrote:
Why do you have the impression the variable is local inside the foreach? *If
you right click the variable, then it should take you to the single
declaration.
"raylopez99" <raylope...@yahoo.comwrote in message
news:49**********************************@x16g2000 prn.googlegroups.com....
Why is the same variable local inside a 'foreach' loop yet 'global' in
scope (or to the class) outside it?
RL
class MyClass
* *{
int[] MyMemberArray1; //member variables, arrays, that are "global" to
the class
char[] MyMemberArray2;
string[] AnotherArray;
public void MyFunction()
* * * *{
* * * * * *MyMemberArray1 = new int[10]; //'global' here; compare
with below
* * * * * *MyMemberArray2 = new char[10];
* * * * * *foreach (string s in AnotherArray)
* * * * * *{
* * * * * * // MyMemberArray1 = new int[10]; //if placed here, only
local to 'foreach' despite being a member of class MyClass!
* * * * * *// MyMemberArray2 = new char[10]; //Ditto
* * * * * }
* * * * }
}
Nov 17 '08 #4
ky************@gmail.com wrote:
Your question is quite confusing. What were you doing with the arrays
inside the foreach? If you notice, you're calling new on them each
time inside the loop, which makes them just that, new. In other words,
every loop around, it'll clear out that variable and replace it with a
new object or pointer.
No, I think you solved it Kyle. That's probably exactly what I was
doing wrong--indeed 'new' makes it 'new' inside the loop.

Rookie mistake, like I am.

RL
Nov 17 '08 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Oliver Corona | last post: by
12 posts views Thread by Olumide | last post: by
23 posts views Thread by Timothy Madden | last post: by
9 posts views Thread by Pyenos | last post: by
2 posts views Thread by Matt Nordhoff | last post: by
8 posts views Thread by Rahul | last post: by
112 posts views Thread by istillshine | last post: by
1 post views Thread by Waqarahmed | last post: by
reply views Thread by Salome Sato | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.