Results 1 to 8 of 8

Thread: Redimensioning Arrays

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Mar 2002
    Location
    England
    Posts
    23

    Redimensioning Arrays

    hi all...

    using VB6, you could use a system like this:

    Dim SomeArray() as Long
    Redim SomeArray(90) as long
    Redim Preserve SomeArray(900) as long

    and you'd be able to change the last dimension.

    Technically this still works in VB.Net, but its excruciatingly slow!!! when timing, VB6 was almost 9x faster than VB.Net...

    SO, I'm guessing - whats the ".Net way" of resizing/redimensioning arrays?

    Jack;

  2. #2
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428
    Well that's the only way that I know of doing it. None of the other .NET languages support resizing arrays at runtime (as far as I've heard), so I guess that's the reason that it is slow, probably the VB guys has to write some kind of nasty code to make it compatible with .NET
    anyways I think that's the only way to do it....

  3. #3
    hellswraith
    Guest
    I did this, it seems to happen instantly on my computer. I am running on a PII 450 with 196Mb of ram and Windows XP Home.

    VB Code:
    1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    2.         Dim myArray(90) As Long
    3.         Dim myTempArray() As Long
    4.         Dim intCount As Integer
    5.        
    6.         'Populate some values into the array.
    7.         For intCount = 0 To 89
    8.             myArray(intCount) = CLng(intCount)
    9.         Next
    10.  
    11.         'To upsize the array bounds
    12.         'First set the temp to the same size as original array.
    13.         ReDim myTempArray(90)
    14.  
    15.         'Copy the original array to the temp.
    16.         myArray.CopyTo(myTempArray, 0)
    17.  
    18.         'Erase original array and redim it.
    19.         Erase myArray
    20.         ReDim myArray(900)
    21.  
    22.         'Copy the temp array back into the original (now upsized) array.
    23.         myTempArray.CopyTo(myArray, 0)
    24.  
    25.         'This just shows when it is done and that the original contents
    26.         'have been copied back.  Also shows that the added
    27.         'dimensions are initialized to 0.
    28.         MessageBox.Show("Done")
    29.         For intCount = 0 To 110 Step 10
    30.             MessageBox.Show(myArray(intCount).ToString())
    31.         Next
    32.     End Sub

  4. #4
    Frenzied Member DevGrp's Avatar
    Join Date
    Nov 2001
    Location
    Charlotte, NC
    Posts
    1,256
    If you dont want to redim and array, use an arraylist. You can add and remove from the list dynamically.

  5. #5
    hellswraith
    Guest
    I believe the arraylist will have more overhead, but I haven't checked it personally.

  6. #6
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    As far as ive heard an array in .Net must first be dimensioned with Dim. ReDim cannot be used initially. Plus an array cannot grow or lose dimensions, but the size of the existing dimensions can be changed.

  7. #7
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    Posted by hellswraith
    I believe the arraylist will have more overhead, but I haven't checked it personally.
    I think that you would be right. An array should run faster since it is a block of computer memory that contains multiple instances of the same data type. Plus you would have to take into account wether an arraylist offers synchronization(thread safety). In Java you usualy get better performance using an ArrayList vs a Vector.

  8. #8

    Thread Starter
    Junior Member
    Join Date
    Mar 2002
    Location
    England
    Posts
    23
    thanks for the replies people

    I'll have a look into that temporary-array method and these "arraylist" things...

    Jack;

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width