A tricky Local Global member variable example

R

raylopez99

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
}

}

}
 
F

Family Tree Mike

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.
 
R

raylopez99

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
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 said:
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
}

}

}
 
K

kyle.szklenski

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

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
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 said:
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
          }
        }
}
 
R

raylopez99

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top