How do I loop through a List(Of String)

Hi,

I’m using UiPath Studio 2022.12.0 and can’t figure out the For Each. I think it changed?

I have a System.Collections.Generic.List(Of String) and I’ve added 4 strings to the list. Now I want to loop through the list using a For Each to get process each string value. Once I’m in the For Each, how to I access the specific list item?

Hi @carlor

you can change the TypeArgument property to String and the just call the Item or CurrenItem variable in your Write line

if you want your for each as object, in your write your pass

Item.ToString()

Or

CurrentItem.ToString()

Item o currenItem will depends on your for each

Regards!

Ah yes. There in lies the problem. In your For Each activity, you can specify the type argument, etc. Take a closer look at mine:

The above is just a test file I’m using for debugging. In my actual process, I’m getting the following error message when it hits the For Each:

For Each: Unable to cast object of type ‘System.String’ to type ‘System.Collections.Generic.List`1[System.String]’.

I’m so confused…

Hi @carlor

As per the latest UiPath update, for the For Each activity, you don’t have to specify the TypeArgument manually. The system will automatically detect the type of the data that you are iterating through & will continue as per.

Hope this helps,
Best Regards.

1 Like

That’s all well and good, but how to I access the list item within the loop?

Thanks,

Carlo.

Are you in the latest system.activities package version?

I have to use UiPath.System.Activities V 22.4.1 (long story…)

Hi @carlor ,

Is it possible for you to share the Screenshot of the dependencies and their versions used ?

Here is my sandbox version where I’m trying to just access the list item:

I’m using the same versions in my actual process:

image

@carlor ,

Could you just Press the keys Ctrl+Space inside the Write Line activity and check if you can find a variable with name CurrentItem ?

It’s not there.

Carlo.

@carlor ,

Maybe also check if a variable named item is present.
image

Maybe more detailed from the activity :

1 Like

Give a try using the following code in your write line activity

Item.ToString()

Regards

1 Like

@carlor

As same as the default type argument, this version stores the iterational data in a default variable called item

So inside the loop, you access the current loop item using:

item.ToString

Hope this helps,
Best Regards.

2 Likes

Ok. So, in my sandbox process, “item” does the trick. I’m now able to iterate through the items in the list. Now I know that it’s possible :slight_smile: Thank you!

Now it just leaves me with trying to figure out why it won’t work in my “real” process. In a library we created, we pass in the List(Of String) and have a For Each loop. Now, this is where it gets confusing for me. Within the For Each, I don’t have an “item” variable. The developer that wrote the library is using “inputfile.ToString”. I don’t know how/where he got that as it isn’t declared anywhere and it’s only appears once. (Unfortunately I can’t ask him has he is out of the country right now.)

At this point, the inputfile variable doesn’t matter because when I try to run I get an error as soon as it hits the For Each:

Unable to cast object of type ‘System.String’ to type ‘System.Collections.Generic.List`1[System.String]’.

In the library that processes the list, it is an argument defined as:

These are the dependencies in the library:
image

The library that creates the list uses the following dependencies:
image

and the main process that uses the two libraries has the following dependencies:
image

The List variable passed around is defined the same way everywhere.

Any thoughts?

Thanks,

Carlo.

@carlor ,

Most probably the System Activities version that the other person has used to develop the workflow was not in the same version as that you have now (Latest Stable). In the previous versions, we could rename the iterative variable from the For Each activity, and this maybe the reason that the item is now inputfile.

For this error, Is it possible to show the implementation of For Each ?

1 Like

Winner! Winner! Chicken Dinner!

Thank you Superman! Mentioning the different versions hit on something. We’ve had to downgrade the original dependencies we were using and I guess that’s where things went haywire. I dragged over a new For Each, moved the contents of the original and renamed the variable to item.ToString and it worked!

Thank you thank you thank you!

Carlo.

1 Like

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.