tags:

views:

42

answers:

4

Hi

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

+2  A: 

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

dr. evil
A: 

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)
MarkJ
Agree, however using existing OBI where an array is passed to me and adding to a Table.Rows colection, so can't change the methodology. Also your answer do give how to add 47 instance without doing the do loop
spacemonkeys
Cheers though :-)
spacemonkeys
A: 

Are you looking for this....

ReDim PRESERVE thatArray(to_the_new_size)

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

tobrien
Wrong end, the issue is that the new items added (object array) when you go from 10 items to 15, items 10-15 have not been initialized with the NEW statementAlso the VB.Net way to so the above (which is what I am using) is array.resize(Array,new size) which doesn't destroy the contents of the array up to the new size
spacemonkeys
A: 

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

tobrien
Hi Tobrien, yes that was the question. The only reason that I asked the question was out of interest, I used table.rows.addrange(array.convertall(array,delegate function that converted all element to table rows))) which converted the array to table rows .. then I had to itterate to the max count adding rows, which after the first statement, seemed a bit long winded, so I asked the question out of interest, as I said in the question I'd achieved what I wanted, just looking for alternate ways to do it
spacemonkeys
In the bad old days, what you would have ended up doing was grabbing (i.e. overloading) the constructor and then using a Windows USER.dll call to move memory or some such (i.e. at the assembly code level) to populate the new entries quickly. As much as most of love/hated the DECLARE features, they DID provide speed when needed.tob
tobrien