Foreach loop

Best practices, code snippets for common functionality, examples, and guidelines.
yaro2o
Posts: 85
Joined: Mon Jan 29, 2018 11:19 am

Foreach loop

Post by yaro2o » Mon Mar 04, 2019 3:26 pm

hi I have several elements for which I will use the same code there is a chance to put this in the loop for/foreach ?

Ranorex.Validate.Exists(element);
Attachments
VirtualBoxVM_2019-03-04_15-22-18.png
VirtualBoxVM_2019-03-04_15-22-18.png (224.05 KiB) Viewed 270 times

User avatar
N612
Posts: 102
Joined: Mon Jul 11, 2016 4:01 pm

Re: Foreach loop

Post by N612 » Mon Mar 04, 2019 10:04 pm

In order to loop over objects, you need to have it stored in an iterable object, such as a list. The below example should help you achieve this.

Code: Select all

List<Element> myElements = new List<Element>();
myElements.Add(repo.FormRun.ButtonOK);
myElements.Add(repo.FormRun.Cancel);
myElements.Add(repo.FormRun.ButtonBrowse);

foreach (var element in myElements) 
	Validate.AttributeEqual(element, "Class", "Button");
Hope this helps!