Lesson 2 - Practice 3 Walkthrough For each loop on an array of strings, item conversion using ToString


#1

Hi all,

I’m going through the training material and have a question regarding the for each loop that is applied to array of strings.

https://academy.uipath.com/lms/index.php?r=player&course_id=1&coming_from=lp&id_plan=22#training

As the loop goes through the array of strings it ‘write lines’ a phrase with each of the members of the array (which are all strings) each time it goes through the loop.

My question is why does the item variable which stores the string, have to be converted using .ToString to attach it to the rest of the string (why is item an object and not a string as we are talking about an array of strings, whats the point in defining the array as an array of strings in the first place?).

Sorry if this is a stupid question.

Thanks,


#2

by default, the TypeArgument of the ‘for each’ activity is an object. if you’d change it to string, you won’t have to use .toString anymore.


#3

Thanks Tiberiu for explaining. I’ve never thought of a control structure (such as a loop) as having a type I’ve always just thought of them interacting with objects (that are typed) and I didn’t even notice that the for each is typed (I still don’t really understand why it would be typed but at least that clears up why it needs the .ToString conversion).

Please excuse the overuse of the word Type :keyboard: