开发者

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 Attri开发者_Go百科buteInfo. But the code doesn't seem to work. Any ideas

Thanks Jothish


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();


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)


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.


Try this,

oItemSubmit.AttributeList = theNewList.ToArray
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