ReDim Statement

June 18, 2007 at 6:07 am (.NET, ASP.NET)

ReDim statement is used to resize an existing array.

Example :-

Dim names(2) as String = {“james”,”jack”,”rose”}

now if we need to add another name then we can resize the array using the ReDim statement.

ReDim names(3)

In the above example the old data in the array will be deleted when redimmed, We can use the Preserve keyword to retain the old data while ReDimming.

ReDim Preserve names(3)

What it actually does is that it copies the old data into the new array with the new size and then delete the old one thereforel it is better to avoid using ReDim statement. Instead you can use ArrayList if the exact size is not known.

Permalink Leave a Comment