Argument 'Value': BC30311: Value of type 'String()' cannot be converted to 'List(Of String)'. The selected value is incompatible with the property type

Hello everyone,

I get the following error when trying to put a string list into an variable of the type string list, and saw online that the solution would be to use the function “New List(Of String)” to work around the error, but I would like to know why I cannot just create it without it whereas I can do so with an array of strings ?

For reference, my variable is as follows :
image

And I try to give it the following values via an assign :

Am I using the wrong data type ?

Thanks in advance,

Bastien

Hi @Bastien_B

Try this,
ErrorList = New List(Of String) From {“Ax001”, “Ax002”, “Ax003”, “Ax004”, “Ax005”, “Bx001”, “Bx002”, “Bx003”, “Cx001”, “Cx002”, “Cx003”, “Cx004”}

Yes, you are creating an Array and the argument asked for a List

They are different data types that have different functionality.

A list can be sorted, and have items easily added and removed, and array cannot.

They are both IEnumerable so if an argument asks for that you can use either Array or List, but if it asks specifically for a list you have to add a list.

You indeed need to create a New List(of string) but you can immediately populate it by adding From {"sddfsf", "sdafsdfs"}

3 Likes

Thank you for your answer !
I have a follow up question : Does {value, value, value} thus refer to an array specifically or a IEnumerable ? If it is specific to an array, is there some representation of a list that is equivalent ? Like [value, value, value], that would allow to create it without creating a New List(of String) ?

An IEnumerable is what is called an interface, which means its an abstract object and it cannot be directly created itself.

To try to make this into a real world example. The concept of an ‘Animal’ could be considered abstract, but is distinct from a ‘plant’.
If I tell you to create an ‘animal’ its not specific enough.
But if I tell you to make a ‘Dog’ it can be specific enough, and a dog is a type of animal, but so is a tunafish.

In this way, you can never directly create an IEnumerable, you can only create objects that inherit ienumerable, such as an Array, List, Collection, Dictionary etc.

Regarding making a List without doing New List(of string), no. You always need that somewhere. You can use an object initializer as I was suggesting to add the From {…} part to immediately populate the list as its created, but you HAVE to create the list first, always.

1 Like

Thank you for your detailed explanation, it helped a lot !

But then, how is it possible to create arrays with just {Value,Value} and no other type of enumerable without initiating an instance of it first ?

Its a quirk of VB.NET specifically. You can’t actually create an Array in C# for example without creating a new Array.

C# is, in my opinion, a far superior language because of things like this cause its so much more consistent and has smart syntax usage etc, but the idea for VB.NET was to be ‘beginner friendly’, however sometimes I think this intially friendliness just causes more confusion down the line.

As for some more technical reasons, its to do with the complexity of the object, a List is more complex than an array, with arrays being more like literals which are hard coded and dont change compared to a List, which does. For whatever reason, they decided that arrays could skip the initializer and when the code compiles it would notice this and add the intializer in the final code the machine reads.

So, the answer to really really know why they made this choice, go back to Microsoft a few decades ago and ask them why they think it makes it more ‘beginner friendly’. I am not a fan, but it is what we have.

1 Like

Oh, okay, thanks for the explanation !
One last question if you do not mind, how can I instanciate a list variable first that will be populated at a later time ? Do I just use an assign with the New List() function or is there some way to instanciate at the time it receives it’s first value ?

New List() wont work as you havent specified the data type of the list,

You need to do New List(of T) where T is the list data type, such as New List(of string) or new List(of int32) etc

You can then populate this list with other values any time you want with the .Add method.

Directly calling that method isnt so easy in UiPath workflows, so they have a couple of activities to help, I think called something like Append Item To List or Append Item to Collection.

They havent helped with the confusion here because, as I recall, I think the List was was working with a ICollection so worked with Lists and Collections, but I digress, I think they both work with Lists.

1 Like

Yes, my bad, I knew about the type mention, just didn’t write it in as I was asking about some way of instanciating at the same time as giving it it’s first value if it was empty.

Thank you a lot for your explanations !

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