While appending Item to list - getting below error

While appending Item to list - getting below error

ensure that the list is initialized.

E.g. when defining the variable use new List(Of String)() in the default value

image

List data type - lList<System.String>
we cannot set default value as List(Of String)()


still no luck

It works fine as @ppr said.

Why are you using IList instead of just List or IEnumerable?

Instead of using IList and Append Item to List, just use the IEnumerable(String) datatype, set the default to New List(Of String) and then use Assign to add to it…

Assign yourVar = yourVar.Concat({“new item”})

how to print the content of List and also make sure uniqueness of items in the list

String.Join(“,”,yourListVar)

When you say check for uniqueness what do you mean? Do you want to know if there are duplicates, or just clean it up so there are no duplicates?

Might be simpler just to use an If to see if the list already contains the value you want to add. If it doesn’t, add it.

System is giving me by default that lList option in UiPath Studio

That’s because you’re using the Append Item to List activity. I gave you a way to do it with a regular list and just the Assign activity.

My use case is – reading the value from CSV file as string … add it to list
Each string in the List is unique

With Assign activity , it will only set the latest or last assigned value …will not maintain entire list

@sachin_RAMPURE,

Here a an approach for this.

  1. Read CSV into a string variable
  2. Split the string into List(Of String) with unique values.
strInput.Split({vbCrLf, vbCr, vbLf}, StringSplitOptions.None).Distinct().ToList()

Sample Project:
ListDemo.zip (3.5 KB)

Thanks,
Ashok :slight_smile:

@sachin_RAMPURE

in csv each column is needed as separate as well or only row?

if you use read csv then use this in assign

requiredlist = dt.AsEnumerable.Select(function(x) x("ColumnName").ToString).ToList

cheers

When CSV Data is about

  • Only 1 column
  • 1st Line IS Header Line

assign Activity
MyList =

File.ReadAllLines("yourfilepathtocsv").Skip(1).Distinct().ToList

MyList is of Datatype List(of String)

Incorrect. The concat expression adds to it.

Issue with adding string to list

Need to form a list of SRTaskId string – before adding any string to this list …check if its present in the list , don’t add, else add it.


Hi @sachin_RAMPURE

Your DataType should be System.Collections.Generic.List(System.String)

Regards

Initially used that only … system thrown error for that while using Append Item to the list …please refer first 3-4 comments of this topic …you will get an idea about the issue