|
-
Jul 15th, 2002, 05:06 PM
#1
Thread Starter
Junior Member
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;
-
Jul 15th, 2002, 08:49 PM
#2
-
Jul 15th, 2002, 09:27 PM
#3
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:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim myArray(90) As Long
Dim myTempArray() As Long
Dim intCount As Integer
'Populate some values into the array.
For intCount = 0 To 89
myArray(intCount) = CLng(intCount)
Next
'To upsize the array bounds
'First set the temp to the same size as original array.
ReDim myTempArray(90)
'Copy the original array to the temp.
myArray.CopyTo(myTempArray, 0)
'Erase original array and redim it.
Erase myArray
ReDim myArray(900)
'Copy the temp array back into the original (now upsized) array.
myTempArray.CopyTo(myArray, 0)
'This just shows when it is done and that the original contents
'have been copied back. Also shows that the added
'dimensions are initialized to 0.
MessageBox.Show("Done")
For intCount = 0 To 110 Step 10
MessageBox.Show(myArray(intCount).ToString())
Next
End Sub
-
Jul 15th, 2002, 10:20 PM
#4
Frenzied Member
If you dont want to redim and array, use an arraylist. You can add and remove from the list dynamically.
-
Jul 15th, 2002, 11:12 PM
#5
I believe the arraylist will have more overhead, but I haven't checked it personally.
-
Jul 16th, 2002, 12:54 AM
#6
Dazed Member
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.
-
Jul 16th, 2002, 01:05 AM
#7
Dazed Member
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.
-
Jul 16th, 2002, 01:10 PM
#8
Thread Starter
Junior Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|