Bug 26600 - Foreach loops with same variable name
Summary: Foreach loops with same variable name
Status: RESOLVED FIXED
Alias: None
Product: Runtime
Classification: Mono
Component: Debugger ()
Version: unspecified
Hardware: PC Windows
: Normal normal
Target Milestone: ---
Assignee: Zoltan Varga
URL:
Depends on:
Blocks:
 
Reported: 2015-01-30 11:09 UTC by onevitalboy
Modified: 2015-04-29 05:34 UTC (History)
6 users (show)

Tags:
Is this bug a regression?: ---
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report on GitHub or Developer Community with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:
Status:
RESOLVED FIXED

Description onevitalboy 2015-01-30 11:09:26 UTC
Description of Problem:
If we have 2 foreach cycles with same variable name, then variable in the second cycle will always show the last value from the first cycle.

Steps to reproduce the problem:
  var a = new[] { 1, 2, 3 };
  foreach (var i in a)
  {
    //
  }

  var b = new[] { "a", "b", "c" };
  foreach (var i in b)
  {
    // set breakpoint here, i == 3
  }
Comment 1 Rajneesh Kumar 2015-02-02 05:17:25 UTC
I have checked this issue but not able to reproduce this. To reproduce this issue I have used the code provided in bug description.

Steps I followed:

1. Create a console project in XS.
2. Add following code in Main method of Program.cs.
                        var a = new[] { 1, 2, 3 };
			foreach (var i in a)
			{
				Console.WriteLine (i);
			}
			var b = new[] { "a", "b", "c" };
			int count = 0;
			foreach (var i in b)
			{
                                // set breakpoint here
				Console.WriteLine (i);
			}
3. Set the breakpoint in second cycle.
4. Run the project. breakpoint will hit.
5. Observed that value of 'i' in debugger that is "a".

I observed that variable in the second foreach cycle shows the value from second cycle itself. Here is the screencast for the same:
Screencast: http://www.screencast.com/t/6Xjd1G15afoP

Could you please provide us your environment info, screencast, steps to reproduce ? That would be helpful to reproduce this issue at our end. Also please attach your Ide logs.

You can get Ide logs via Help=>Open Log directory=>Ide_.log (Choose Latest)
Enviornment Info via Help=>About=>Show Detyails=>Copy Information.

Ide Logs:https://gist.github.com/Rajneesh360Logica/51e373a0346b68ae6888

Environment Info:

=== Xamarin Studio ===

Version 5.7.1 (build 16)
Installation UUID: a7e29e93-6348-4126-9ebc-b2777c96a552
Runtime:
	Microsoft .NET 4.0.30319.18408
	GTK+ 2.24.22 (MS-Windows theme)
	GTK# 2.12.26

=== Xamarin.Android ===

Version: 4.20.0 (Business Edition)
Android SDK: E:\android-sdk
	Supported Android versions:
		2.1    (API level 7)
		2.2    (API level 8)
		2.3    (API level 10)
		3.1    (API level 12)
		4.0    (API level 14)
		4.0.3  (API level 15)
		4.2    (API level 17)
		4.3    (API level 18)
		4.4    (API level 19)
		4.4.87 (API level 20)
		5.0    (API level 21)
Java SDK: C:\Program Files (x86)\Java\jdk1.6.0_39
java version "1.6.0_39"
Java(TM) SE Runtime Environment (build 1.6.0_39-b04)
Java HotSpot(TM) Client VM (build 20.14-b01, mixed mode)

=== Build Information ===

Release ID: 507010016
Git revision: f12fcaf4707ab436bee2df6263eb5333197b262c
Build date: 2015-02-01 19:17:02-05
Xamarin addins: f7b7d34419c9ec24501bfa7c658e80a6305613e0

=== Operating System ===

Windows 6.2.9200.0 (64-bit)
Comment 2 onevitalboy 2015-02-02 07:38:09 UTC
I forgot to specify that I face this issue in Android app, sorry for that.

Just change the first point to this one:
1. Create an empty Android application project in XS or VS.

I hope it will help you to reproduce the issue, otherwise I will provide you with environment info etc.
Thanks!
Comment 3 Sadik Ali 2015-02-03 06:18:17 UTC
@  Onevitalboy, Thanks a lot for clarification of issue. 
I have tried this issue and now I am able to reproduce this. To reproduce this issue I have used the code provided in bug description.

Steps I followed:

1. Create an android application in XS.
2. Add a class named 'TestClass' and add following code:
            var a = new[] { 1, 2, 3 };
            foreach (var i in a)
            {
                Console.WriteLine (i);
            }
            var b = new[] { "a", "b", "c" };
            int count = 0;
            foreach (var i in b)
            {
                // set breakpoint here
                Console.WriteLine (i);
            }
3. Set the breakpoint in second cycle.
4. Create object of this class at click event of button in MainActivity.cs
4. Run the project click on button, breakpoint will hit.
5. Observed that value of 'i' in debugger that is always "3".

I observed that variable in the second foreach cycle shows the last value from first cycle that is '3'. Please refer the screencast.
http://www.screencast.com/t/cG58cSiv

Additional Information: I observed that in debugger, variable in the second foreach cycle shows the last value from first cycle, but I am getting correct output in application output. 

Ide Logs: https://gist.github.com/Sadik1/14f32cd65b63aa971c2a
Application Output:https://gist.github.com/Sadik1/c7939e0235dc9ee3b0f8

Environment Info:

=== Xamarin Studio ===

Version 5.7.1 (build 16)
Installation UUID: a7e29e93-6348-4126-9ebc-b2777c96a552
Runtime:
	Microsoft .NET 4.0.30319.18408
	GTK+ 2.24.22 (MS-Windows theme)
	GTK# 2.12.26

=== Xamarin.Android ===

Version: 4.20.0 (Business Edition)
Android SDK: E:\android-sdk
	Supported Android versions:
		2.1    (API level 7)
		2.2    (API level 8)
		2.3    (API level 10)
		3.1    (API level 12)
		4.0    (API level 14)
		4.0.3  (API level 15)
		4.2    (API level 17)
		4.3    (API level 18)
		4.4    (API level 19)
		4.4.87 (API level 20)
		5.0    (API level 21)
Java SDK: C:\Program Files (x86)\Java\jdk1.6.0_39
java version "1.6.0_39"
Java(TM) SE Runtime Environment (build 1.6.0_39-b04)
Java HotSpot(TM) Client VM (build 20.14-b01, mixed mode)

=== Build Information ===

Release ID: 507010016
Git revision: f12fcaf4707ab436bee2df6263eb5333197b262c
Build date: 2015-02-01 19:17:02-05
Xamarin addins: f7b7d34419c9ec24501bfa7c658e80a6305613e0

=== Operating System ===

Windows 6.2.9200.0 (64-bit)
Comment 4 Jeffrey Stedfast 2015-02-09 11:30:18 UTC
I think this is runtime or compiler. Probably some sort of scoping issue?
Comment 5 David Karlaš 2015-04-29 05:34:52 UTC
This should be fixed in Xamarin.Android 5.2(pdb2mdb was merged with Mono runtime, which has this scoping problems fixed).

Sadik, can you verify this when you get XA 5.2? Tnx.