reversed foreach loop

Class library usage, coding and language questions.
User avatar
odklizec
Ranorex Guru
Ranorex Guru
Posts: 7470
Joined: Mon Aug 13, 2012 9:54 am
Location: Zilina, Slovakia

reversed foreach loop

Post by odklizec » Mon Apr 22, 2013 3:09 pm

Hi folks,

Any idea how can I do reversed foreach loop? I have a code like this...
foreach (Ranorex.TabPage tab in tabPageList)
		    {  
				tab.Click();
				Report.Screenshot(repo.LB.Self);
				Report.Log(ReportLevel.Info, "Mouse", "Mouse Left Click item 'LB.TabPage' at .462;.462.", repo.LB.TabPageInfo);
				tab.Click(new Location(TabPage_Screenshot1, "Center", TabPage_Screenshot1_Options));
		    }
Basically, the code enumerates available "tabs" in program, click each tab (to make its [x] button visible), take a screenshot and click the tab's [x] button. And here comes the problem.

If the foreach loop is done in a normal order (1, 2, ....n), the tabs are closed but not all of them, because the tabPageList Count is not decreased. So I soon have less tabs, than is the initial count of all tabs and the actual loop index does not match with actual tab index. This is why I need to start closing the tabs in reversed order (5, 4,...1).

I tried to use this code (found about "Reverse" via google):
foreach (Ranorex.TabPage tab in tabPageList.Reverse())
But it seems it does not work. Any idea how to make the "Reverse" working?

Sorry for a silly question, I'm a C# noob ;)
Last edited by odklizec on Fri May 17, 2013 8:27 am, edited 2 times in total.
Pavel Kudrys
Ranorex explorer at Descartes Systems

Please add these details to your questions:
  • Ranorex Snapshot. Learn how to create one >here<
  • Ranorex xPath of problematic element(s)
  • Ranorex version
  • OS version
  • HW configuration

User avatar
odklizec
Ranorex Guru
Ranorex Guru
Posts: 7470
Joined: Mon Aug 13, 2012 9:54 am
Location: Zilina, Slovakia

Re: reversed foreach loop

Post by odklizec » Tue Apr 23, 2013 7:47 am

OK, problem solved. Instead of foreach loop I used "for" loop...
for(int i=tabPageList.Count-1; i >= 0; i--)
                {    
                    Ranorex.TabPage tab = tabPageList;
                    tab.Click();  
                    Report.Screenshot(repo.LB.Self);  
                    Report.Log(ReportLevel.Info, "Mouse", "Mouse Left Click item 'LB.TabPage' at .462;.462.", repo.LB.TabPageInfo);  
                    tab.Click(new Location(TabPage_Screenshot1, "Center", TabPage_Screenshot1_Options));  
                }

Anyway, it would be interesting to know how to do the reversed foreach loop. Any idea how to do this?
Pavel Kudrys
Ranorex explorer at Descartes Systems

Please add these details to your questions:
  • Ranorex Snapshot. Learn how to create one >here<
  • Ranorex xPath of problematic element(s)
  • Ranorex version
  • OS version
  • HW configuration

User avatar
Support Team
Site Admin
Site Admin
Posts: 12145
Joined: Fri Jul 07, 2006 4:30 pm
Location: Houston, Texas, USA
Contact:

Re: reversed foreach loop

Post by Support Team » Tue Apr 23, 2013 11:32 am

Hi,

Additionally you could also use the "Reverse" as follows:
tabPageList.Reverse();
foreach (Ranorex.TabPage tab in tabPageList)  
            {    
                tab.Click();  
                Report.Screenshot(repo.LB.Self);  
                Report.Log(ReportLevel.Info, "Mouse", "Mouse Left Click item 'LB.TabPage' at .462;.462.",   repo.LB.TabPageInfo);  
                tab.Click(new Location(TabPage_Screenshot1, "Center", TabPage_Screenshot1_Options));  
            }
on the following page is also a small example where you can see how the Reverse works: Reverse.

Kind regards,
Markus

User avatar
odklizec
Ranorex Guru
Ranorex Guru
Posts: 7470
Joined: Mon Aug 13, 2012 9:54 am
Location: Zilina, Slovakia

Re: reversed foreach loop

Post by odklizec » Tue Apr 23, 2013 12:39 pm

Hi Markus,

Thanks for the reply and hint! Silly me, why I haven't thought of using tabPageList.Reverse() on a separate row? At least I now know two ways to achieve the same result ;)
Pavel Kudrys
Ranorex explorer at Descartes Systems

Please add these details to your questions:
  • Ranorex Snapshot. Learn how to create one >here<
  • Ranorex xPath of problematic element(s)
  • Ranorex version
  • OS version
  • HW configuration

