spacemonkeys
spacemonkeys

Reputation: 1749

adding blank elements to array

I have an array x in size of objects (between 1 and 100) and I want to increase the size to 101 ... I've resized the array and that adds the objects but unfortunatly (not suprising) the added items have not been initialised, do I've reverted to using a do while loop and adding the elements indiviually, but looking at the code around it where addrange is used extensivily, I was just wondering if that was a neat vb.net way of doing the same thing

Bit of a learning question, just looking for neat ways to do the same thing

Thanks in advance

Upvotes: 0

Views: 876

Answers (4)

tobrien
tobrien

Reputation: 618

I am (then) not sure what your looking to accomplish... using ReDim Preserve (older) or the .reSize (newer) will certainly add to the "end" of the array and those elements MUST BE null since you cannot pass new elements into the constructor.

But is that what your looking for ? A way to pass the new values into the constructor of the new elements so it returns with the old-er values as will as the new ones?

Even if you overload the constructor, you'll have to LOOP the new values in. (As far as I can see.)

Upvotes: 0

tobrien
tobrien

Reputation: 618

Are you looking for this....

ReDim PRESERVE thatArray(to_the_new_size)

The preserve should, uhm, preserve the original values in their original places.

Upvotes: 0

MarkJ
MarkJ

Reputation: 30398

Try not using an array but a List instead. They're much easier to use and give more control.

Dim myList As List(Of Whatever)  
myList.Add(New Whatever)

Upvotes: 0

dr. evil
dr. evil

Reputation: 27265

Yes you need to loop and add new objects into newly added indexes.

Upvotes: 2

Related Questions