JoR
JoR

Reputation: 305

Converting an Array to list and adding values

I am trying to add a value to an array by converting it to a list

Dim oItemSubmit As InventoryService.InventoryItemSubmit

 Dim oAttInfo As New InventoryService.AttributeInfo
 oAttInfo.Name = "FeatName"
 oAttInfo.Value = "value"
 oItemSubmit.AttributeList.ToList().Add(oAttInfo)

AttributeList is an array on AttributeInfo. But the code doesn't seem to work. Any ideas

Thanks Jothish

Upvotes: 1

Views: 781

Answers (4)

PdotWang
PdotWang

Reputation: 147

Try this,

oItemSubmit.AttributeList = theNewList.ToArray

Upvotes: 0

Stecya
Stecya

Reputation: 23266

AttributeList.ToList() is making an copy, and you are inserting into copy, not into original array

You can use Concat to add element or sequence

C# sample

oItemSubmit.AttributeList = oItemSubmit.AttributeList.Concat(new []{oAttInfo}).ToArray();

Upvotes: 4

Anton Gogolev
Anton Gogolev

Reputation: 115751

The oItemSubmit.AttributeList.ToList() call creates a whole new instance of List(Of AttributeInfo) class, which has nothing to do with the original array other than that it contains items from it. Adding items to it will not add items to said array.

Upvotes: 0

Aliostad
Aliostad

Reputation: 81660

oItemSubmit.AttributeList.ToList().Add(oAttInfo)

is adding the element to a temporary list which is created and then not used.

You need to store the reference and then add to it:

Dim myList as List<MyType>
myList = oItemSubmit.AttributeList.ToList()
myList.Add(oAttInfo)

Upvotes: 0

Related Questions