User avatar
odklizec
Ranorex Guru
Ranorex Guru
Posts: 7470
Joined: Mon Aug 13, 2012 9:54 am
Location: Zilina, Slovakia

Re: reversed foreach loop

Post by odklizec » Wed Apr 24, 2013 9:02 am

Hi again Markus,

I'm afraid, your suggestion does not work in my code module. I used the "Reverse" code as per your suggestion and this is what I've got...
'System.Collections.Generic.IList<Ranorex.TabPage>' does not contain a definition for 'Reverse' and no extension method 'Reverse' accepting a first argument of type 'System.Collections.Generic.IList<Ranorex.TabPage>' could be found (are you missing a using directive or an assembly reference?) (CS1061) - C:\Documents and Settings\Tester\My Documents\Ranorex\RanorexStudio Projects\xSpector\xSpector\Recordings\HandleMultipleTabs.UserCode.cs:41,17
The thing is that the "Reverse" keyword is not displayed in the the code completion combo...
Reverse.png
It works OK in the MSDN example you pointed me to. Looking in MSDN I found that the IList does not support the Reverse method. So, the question is, how to use "List" instead of IList?

Here is the entire tab handling module:
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using System.Drawing;
using System.Threading;
using WinForms = System.Windows.Forms;

using Ranorex;
using Ranorex.Core;
using Ranorex.Core.Testing;

namespace xSpector.Recordings
{
    public partial class HandleMultipleTabs
    {
        private void Init()
        {
            // Your recording specific initialization code goes here.
        }
        
        public void Close_All_Tabs()
        {			
		IList<Ranorex.TabPage> tabPageList = repo.LiteBox3d.TabPage.Find<Ranorex.TabPage>("/form[@title~'^LiteBox3D' and @processname='LiteBox3D']/*/*/*/tabpagelist/tabpage");
		if (tabPageList.Count > 1)
			{
			tabPageList.Reverse();
				foreach (Ranorex.TabPage tab in tabPageList)
				{
					tab.Click();
					Report.Screenshot(repo.LiteBox3d.Self);
	            	Report.Log(ReportLevel.Info, "Mouse", "Mouse Left Click item 'LiteBox3d.TabPage' at .462;.462.", repo.LiteBox3d.TabPageInfo);
					tab.Click(new Location(TabPage_Screenshot1, "Center", TabPage_Screenshot1_Options));
			    }  
			}
         
        }
      
        #region Image Feature Data
        CompressedImage TabPage_Screenshot1
        { get { return repo.LiteBox3d.TabPageInfo.GetScreenshot1(new Rectangle(1, 0, 13, 13)); } }

        Imaging.FindOptions TabPage_Screenshot1_Options
        { get { return Imaging.FindOptions.Parse("0.95;EdgesSobel;0,0,0,0;True;10000000;0ms"); } }

		#endregion        
        
    }
}
You do not have the required permissions to view the files attached to this post.
Pavel Kudrys
Ranorex explorer at Descartes Systems

Please add these details to your questions:
  • Ranorex Snapshot. Learn how to create one >here<
  • Ranorex xPath of problematic element(s)
  • Ranorex version
  • OS version
  • HW configuration

User avatar
Support Team
Site Admin
Site Admin
Posts: 12145
Joined: Fri Jul 07, 2006 4:30 pm
Location: Houston, Texas, USA
Contact:

Re: reversed foreach loop

Post by Support Team » Wed Apr 24, 2013 9:54 am

Hi,

This is because the "IList" interface doesn't provide such a method.
The Reverse() is a method of the "List" class, therefore it is necessary to convert the IList to a "normal" List in order to access the Reverse method.
For instance:
IList<Ranorex.TabPage> tabPageList = repo.LiteBox3d.TabPage.Find<Ranorex.TabPage>("/form[@title~'^LiteBox3D' and @processname='LiteBox3D']/*/*/*/tabpagelist/tabpage");  
List<Ranorex.TabPage> list = new List<Ranorex.TabPage>(tabPageList);
        if (list.Count > 1)  
            {  
            list.Reverse();
           ...
Sorry, I should have mentioned it in my last post.

Regards,
Markus

User avatar
odklizec
Ranorex Guru
Ranorex Guru
Posts: 7470
Joined: Mon Aug 13, 2012 9:54 am
Location: Zilina, Slovakia

Re: reversed foreach loop

Post by odklizec » Wed Apr 24, 2013 9:59 am

Hi Markus,

Thanks for the explanation. No need to apologize! I think I will stick with simple "For" loop as it works as expected and without the need of additional IList conversion ;)
Pavel Kudrys
Ranorex explorer at Descartes Systems

Please add these details to your questions:
  • Ranorex Snapshot. Learn how to create one >here<
  • Ranorex xPath of problematic element(s)
  • Ranorex version
  • OS version
  • HW configuration