Foreach Loop invalidates after screen change

Technology specific object identification, supported applications, web technologies, and 3rd party controls.
eldorado25
Posts: 21
Joined: Tue Aug 06, 2019 4:14 pm

Foreach Loop invalidates after screen change

Post by eldorado25 » Wed Mar 11, 2020 5:30 pm

I know this is by design,

but is there any work around wherein which I can have it not invalidate itself?

I thought it would be the case of this thread: https://www.ranorex.com/forum/foreach-l ... t6285.html
But I cannot seem to get the Find function working

Any help?

Thanks

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

Re: Foreach Loop invalidates after screen change

Post by odklizec » Thu Mar 12, 2020 8:38 am

Hi,

Could you please post your code, so we can eventually suggest a solution? Usually, it should be enough to simply reset the list, with new instance of looped object. But without seeing exact code, it's impossible to tell what you should do.
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

eldorado25
Posts: 21
Joined: Tue Aug 06, 2019 4:14 pm

Re: Foreach Loop invalidates after screen change

Post by eldorado25 » Thu Mar 12, 2020 9:49 am

Code: Select all

IList<Ranorex.TdTag> AuthCells = repo.LinkedScreens.TableCellInfo.Find<Ranorex.TdTag>("");
			int counter = AuthCells.Count;
			Report.Info("Cells in list " + counter.ToString());
			if(counter>0)
			{
				for(int i = 1; i <=counter; i++)
				{
					AuthCells = repo.LinkedScreens.TableCell.Find<Ranorex.TdTag>();
					foreach(Ranorex.TdTag Cell in AuthCells)
				{
				Validate.Exists(Cell);
				if(Cell.InnerText != null)
				{
					Report.Info(Cell.ToString());
					//Do Stuff (it's basically if contains this, do this, else if contains that, do that)
				}
			}
		}
		
What I have is a dynamic table that contains a maximum of 3 records based on certain circumstances. Each one of these rows has a view button in a cell next to a Summary cell for that row. When the user clicks on each view button it will take them to a document which needs to be processed by that user, but each one of these has a specific workflow.

What I want to do is access these rows if they exist and carry out the workflow based on the text inside the summary cell, I know a more simple solution exists but this would demand hardcoding some stuff in, which I am not fond of.

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

Re: Foreach Loop invalidates after screen change

Post by odklizec » Thu Mar 12, 2020 10:02 am

Hi,

So if I understand it right, after you "do stuff", the AuthCells is no longer valid, right? In this case, it should be enough to simply refill the AuthCells with new (refreshed) content?...

Code: Select all

IList<Ranorex.TdTag> AuthCells = repo.LinkedScreens.TableCellInfo.Find<Ranorex.TdTag>("");
int counter = AuthCells.Count;
Report.Info("Cells in list " + counter.ToString());
if(counter>0)
{
    for(int i = 1; i <=counter; i++)
    {
        AuthCells = repo.LinkedScreens.TableCell.Find<Ranorex.TdTag>();
        foreach(Ranorex.TdTag Cell in AuthCells)
        {
            Validate.Exists(Cell);
            if(Cell.InnerText != null)
            {
                Report.Info(Cell.ToString());
                //Do Stuff (it's basically if contains this, do this, else if contains that, do that)
                AuthCells = repo.LinkedScreens.TableCell.Find<Ranorex.TdTag>();
            }
        }
    }
}
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

eldorado25
Posts: 21
Joined: Tue Aug 06, 2019 4:14 pm

Re: Foreach Loop invalidates after screen change

Post by eldorado25 » Thu Mar 12, 2020 10:04 am

My god I'm an idiot, thank you so much this should have been obvious to me :lol:

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

Re: Foreach Loop invalidates after screen change

Post by odklizec » Thu Mar 12, 2020 10:11 am

Hi, no you are not. I found it "the hard way" too in the past :D
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