1

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

4 Answers 4

4

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();
Sign up to request clarification or add additional context in comments.

Comments

0
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)

1 Comment

Thanks Aliostad. I was thinking of some one liner to do the code.
0

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.

Comments

0

Try this,

oItemSubmit.AttributeList = theNewList.ToArray

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.