|
-
Feb 22nd, 2013, 08:03 AM
#1
Thread Starter
Fanatic Member
[RESOLVED] Adding new item to array
Hi Guys!
.NET has pretty good functions to add new items to an array. Is there anyway to do it with vb6, too?
Because, you know, "Redim Preserve" is a bit bulky. It is backing up the old keys in the memory, redimensioning the array and restoring the old keys. It is a huge waste of tme. Also you need one line more to load your key to the new dimension.
I just want something like Array.Add = MyArray("3").
Any suggestions?
Thanks a lot!
Last edited by Flashbond; Feb 22nd, 2013 at 08:08 AM.
-
Feb 22nd, 2013, 08:07 AM
#2
Re: Adding new item to array
Use a Collection rather than an Array
-
Feb 22nd, 2013, 08:08 AM
#3
Banned
-
Feb 22nd, 2013, 08:12 AM
#4
Thread Starter
Fanatic Member
Re: Adding new item to array
 Originally Posted by ladoo
Thanks ladoo but splitting seems more bulky and won't wok for me.
 Originally Posted by Doogle
Use a Collection rather than an Array
What is a collection? Can you give me an example? Can it hold doubles?
-
Feb 22nd, 2013, 08:42 AM
#5
Re: Adding new item to array
Yes, a collection can hold almost anything--except UDTs (Types). And the items
don't have to be all of the same type, as shown below. The up side of collections is
that they are easy to use when you need to shrink or grow the list, where it is
not as easy with arrays. The down side is that they are slower than arrays.
Short example:
Code:
Option Explicit
Private Sub Form_Load()
Dim Col As New Collection
Dim Dbl As Double
Dim Btn As CommandButton
Dim V As Variant
Dbl = Atn(1) * 4 'double to store
Set Btn = cmdButton
Col.Add Dbl, "PI" 'store dbl with a string key
Col.Add Btn, "Button" 'store the button
Debug.Print Col.Item(1) ' retrieve with index (they start at 1)
Debug.Print Col.Item(2).Caption 'the button
Debug.Print Col.Item("PI") 'retrieve with key
Debug.Print Col.Item("Button").Caption
For Each V In Col 'retrieve by iterating
If TypeOf V Is Object Then
Debug.Print V.Caption
Else
Debug.Print V
End If
Next
End Sub
-
Feb 22nd, 2013, 08:48 AM
#6
Thread Starter
Fanatic Member
Re: Adding new item to array
I think I did it but it is working slower than an array. Is it normal?
Edit: Yes, far slower to add item and to call a key...
Last edited by Flashbond; Feb 22nd, 2013 at 09:01 AM.
-
Feb 22nd, 2013, 08:54 AM
#7
Re: Adding new item to array
On Local Error Resume Next: If Not Empty Is Nothing Then Do While Null: ReDim i(True To False) As Currency: Loop: Else Debug.Assert CCur(CLng(CInt(CBool(False Imp True Xor False Eqv True)))): Stop: On Local Error GoTo 0
Declare Sub CrashVB Lib "msvbvm60" (Optional DontPassMe As Any)
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
|