473,398 Members | 2,343 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

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 2608
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Oliver Corona | last post by:
I am wondering if anyone has any insights on the performance benefit (or detriment) of declaring local variables instead of referencing members. Is allocating memory for a new variable more...
12
by: Olumide | last post by:
I'm studying Nigel Chapman's Late Night Guide to C++ which I think is an absolutely fantastic book; however on page 175 (topic: operator overlaoding), there the following code snippet: inline...
5
by: john | last post by:
By doing: void MyClass::MyFunction() { static int myvar; .... } We can defined a function local variable 'myvar' that keeps its value from call to call (the point is that the variable can...
23
by: Timothy Madden | last post by:
Hello all. I program C++ since a lot of time now and I still don't know this simple thing: what's the problem with local functions so they are not part of C++ ? There surely are many people...
9
by: Pyenos | last post by:
#############################CODE############################## t_len=0 class WORK: def getwork(self): def formattable(table_to_process,type): TYPE= #list of types to format if type==TYPE: def...
55
by: Zytan | last post by:
I see that static is more restricted in C# than in C++. It appears usable only on classes and methods, and data members, but cannot be created within a method itself. Surely this is possible in...
2
by: Matt Nordhoff | last post by:
Sebastjan Trepca wrote: Python doesn't like when you read a variable that exists in an outer scope, then try to assign to it in this scope. (When you do "a = b", "b" is processed first. In...
8
by: Rahul | last post by:
Hi, I have the following code and i get a compilation error, int main() { class Locale { public: static int c;
112
by: istillshine | last post by:
When I control if I print messages, I usually use a global variable "int silent". When I set "-silent" flag in my command line parameters, I set silent = 1 in my main.c. I have many functions...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.